AP Computer Science --- Haas --- PiggyBank

Below are two classes. One defines a PiggyBank class, and the other class is a tester. Copy the two classes into BlueJ.

  1. The method definitions in the PiggyBank class are not complete! Finish coding the incomplete methods.
  2. Add a method addPennies which adds a given number of pennies to the PiggyBank.
  3. Add a method getDollars which returns the value in the bank in whole dollars.
  4. Add a method printContents which prints the total contents of the PiggyBank: numbers of pennies, nickles, dimes and quarters.
  5. Add a method fewestCoins which changes the contents of the PiggyBank to the fewest number of coins without changing the total value.
    Example:
    If before fewestCoins is called: pennies=15 nickles=2 dimes=3 quarters=1
    then after fewestCoins is called: pennies=0 nickles=1 dimes=0 quarters=3
  6. Add a constructor which allows the user to create a new PiggyBank with an initial number of pennies, nickles, dimes, and quarters.
  7. Change the tester class to test all of the above enhancements.

/*******************************************************
 *   >>>>>>>>>>>>>>> PiggyBank Class <<<<<<<<<<<<<<<<
 *  Computes the total value of a collection of coins.
********************************************************/
public class PiggyBank
{
   private static final double NICKEL_VALUE = 0.05;
   private static final double DIME_VALUE = 0.1;
   private static final double QUARTER_VALUE = 0.25;

   private int nickels;
   private int dimes;
   private int quarters;
   
   /**
   *   Constructs an empty PiggyBank.
   */
   public PiggyBank()
   {
      nickels = 0;
      dimes = 0;
      quarters = 0;
   }

   /**
   *   Add nickels
   *   @param count the number of nickels to add
   */
   public void addNickels(int count)
   {
      // >>> complete the code to add to nickels <<<
   }

   /**
   *   Add dimes
   *   @param count the number of dimes to add
   */
   public void addDimes(int count)
   {
      // >>> complete the code to add to dimes <<<
   }

   /**
   *  Add quarters
   *  @param count the number of quarters to add
   */
   public void addQuarters(int count)
   {
      // >>> complete the code to add to quarters <<<
   }

   /**
   *   Get the total value of the coins in the purse.
   *   @return the sum of all coin values
   */
   public double getTotal()
   {
      // >>> complete the code: returns the total value of the piggy bank <<<
   }
}


/******************************************************
**         >>>>>>>>> PiggyBankTester <<<<<<<<<<<
*******************************************************/
import java.util.*;

public class PiggyBankTester
{
   public static void main(String[] args)
   {
      PiggyBank myBank = new PiggyBank();
      myBank.addNickels(5);
      myBank.addDimes(3);
      myBank.addQuarters(2);

      double totalValue = myBank.getTotal();
      System.out.println("The total is " + totalValue);
      
   }
}