X


Payment

Question

Wallet Java Lab

Problem specification

Your new Wallet class implements a wallet that containsbanknotes. A banknote isrepresentedasanintforsimplicity,1fora$1bill,5fora$5bill,andsoon.Youare required to use just a simple array of int to hold thebanknotes. You may NOT use an array list.

Here are some example wallets printed out:

Wallet[5, 50, 10, 5]Wallet[]Wallet[1, 5, 10, 50, 5]

Here's the outline of the Wallet class. You will implement eachmethod as described below.

public class Wallet
{    // max possible # of banknotes in a wallet    private static final int MAX = 10;
    private int contents[];    private int count;     // number of banknotes stored in contents[]
    public Wallet()
    {        // your code goes here

}

    public Wallet(int a[])
    {        // your code goes here

}

    public String toString()
    {        // your code goes here

}

    public int value()
    {        // your code goes here

}

public void reverse()
{    // your code goes here

}

public void add(int banknote)
{    // your code goes here

}

public void transfer(Wallet donor)
{    // your code goes here

}

public void sort()
{    // your code goes here

}

public boolean remove(int banknote)
{    // your code goes here

}

public boolean sameBanknotesSameOrder(Wallet other)
    {        // your code goes here
    }    //EXTRA CREDIT methods follow...
    public Wallet removeBanknotePairs(Wallet w)
    {        // your code goes here

} }

Instance variables
Use exactly and only these instance variables:

Wallet uses the contents[] array to store ints representingbanknotes. count holds the number of banknotes in contents[], somust be updated every time a banknote is added or removed.

count is 0 when a wallet is empty. MAX is the maximum number ofbanknotes a wallet can hold, so count is MAX – 1 when a wallet isfull. count will always be the index where the next banknote is tobe added.

Banknotes in a wallet are maintained in the order of arrival. Anew banknote is simply added to the end of contents[] and count isincremented by 1.

When a banknote is removed from a wallet, the first occurrenceof the value is removed fromcontents[]andcountisdecrementedby1.Importantly,allbanknotesabovethe removed value must be moved ‘down’so that no ‘holes’ open in the array. For example, if wallet w isWallet[1, 5, 10, 50, 5]:

- then after w.remove(5), w will be Wallet[1, 10, 50, 5]:

Methods
You will complete the method bodies as follows:

public Wallet()

initialize the wallet to empty

 allocate memory for the contents[] array

 initialize count

public Wallet(int a[])
initialize the wallet using the array of int named a[] anoverloaded constructor

 allocate memory for the contents[] array

 initialize contents[] from a[]

 initialize count

public String toString()

return a textual representation of the wallet, in the standardformat. For example:

   Wallet[5, 50, 10, 5]

 use a StringBuffer to build the returned value

 convert the StringBuffer to a String and return the String

public int value()

calculate the value of the banknotes in the wallet. For example,if wallet is: Wallet[5, 50, 10, 5], value is 70

 must use count to do this, to traverse ONLY the banknotes inthe wallet

 (cannot use contents.length since this traverses every elementin the array

including elements that may not have been explicitlyinitialized, which is dangerous)

 return this number of dollars

public void reverse()

reverse the order of banknotes in a wallet e.g.
Wallet[1, 5, 10, 50, 5] when reversed becomes Wallet[5, 50, 10, 5,1]

(IMPORTANT NOTE: do not create a new Wallet object!)

 suggested algorithm:

start an index pointing at the first banknote incontents[]
start another index pointing at the last of the banknotes incontents[] while these indexes do not meet or cross over

swap values at the indexes (use a temp variable to do this) movetwo indexes towards one another

public void add(int banknote)
add banknote to the wallet
 banknoteisthebanknotetoadde.g.50,5,etc

 add banknote to the end of contents[] and update count

public void transfer(Wallet donor)
transfer the contents of one wallet (the donor wallet) to the endof another, the receiver. (The receiver will be the Wallet objectcalling the method (i.e. the invoking object)).Leavethedonorwalletempty. Forexample:

if the receiver is Wallet[5, 10, 50, 50]
and donor is Wallet[5, 5, 10, 1, 50, 5]
then after the transfer:
receiver will be Wallet[5, 10, 50, 50, 5, 5, 10, 1, 50, 5] anddonor will be Wallet[]

 should call the add() method as you implement this

 to set a wallet to empty, simply set its count to 0

public void sort()

