Answered step by step
Verified Expert Solution
Question
1 Approved Answer
PF1 Lab9 - MoneyBag! For this lab, you will create a virtual MoneyBag that can be used to store change (no paper money). You will
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started