Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

need my testcode to work and output the exact result below i need to add MoneyBa

ID: 3720099 • Letter: N

Question

need my testcode to work and output the exact result below i need to add MoneyBag in my test code PF1/lab9 ======testcode============ 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

Hi,
here is the class you need with comments

public class MoneyBag {

//defining the money bag variables

private int quarters;

private int dimes;

private int nickels;

private int pennies;

public MoneyBag(int quarters, int dimes, int nickels, int pennies) {

//constructor with fields

this.quarters = quarters;

this.dimes = dimes;

this.nickels = nickels;

this.pennies = pennies;

}

public MoneyBag() {//default constructor

this.quarters = 0;

this.dimes = 0;

this.nickels = 0;

this.pennies = 0;

}

public void printContents() {//printing the money bag

System.out.println("quarters = "+this.quarters+", dimes = "+this.dimes+ ", nickels = "+this.nickels+", pennies = "+this.pennies);

}

public int getValue() {//caclulating the value of bag

return this.quarters*25+this.dimes*10+this.nickels*5+this.pennies;

}

public void add(MoneyBag mb1) {//adding another money bag

this.quarters+=mb1.quarters;

this.dimes+=mb1.dimes;

this.nickels+=mb1.nickels;

this.pennies+=mb1.pennies;

}

public void compare(MoneyBag mb1) {//comparing with another bag

if(this.getValue()<mb1.getValue())

{

System.out.println("IS LESS THAN");

}

else if(this.getValue()>mb1.getValue())

{

System.out.println("IS GREATER THAN");

}

else

{

System.out.println(" IS EQUAL TO");

}

}

public void add(int i, int j, int k, int l) {//adding change directly

this.quarters+=i;

this.dimes+=j;

this.nickels+=k;

this.pennies+=l;

}

public MoneyBag subtract(int i) {//subtracting from the bag

if(this.getValue()<i)

{

System.out.println(" Insufficient funds - purchase denied");

return this;

}

  

this.quarters-=i/25;

i-=i/25 ;

this.dimes-=i/10;

i-=i/10;

this.nickels-=i/5;

i-=i/5;

this.pennies-=i;

return this;

}

}


Thumbs up if this was helpful, otherwise let me know in comments