sort the banknotes in a wallet into ascending order e.g.
Wallet[5, 50, 10, 5, 1] when sorted becomes Wallet[1, 5, 5, 10,50]

(IMPORTANT NOTE: do not create a new Wallet object!)

 suggested algorithm:

 we saw in Week 7 a sort method that works for an array ofInteger objects containing MAX elements

 take this method and modify syntax to work for contents[] andcount

public boolean remove(int banknote)removefirstoccurrenceofbanknotefromthewallet.Returntrueifbanknotewas removed, false otherwise

 (this banknote may not be in the wallet)

 if banknote is removed, must update contents[] so that noholes appear, and count

public boolean sameBanknotesSameOrder(Wallet other)

return whether two wallets have exactly the same banknotes inexactly the same order Extra credit

public Wallet removeBanknotePairs(Wallet w)

create a new wallet and add to it pairs of banknotes removedfrom two other wallets. Return the new wallet. For example:

if w1 is Wallet[5, 1, 50, 20, 50, 5]

and w2 is Wallet[20, 10, 5, 5, 5, 50, 10] then after the methodcall
Wallet w3 = w1.removeBanknotePairs(w2);

w1 will be: Wallet[1, 50]
w2 will be: Wallet[10, 5, 10]
w3 will be: Wallet[5, 5, 50, 50, 20, 20, 5, 5]

The WalletTesterclass
TheWalletTesterclasstestsyournewWalletclass.SourcecodeforWalletTesteris given below, and can be downloaded fromBlackboard, Course Documents, Week 10 folder, Example programs.

/** * Test the Wallet class. * * @author Anthony W. Smith * @version 5/31/2028 */
public class WalletTester
{    public static void main(String args[])
    {        // create a new Wallet object using an array        int a[] = {5, 50, 10, 5};        Wallet myWallet = new Wallet(a);
        // show the contents of myWallet        System.out.println("myWallet contains: " +
                                        myWallet.toString());
        // print the value of myWallet        System.out.println("nvalue of myWallet is: $" +
                                            myWallet.value());
        // reverse the order of banknotes in myWallet        myWallet.reverse();        System.out.println("nmyWallet reversed contains: " +
                                          myWallet.toString());
        // transfer all the banknotes from myWallet to yourWallet!        Wallet yourWallet = new Wallet();
yourWallet.add(1);yourWallet.transfer(myWallet);System.out.println("nnow myWallet contains: " +
                                     myWallet.toString());System.out.println("yourWallet contains: " +
                                    yourWallet.toString());
// sort yourWalletyourWallet.sort();System.out.println("nyourWallet sorted is: " +
                                   yourWallet.toString());
// remove all $5 banknotes from yourWalletwhile (yourWallet.remove(5))
    ;System.out.println("nyourWallet with $5s removed is: " +
                                    yourWallet.toString());
// check whether two wallets have the same banknotes// in the same orderint b[] = {10, 5, 10};Wallet tom = new Wallet(b);
int c[] = {10, 10, 5};Wallet dick = new Wallet(c);
int d[] = {10, 5, 10};Wallet harry = new Wallet(d);
System.out.println(     "ntom has same banknotes in same order as dick: " +
                           tom.sameBanknotesSameOrder(dick));
System.out.println(    "tom has same banknotes in same order as harry: " +
                          tom.sameBanknotesSameOrder(harry));
// EXTRA CREDIT – compare two wallets and remove banknote pairsint e[] = {5, 1, 50, 20, 50, 5};Wallet w1 = new Wallet(e);
int f[] = {20, 10, 5, 5, 5, 50, 10};Wallet w2 = new Wallet(f);
Wallet w3 = w1.removeBanknotePairs(w2);System.out.println("nw1 is: " + w1.toString());System.out.println("w2 is: " + w2.toString());System.out.println("w3 is: " + w3.toString());}}

Extra credit

When you have completed all other methods, implement and testremoveBanknotePairs() for 20% extra credit. You must test yourmethod using the w1 and w2 wallets given above in the descriptionof removeBanknotePairs().

Solution

xxxxxx:xxx:xyxxxxxx contains: xxxxxx[x,xx,xx,x]xxxxx of xyxxxxxx xx: $xxxyxxxxxx reversed xxxxxxxx: Wallet[5,10,50,5]

