PF1 Lab9 - MoneyBag! For this lab, you will create a virtual MoneyBag that can b
ID: 3718734 • Letter: P
Question
PF1 Lab9 - MoneyBag! For this lab, you will create a virtual MoneyBag that can be used to store change (no paper money). You will provide methods to create/initialize the MoneyBags, compare two MoneyBags, add change to a MoneyBag, dump the contents of one MoneyBag to another, and make a purchase using a MoneyBag (subtracting the purchase price from a MoneyBag). Instead of a lengthy description for how the methods should function, I am going to provide you with a test program that will be the "customer" for your class. This will allow you to determine what methods/etc you need to provide. I'm also providing you with the output that this test program produced when I ran it with my MoneyBag class. You need to CAREFULLY STUDY the output and the test program to see how the methods should function. This is a logic problem in itself! One hint is to notice that the subtract method, which is used to make a purchase of the specified amount should always return a MoneyBag with the fewest amount of coins in it. In other words, you hand a clerk your moneybag, and he/she will "optimize" your change by returning it with the SMALLEST number of coins possible! So provided that your MoneyBag contains enough money to cover the price of the purchase, the composition of the change in the MoneyBag will not affect the composition of the change returned, which will always contain the fewest coins possible. Your lab9 source code should be in the directory ~/PF1/lab9, and should include the main method provided below. Note: Your program should work for other sets of test data. NOTE2: DO NOT MODIFY THE CODE I PROVIDE FOR YOU!!!!!! = TEST PROGRAM ====================================================== public class Lab9 { public static void main(String args[]) { MoneyBag mb0 = new MoneyBag(); MoneyBag mb1 = new MoneyBag(1, 2, 3, 4); int value; System.out.println("mb0:"); mb0.printContents(); value = mb0.getValue(); System.out.println("value = " + value + " "); System.out.println("mb1:"); mb1.printContents(); value = mb1.getValue(); System.out.println("value = " + value + " "); System.out.println("adding $0.50 to mb0"); mb0.add(2, 0, 0, 0); System.out.println("mb0:"); mb0.printContents(); value = mb0.getValue(); System.out.println("value = " + value + " "); System.out.print("Comparing mb0 and mb1: "); mb0.compare(mb1); System.out.println(" Adding $0.14 to mb0"); mb0.add(0, 0, 2, 4); System.out.println("mb0:"); mb0.printContents(); value = mb0.getValue(); System.out.println("value = " + value + " "); System.out.print("Comparing mb0 and mb1: "); mb0.compare(mb1); System.out.println(" Adding to mb0"); mb0.add(0, 0, 0, 1); System.out.println("mb0:"); mb0.printContents(); value = mb0.getValue(); System.out.println("value = " + value + " "); System.out.print("Comparing mb0 and mb1: "); mb0.compare(mb1); System.out.println(" Adding contents of mb1 into mb0"); mb0.add(mb1); System.out.println("mb0:"); mb0.printContents(); value = mb0.getValue(); System.out.println("value = " + value + " "); System.out.println("mb1:"); mb1.printContents(); value = mb1.getValue(); System.out.println("value = " + value + " "); System.out.println("Creating a new MoneyBag"); MoneyBag mb2 = new MoneyBag(5, 4, 3, 2); System.out.println("mb2:"); mb2.printContents(); value = mb2.getValue(); System.out.println("value = " + value + " "); System.out.println("Subtracting $5.00"); mb2 = mb2.subtract(500); System.out.println("mb2:"); mb2.printContents(); value = mb2.getValue(); System.out.println("value = " + value + " "); System.out.println("Oops, let's try $0.50"); mb2 = mb2.subtract(50); System.out.println("Change due:"); System.out.println("mb2:"); mb2.printContents(); value = mb2.getValue(); System.out.println("value = " + value + " "); System.out.println("Subtracting $0.41"); mb2 = mb2.subtract(41); System.out.println("Change due:"); System.out.println("mb2:"); mb2.printContents(); value = mb2.getValue(); System.out.println("value = " + value + " "); } } = OUTPUT ============================================================ mb0: quarters = 0, dimes = 0, nickels = 0, pennies = 0 value = 0 mb1: quarters = 1, dimes = 2, nickels = 3, pennies = 4 value = 64 adding $0.50 to mb0 mb0: quarters = 2, dimes = 0, nickels = 0, pennies = 0 value = 50 Comparing mb0 and mb1: IS LESS THAN Adding $0.14 to mb0 mb0: quarters = 2, dimes = 0, nickels = 2, pennies = 4 value = 64 Comparing mb0 and mb1: IS EQUAL TO Adding to mb0 mb0: quarters = 2, dimes = 0, nickels = 2, pennies = 5 value = 65 Comparing mb0 and mb1: IS GREATER THAN Adding contents of mb1 into mb0 mb0: quarters = 3, dimes = 2, nickels = 5, pennies = 9 value = 129 mb1: quarters = 0, dimes = 0, nickels = 0, pennies = 0 value = 0 Creating a new MoneyBag mb2: quarters = 5, dimes = 4, nickels = 3, pennies = 2 value = 182 Subtracting $5.00 Insufficient funds - purchase denied. mb2: quarters = 5, dimes = 4, nickels = 3, pennies = 2 value = 182 Oops, let's try $0.50 Change due: mb2: quarters = 5, dimes = 0, nickels = 1, pennies = 2 value = 132 Subtracting $0.41 Change due: mb2: quarters = 3, dimes = 1, nickels = 1, pennies = 1 value = 91
Explanation / Answer
Below is your progra. I have only one issue here... Adter adding mb1 to mb0, I cannot make mb1 0 from mb0 class. So I have to make little change in Main method..
This is because in java, object is passed by value always to the method.
mb0.add(mb1) can only affect the values of mb0 not mb1.
Please let me know if I can make changes to main method. I'll update the answer.,
public class MoneyBag
{
private int quarters;
private int dimes;
private int nickles;
private int pennies;
private double total;
public MoneyBag()
{
quarters = 0;
dimes = 0;
nickles = 0;
pennies = 0;
getValue();
}
public MoneyBag(int q, int d, int n, int p)
{
quarters = q;
dimes = d;
nickles = n;
pennies = p;
getValue();
}
public int getValue()
{
total = ((quarters * 25) + (dimes * 10) + (nickles * 5) + (pennies * 1));
return (int) total;
}
public void add(int q, int d, int n, int p)
{
quarters += q;
dimes += d;
nickles += n;
pennies += p;
getValue();
}
public void add(MoneyBag other)
{
this.quarters += other.quarters;
this.dimes += other.dimes;
this.nickles += other.nickles;
this.pennies += other.pennies;
getValue();
}
public MoneyBag subtract(int penniesCount)
{
double amount = 0.01 * penniesCount;
int quarters, dimes, nickles, pennies;
quarters = (int) (amount / 0.25);
double rem = (amount % 0.25);
dimes = (int) (rem / 0.10);
rem = (rem % 0.10);
nickles = (int) (rem / 0.05);
rem = (rem % 0.05);
rem = (rem / 0.01);
pennies = (int) rem;
if (this.total >= penniesCount)
{
this.quarters -= quarters;
this.dimes -= dimes;
this.nickles -= nickles;
this.pennies -= pennies;
getValue();
} else {
System.out.println("Insufficient funds - purchase denied.");
}
return this;
}
public int compare(MoneyBag Value)
{
if (this.total > Value.total) {
System.out.print("IS GREATER THAN");
return 1;
}
if (this.total == Value.total) {
System.out.print("IS EQUAL TO");
return 0;
}
System.out.print("IS LESS THAN");
return -1;
}
public void printContents()
{
System.out.println("Quarters: " + this.quarters + ", Dimes: " + this.dimes + ", Nicels: " + this.nickles
+ ",Pennies: " + this.pennies);
}
}
Output
mb0:
Quarters: 0, Dimes: 0, Nicels: 0,Pennies: 0
value = 0
mb1:
Quarters: 1, Dimes: 2, Nicels: 3,Pennies: 4
value = 64
adding $0.50 to mb0
mb0:
Quarters: 2, Dimes: 0, Nicels: 0,Pennies: 0
value = 50
Comparing mb0 and mb1: IS LESS THAN
Adding $0.14 to mb0
mb0:
Quarters: 2, Dimes: 0, Nicels: 2,Pennies: 4
value = 64
Comparing mb0 and mb1: IS EQUAL TO
Adding to mb0
mb0:
Quarters: 2, Dimes: 0, Nicels: 2,Pennies: 5
value = 65
Comparing mb0 and mb1: IS GREATER THAN
Adding contents of mb1 into mb0
mb0:
Quarters: 3, Dimes: 2, Nicels: 5,Pennies: 9
value = 129
mb1:
Quarters: 1, Dimes: 2, Nicels: 3,Pennies: 4
value = 64
Creating a new MoneyBag
mb2:
Quarters: 5, Dimes: 4, Nicels: 3,Pennies: 2
value = 182
Subtracting $5.00
Insufficient funds - purchase denied.
mb2:
Quarters: 5, Dimes: 4, Nicels: 3,Pennies: 2
value = 182
Oops, let's try $0.50
Change due:
mb2:
Quarters: 3, Dimes: 4, Nicels: 3,Pennies: 2
value = 132
Subtracting $0.41
Change due:
mb2:
Quarters: 2, Dimes: 3, Nicels: 2,Pennies: 1
value = 91
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.