Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

**I will upvote as soon as possible!** Create the following additional methods in the ChangeJar class: public static void Mutate(Boolean on) A method that turns

**I will upvote as soon as possible!**

Create the following additional methods in the ChangeJar class:

public static void Mutate(Boolean on) A method that turns off (false) and on (true) any subtract/add methods in ChangeJar based on the value (true,false) of the variable on. In other words, when on is false, this prevents any subtract/add method from changing (mutate) the state of the this object as it relates to the amount in the ChangeJar.

ChangeJar:

public class ChangeJar { private int quarters; private int dimes; private int nickels; private int pennies;

public ChangeJar() { this.quarters = quarters; this.dimes = dimes; this.nickels = nickels; this.pennies = pennies; }

/** * @param quarters * @param dimes * @param nickels * @param pennies */ public ChangeJar(String amount) { try { double amt = Double.parseDouble(amount); int cents = (int) (amt * 100);

this.quarters = cents / 25; cents %= 25; this.dimes = cents / 10; cents %= 25; this.nickels = cents / 5; cents %= 5; this.pennies = cents; } catch (Exception e) { throw new IllegalArgumentException("** Invalid.Must be a decimal value **"); }

}

/** * @return the quarters */ public int getQuarters() { return quarters; }

/** * @param quarters * the quarters to set */ public void setQuarters(int quarters) { this.quarters = quarters; }

/** * @return the dimes */ public int getDimes() { return dimes; }

/** * @param dimes * the dimes to set */ public void setDimes(int dimes) { this.dimes = dimes; }

/** * @return the nickels */ public int getNickels() { return nickels; }

/** * @param nickels * the nickels to set */ public void setNickels(int nickels) { this.nickels = nickels; }

/** * @return the pennies */ public int getPennies() { return pennies; }

/** * @param pennies * the pennies to set */ public void setPennies(int pennies) { this.pennies = pennies; }

public boolean equals(Object other) { ChangeJar cj = (ChangeJar) other; int othercents = cj.getQuarters() * 25 + cj.getDimes() + cj.getNickels() + cj.getPennies(); int cents = getQuarters() * 25 + getDimes() + getNickels() + getPennies(); if (othercents == cents) { return true; } else { return false; } }

public static boolean equals(ChangeJar jar1, ChangeJar jar2) { return (jar1.equals(jar2)); }

public int compareTo(ChangeJar other) { int cents = getQuarters() * 25 + getDimes() + getNickels() + getPennies(); int othercents = other.getQuarters() * 25 + other.getDimes() + other.getNickels() + other.getPennies();

if (cents > othercents) return 1; else if (cents < othercents) return -1; else return 0; }

public void subtract(int quarters, int dimes, int nickels, int pennies) { if (this.quarters >= quarters) this.quarters -= quarters; else this.quarters = 0;

if (this.dimes >= dimes) this.dimes -= dimes; else this.dimes = 0;

if (this.nickels >= nickels) this.nickels -= nickels; else this.nickels = 0;

if (this.pennies >= pennies) this.pennies -= pennies; else this.pennies = 0;

}

public void subtract(ChangeJar other) { if (this.quarters >= other.getQuarters()) this.quarters -= other.getQuarters(); else this.quarters = 0;

if (this.dimes >= other.getDimes()) this.dimes -= other.getDimes(); else this.dimes = 0;

if (this.nickels >= other.getNickels()) this.nickels -= other.getNickels(); else this.nickels = 0;

if (this.pennies >= other.getPennies()) this.pennies -= other.getPennies(); else this.pennies = 0;

}

public void dec() { if (this.pennies >= 0) this.pennies -= pennies; else { int cents = getQuarters() * 25 + getDimes() + getNickels() + getPennies();

this.quarters = cents / 25; cents %= 25; this.dimes = cents / 10; cents %= 25; this.nickels = cents / 5; cents %= 5; this.pennies = cents;

} }

public void add(int quarters, int dimes, int nickels, int pennies) {

this.quarters += quarters;

this.dimes += dimes;

this.nickels += nickels;

this.pennies += pennies;

}

public void add(ChangeJar other) {

this.quarters += other.getQuarters();

this.dimes += other.getDimes();

this.nickels += other.getNickels();

this.pennies += other.getPennies();

}

public void inc() { if (this.pennies < 98) this.pennies += 1; else { int cents = getQuarters() * 25 + getDimes() + getNickels() + getPennies();

cents++;

this.quarters = cents / 25; cents %= 25; this.dimes = cents / 10; cents %= 25; this.nickels = cents / 5; cents %= 5; this.pennies = cents;

} }

public String toString() { String str = null; if (this.pennies == 1) str = quarters + " quarters," + dimes + " dimes," + nickels + " nickels," + pennies + " penny"; else str = quarters + " quarters," + dimes + " dimes," + nickels + " nickels," + pennies + " pennies";

return str;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Application Development And Design

Authors: Michael V. Mannino

1st Edition

0072463678, 978-0072463675

More Books

Students also viewed these Databases questions

Question

Explain the meaning of the variable overhead efficiency variance?

Answered: 1 week ago