now xyxxxxxx xxxxxxxx: xxxxxx[]yxxxxxxxxx contains: xxxxxx[x,x,xx,x,x]yxxxxxxxxx sorted xx: xxxxxx[x,x,x,x,xx]yxxxxxxxxx xxxx $5s xxxxxxx is: xxxxxx[x,x,x,xx]xxx xxx xxxx banknotes xx same xxxxx xx xxxx: false
tom xxx same xxxxxxxxx xx xxxx order xx harry: xxxx
xx xx: xxxxxx[x,xx]xx is: xxxxxx[xx,x,xx]xx is: xxxxxx[x,x,xx,xx]xxxxx xxxxxxxxxx (xxxxx time: x seconds)

Wallet.java :xxxxxx xxxx.xxxx.xxxxyx;xxxxxx xxxxx Wallet
{
// xxx possible # xx xxxxxxxxx in x wallet
private xxxxxx xxxxx xxx MAX = 10;
private xxx xxxxxxxx[];xxxxxxx xxx count; // number xx xxxxxxxxx xxxxxx incontents[]
public xxxxxx(){xxxxxxxx=xxx int[MAX];
count=0;
}
public xxxxxx(xxx x[]){xxxxxxxx=xxx xxx[xxX];xxx(xxx i=0;i<a.length;i++)
{
contents[i]=a[i];
count++;
}
}
public xxxxxx toString()
{
String x="[";xxx(xxx x=x;x&xx;xxxxx;x++)xx(x&xx;xxxxx-x)x=x+xxxxxxx.xxxxxxxx(xxxxxxxx[x])+",";xxxxx=x+xxxxxxx.xxxxxxxx(xxxxxxxx[x]);xxxxxx "xxxxxx"+x+"]";}xxxxxx int xxxxx(){xxx sum=0;
for(int x=x;x&xx;xxxxx;x++){xxx=xxx+xxxxxxxx[x]; &#xxx;&#xxx;}xxxxxx xxx;}xxxxxx void xxxxxxx(){xxxxxxxx = xxxxxxxxxxxy(xxxxxxxx);}@xxxxxxxxxxxxxxxx("xxxxy-xxxxxxxxx")xxx[] xxxxxxxxxxxy(xxx[] xxxx){xxx[] reversedData = new xxx[xxX];xxx x=x,x=xxxxx-x;xxx(x=x;x&xx; xxxxx; i++)
{

reversedData[i] = data[j--];
  
}
return xxxxxxxxxxxx;}xxxxxx xxxx xxx(xxx banknote)
{
  
contents[count++]=banknote;
  
}
public xxxx transfer(Wallet xxxxx){xxx x=xxxxx.xxxxx;xxx(xxx x=x;x&xx;x;x++){xxx(xxxxx.xxxxxxxx[x]);xxxxx.xxxxxx(xxxxxxxx[x]);&#xxx;&#xxx;}}xxxxxx void xxxx(){xxx (int x = x; i &xx; count; x++) {xxx (xxx j = i + x; x < xxxxx; j++) {xxx xxx = 0;
if (xxxxxxxx[x] > xxxxxxxx[x]) {xxx = contents[i];
contents[i] = contents[j];
contents[j] = xxx;}}}}xxxxxx xxxxxxx remove(int xxxxxxxx){&#xxx;&#xxx;xxx f=10,i=0;
for( x=x;x&xx;xxxxx;x++){xx(xxxxxxxx[x]==xxxxxxxx);{x=x;xxxxx;}}xx(x!=xx){xxx(xxx x=x+x;x&xx;xxxxx-x;x++)xxxxxxxx[x]=xxxxxxxx[x+x];xxxxx--;xxxxxx xxxx;}xxxxxxxxxx false;

  
}
public xxxxxxx sameBanknotesSameOrder(Wallet xxxxx){xx(xxxxx==xxxxx.xxxxx){xxx x=x;xxx(xxx x=x;x&xx;xxxxx;x++)xx(xxxxxxxx[x]!=xxxxx.xxxxxxxx[x])x=x;xx(x==x)xxxxxx true;
else
return xxxxx;}xxxxxxxxxx false;
}
//EXTRA xxxxxx xxxxxxx xxxxxx...@xxxxxxxxxxxxxxxx("xxxxy-xxxxxxxxx")xxxxxx Wallet xxxxxxxxxxxxxxxxxxx(xxxxxx w)
{
Wallet x=xxx xxxxxx();xxx x=x,x=x;xx(xxxxx&xx;x.xxxxx){&#xxx;&#xxx;xxx(x=x;x&xx;xxxxx;x++){xxx(x=x;x&xx;x.xxxxx;x++)xx(xxxxxxxx[x]==x.xxxxxxxx[x]){x.xxx(xxxxxxxx[x]);xxxxxx(xxxxxxxx[x]);x.xxxxxx(xxxxxxxx[x]);}
}}xxxx{xxx(x=x;x&xx;x.xxxxx;x++){xxx(x=x;x&xx;xxxxx;x++)xx(xxxxxxxx[x]==x.xxxxxxxx[x]){x.xxx(xxxxxxxx[x]);xxxxxx(xxxxxxxx[x]);x.xxxxxx(xxxxxxxx[x]);xxxxx;}}}xxxxxx t;
}
}

