Develop a test program called coincountertest


Assignment:

Develop a test program called CoinCounterTest to test the CoinCounter class.

1. In the test program do the following:

1. Write client code (in the CoinCounterTest.java file) to create a CoinCounter object called counter.

2. Write client code to call the print method of counter.

3. Write client code to deposit 7 quarters, 3 dimes, 10 nickels, and 17 pennies in counter. Output the new status of counter using the toString method.

4. Write client code to put the value of the coins deposited so far into a double variable called totalValue. Then print totalValue with an identifying label.

5. Write client code to deposit an additional 11 quarters, 8 dimes, 0 nickels, and 100 pennies in counter. Again find out and print the value of the total deposits.

6. Write client code to create an additional CoinCounter object called bankCounter. Make deposits of 30 quarters, 90 dimes, 100 nickels, and 1000 pennies to bankCounter.

7. Write client code to print bankCounter using the print method.

8. Write client code to clear bankCounter and print bankCounter using the toString
method.

2: Response the following questions:

1. What type of value does getTotalDeposit return?

2. Draw a picture of the object variable counter and the object after the first deposit (7 quarters, 3 dimes, 10 nickels, and 17 pennies).

3. Suppose you created a CoinCointer object called count2 and then called count2.deposit(25, 8, 5, 10). What would count2.getTotalDeposit() return?

public class CoinCounter {
// constants giving the coin values in $
static final double QUARTER_VALUE = 0.25;
static final double DIME_VALUE = 0.10; // <==<1>
static final double NICKEL_VALUE = 0.05; // <==<1>
static final double PENNY_VALUE = 0.01; // <==<1> // <==

// fields contain the object's internal state
private int myQuarters; // total quarters
private int myDimes; // total dimes <==<1>
private int myNickels; // total nickel <==<1>
private int myPennies; // total pennies <==<1>

// constructor (initialize object on creation)
public CoinCounter( )
{
myQuarters = 0; // no quarters
myDimes = 0; // no dimes <==<1>
myNickels = 0; // no nickels <==<1>
myPennies = 0; // no pennies <==<1>
}

// accessors (methods don't change object)
public double getTotalDeposit( )
{
// <4>return total value of coins deposited in $
return myQuarters*QUARTER_VALUE +
myDimes*DIME_VALUE +
myNickels*NICKEL_VALUE +
myPennies*PENNY_VALUE; // <==
}

public void print( )
{
// <2>output the coin values in a nice format
System.out.println("Quarter value =\t" + myQuarters*QUARTER_VALUE);
System.out.println("Dime value = \t" + myDimes*DIME_VALUE);
System.out.println("Nickel value = \t" + myNickels*NICKEL_VALUE);
System.out.println("Penny value = \t" + myPennies*PENNY_VALUE);
/*System.out.println("Total deposit = " + (myQuarters*QUARTER_VALUE +
myDimes*DIME_VALUE + myNickels*NICKEL_VALUE +
myPennies*PENNY_VALUE ) );
*/
System.out.println("Total deposit = " + getTotalDeposit());
System.out.println();
}

// <6> toString method
public String toString()
{
// <== toString method
return "Quarter value =\t" + myQuarters*QUARTER_VALUE + "\n" +
"Dime value = \t" + myDimes*DIME_VALUE + "\n" +
"Nickel value = \t" + myNickels*NICKEL_VALUE + "\n" +
"Penny value = \t" + myPennies*PENNY_VALUE + "\n\n";
}

// modifiers (methods change object's state)
public void deposit (int quarters, int dimes,
int nickels, int pennies)
{
// <3>update the totals to reflect this deposit
myQuarters += quarters;
myDimes += dimes;
myNickels += nickels;
myPennies += pennies;
}

// <6> clear method
public void clear( ) {
myQuarters = 0;
myDimes = 0;
myNickels = 0;
myPennies = 0;
}
}

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Develop a test program called coincountertest
Reference No:- TGS01935248

Now Priced at $25 (50% Discount)

Recommended (96%)

Rated (4.8/5)