Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*I will upvote instantly!* Instructions: A start to a main program has been provided and your task is to add Java statements that would test

*I will upvote instantly!*

Instructions: A start to a main program has been provided and your task is to add Java statements that would test each method separately and completely.

Main method public static void main (String[] args) {

ChangeJar s = new ChangeJar("2.82");

System.out.println("2.82 Amount: " + s);

s = new ChangeJar("8");

System.out.println("8 Amount: " + s);

s = new ChangeJar(".28");

System.out.println(".28 Amount: " + s);

ChangeJar s1 = new ChangeJar();

System.out.println("0 Amount: " + s1);

s1.add(1,1,1,100); System.out.println("1,1,1,100 Amount: " + s1);

ChangeJar s2 = new ChangeJar(41.99);

s2.add(0,0,0,99); for (int i = 0; i

System.out.println("amount: " + s2);

// ADD more test cases here

}

image text in transcribed

image text in transcribed

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

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

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;

}

}

Implement the following methods and properties in Change Jar class. For properties, you will need four instance variables: quarters (integer), dimes (integer), nickels (integer), pennies (integer). There are no dollars bills (or dollar coins) in this Change Jar. For methods, you will need to implement the following (include any setters or getters that are needed). Unless otherwise stated, you can assume the input has no errors for step 2 (i.e., a valid set of numbers) contained within (later steps this does not apply). public ChangeJar() Default constructor that sets the Change Jar to zero. public ChangeJar (double amount) A constructor that initializes the instance variables with the provided value converted to quarters, dimes, nickels, and pennies. For example, if amount was 1.34 then you would have 5 quarters, 1 nickel, 4 pennies. Page public Changejar (ChangeJar other) A constructor that initializes the instance variables with the other ChangeJar parameter. public ChangeJar (String amount) A constructor that accepts a string as a parameter with the provided value converted to quarters, dimes, nickels, and pennies. For example, if amount was 1.34" then you would have 5 quarters, 1 nickel, 4 pennies. More examples: o 1.3 is 5 quarters, 1 nickel o 1.34 is 5 quarters, 1 nickel, 4 pennies O "0.34 is 34 cents; 1 quarter, 1 nickel, 4 pennies O "0.04 is 4 cents o "500. is not allowed, instead "500.0 is used. public boolean equals (Object other) A method that returns true if this Change Jar object is exactly the same (in terms of the amount in the Change Jar) as the other object (Note: you must cast the other object as a Change Jar object). public static boolean equals (ChangeJar jari, ChangeJar jar2) A static method that returns true if Change Jar object jarl is exactly the same (in terms of amount in the Change Jar) as if Change Jar object jar2. public int compareTo (ChangeJar other) A method that returns 1 if this Change Jar object is greater than (based upon the total in the ChargeJar) the other Change Jar object; returns -1 if the "this Change Jar object is less than the other Change Jar; returns 0 if the this Change Jar object is equal to the other ChangeJar object. public static int compareTo (ChangeJar jari, Change Jar jar2) A method that returns 1 if Change Jar object jarl is greater (in terms of the amount in the Change Jar) than ChangeJar object jar2; returns -1 if the Change Jar object jarl is less than Change Jar jar2; returns 0 if the Change Jar object jarl is equal to ChangeJar object jar2. public void subtract(int quarters, int dimes, int nickels, int pennies) A method that subtracts the parameters from the this ChangeJar object. You may assume all of the parameter are positive. public void subtract (Changejar other) A method that subtracts Change Jar other to the this Change Jar object. (For step 2 there are no worries about errors) public void dec (). A method that decrements the this ChangeJar by 1 penny. public void add (int quarters, int dimes, int nickels, int pennies) A method that adds the parameters from the this ChangeJar object. You may assume all of the parameter are positive. public void add (Changejar other) A method that add ChangeJar other to the this Change Jar object. public void inc() A method that increments the this ChangeJar by 1 penny. public String toString() Method that returns a string that represents a ChangeJar with the following format: 10 quarters, 1 dime, 0 nickels, 1 penny. Be sure to use proper pluralization. For example, 1 penny or 2 pennies

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

Students also viewed these Databases questions