Purpose Purpose of this lab is for you to develop a program
Purpose
Purpose of this lab is for you to develop a program where many objects are created from a class. Primitives and objects are passed as parameters to class methods.
Problem specification
Your new Wallet class implements a wallet that contains banknotes. A banknote is represented as an int for simplicity, 1 for a $1 bill, 5 for a $5 bill, and so on. You are required to use just a simple array of int to hold the banknotes. 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 each method 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 add(int banknote) {
// your code goes here
}
public void transfer(Wallet donor) {
// 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 representing banknotes. count holds the number of banknotes in contents[], so must be updated every time a banknote is added or removed. count is 0 when a wallet is empty. MAX is the maximum number of banknotes a wallet can hold, so count is MAX when a wallet is full. count will always be the index where the next banknote is to be added. Banknotes in a wallet are maintained in the order of arrival. A new banknote is simply added to the end of contents[] and count is incremented by 1. When a banknote is removed from a wallet, the first occurrence of the value is removed from contents[] and count is decremented by 1. Importantly, all banknotes above the removed value must be moved ‘down’ so that no ‘holes’ open in the array. For example, if wallet w is Wallet[1, 5, 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[] an overloaded 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 standard format. For example: Wallet[5, 50, 10, 5] • use a StringBuilder to build the returned value • convert the StringBuilder 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 in the wallet • (cannot use contents.length since this traverses every element in the array including elements that may not have been explicitly initialized, which is dangerous) • return this number of dollars
public void add(int banknote) add banknote to the wallet • banknote is the banknote to add e.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 end of another, the receiver. (The receiver will be the Wallet object calling the method (i.e. the invoking object)). Leave the donor wallet empty. For example: 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] and donor 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 boolean remove(int banknote) remove first occurrence of banknote from the wallet. Return true if banknote was removed, false otherwise • (this banknote may not be in the wallet) • if banknote is removed, must update contents[] so that no holes appear, and count
public boolean sameBanknotesSameOrder(Wallet other) return whether two wallets have exactly the same banknotes in exactly the same order
Extra credit
public Wallet removeBanknotePairs(Wallet w) create a new wallet and add to it pairs of banknotes removed from two other wallets. Return the new wallet.
The WalletTester class
The WalletTester class tests your new Wallet class. Source code for WalletTester is given below.
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(\"\ value of myWallet is: $\" + myWallet.value());
// transfer all the banknotes from myWallet to yourWallet!
Wallet yourWallet = new Wallet();
yourWallet.add(1);
yourWallet.transfer(myWallet);
System.out.println(\"\ now myWallet contains: \" + myWallet.toString());
System.out.println(\"yourWallet contains: \" + yourWallet.toString());
// remove all $5 banknotes from yourWallet
while (yourWallet.remove(5)) ;
System.out.println(\"\ yourWallet with $5s removed is: \" + yourWallet.toString());
// check whether two wallets have the same banknotes
// in the same order
int b[] = {10, 5, 10};
Wallet tom = new Wallet(b);
int c[] = {10, 5, 10, 1};
Wallet dick = new Wallet(c);
int d[] = {10, 5, 10};
Wallet harry = new Wallet(d);
System.out.println( \"\ tom 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 pairs
int 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(\"\ w1 is: \" + w1.toString());
System.out.println(\"w2 is: \" + w2.toString());
System.out.println(\"w3 is: \" + w3.toString());
}
}
Solution
PROGRAM CODE:
package sample;
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() {
count = 0;
contents = new int[MAX];
}
public Wallet(int a[]) {
contents = new int[MAX];
contents = a;
count = a.length;
}
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(\"Wallet[\");
for(int i=0; i<count; i++)
{
builder.append(contents[i]);
if(i< count-1)
builder.append(\" \");
}
builder.append(\"]\");
return builder.toString();
}
public int value() {
int value = 0;;
for(int i=0; i<count; i++)
{
value = value + contents[i];
}
return value;
}
public void add(int banknote) {
contents[count] = banknote;
count++;
}
public void transfer(Wallet donor) {
for(int i=0; i<donor.count; i++)
{
add(donor.contents[i]);
}
donor.count = 0;
}
public boolean remove(int banknote) {
boolean isRemoved = false;
int index = 0;;
for(int i=0; i<count; i++)
{
if(contents[i] == banknote)
{
index = i;
isRemoved = true;
break;
}
}
if(isRemoved)
{
for(int i=0; i<count; i++)
{
if(i>=index && (i+1)!=count)
{
contents[i] = contents[i+1];
}
}
}
count--;
return isRemoved;
}
public boolean sameBanknotesSameOrder(Wallet other) {
boolean isSame = true;
for(int i=0; i<count; i++)
{
if(this.contents[i] != other.contents[i])
{
isSame = false;
break;
}
}
return isSame;
}
//EXTRA CREDIT methods follow...
public Wallet removeBanknotePairs(Wallet w) {
Wallet wallet = new Wallet();
int counter = 0;
while(counter<2 )
{
wallet.add(this.contents[0]);
this.remove(this.contents[0]);
counter++;
}
counter =0;
while(counter<2)
{
wallet.add(w.contents[0]);
w.remove(w.contents[0]);
counter++;
}
return wallet;
}
}
OUTPUT:
myWallet contains: Wallet[5 50 10 5]
value of myWallet is: $70
now myWallet contains: Wallet[]
yourWallet contains: Wallet[1 5 50 10 5]
yourWallet with $5s removed is: Wallet[1 50]
tom has same banknotes in same order as dick: true
tom has same banknotes in same order as harry: true
w1 is: Wallet[50 20 50 5]
w2 is: Wallet[5 5 5 50 10]
w3 is: Wallet[5 1 20 10]