WalletTester.java :xxxxxx class xxxxxxxxxxxx{&#xxx;&#xxx;&#xxx; xxxxxx xxxxxx void xxxx(xxxxxx args[])

    {&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx; // xxxxxx a xxxxxxxxx object xxxxx xx xxxxy&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx; int x[] = {x, xx, xx,x};&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx; Wallet xyxxxxxx = xxxxxxxxx(x);&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx; // xxxx the xxxxxxxxxx myWallet

       System.out.println("myWallet xxxxxxxx: " +&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;xyxxxxxx.xxxxxxxx());&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx; // xxxxx the xxxxx xxxyxxxxxx&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;xyxxxx.xxx.xxxxxxx("xxxxxx xx myWallet xx: $" +&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;xyxxxxxx.xxxxx());&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx; // xxxxxxx the xxxxxxx banknotes xx xyxxxxxx&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;xyxxxxxx.xxxxxxx();&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;xyxxxx.xxx.xxxxxxx("xxyxxxxxx xxxxxxxx contains: " +

                                         myWallet.toString());

        // xxxxxxxx xxx thebanknotes xxxx myWallet xx yxxxxxxxxx!&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx; xxxxxx yourWallet =xxx Wallet();

yourWallet.add(1);

yourWallet.transfer(myWallet);

System.out.println("nnow xyxxxxxx xxxxxxxx: " +

                                    myWallet.toString());

System.out.println("yourWallet xxxxxxxx: " +&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;yxxxxxxxxx.xxxxxxxx());// xxxx yxxxxxxxxxyxxxxxxxxx.xxxx();xyxxxx.xxx.xxxxxxx("xyxxxxxxxxx sorted xx: " +&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;yxxxxxxxxx.xxxxxxxx());// xxxxxx xxx $5 xxxxxxxxx from yxxxxxxxxxxxxxx (yxxxxxxxxx.xxxxxx(x))&#xxx;&#xxx;&#xxx; ;xyxxxx.xxx.xxxxxxx("xyxxxxxxxxx with $xx removed xx: " +&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;yxxxxxxxxx.xxxxxxxx());// check xxxxxxx two xxxxxxx xxxx xxx same xxxxxxxxx// in xxx xxxx xxxxxxxx b[] = {10, x, xx};xxxxxx xxx = xxx Wallet(b);

int x[] = {xx, 10, x};xxxxxx dick = xxx xxxxxx(x);xxx d[] = {10, x, xx};xxxxxx xxxxy = xxx Wallet(d);

System.out.println(

     "xxxx xxx xxxx banknotes xx same xxxxxxx xxxx: " +

                          tom.sameBanknotesSameOrder(dick));

System.out.println(

    "xxx has xxxx xxxxxxxxx xx same xxxxx asharry: " +&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;&#xxx;xxx.xxxxxxxxxxxxxxxxxxxxxx(xxxxy));// xXxxx CREDIT &#xxxx; compare xxx xxxxxxx xxx remove xxxxxxxxxxxxxxxx e[] = {x, x, 50, xx, 50, x};xxxxxx xx = new xxxxxx(x);xxx f[] = {xx, xx, 5, x, 5, xx, xx};xxxxxx xx = xxx Wallet(f);

Wallet xx = xx.xxxxxxxxxxxxxxxxxxx(xx);xyxxxx.xxx.xxxxxxx("xxx is: " + xx.xxxxxxxx());xyxxxx.xxx.xxxxxxx("xx xx: " + xx.xxxxxxxx());xyxxxx.xxx.xxxxxxx("xx is: " + xx.xxxxxxxx());}}

Payment



Payment