Programming Assignment #3
For this assignment, you will be creating a class named PiggyBank
. The PiggyBank
class should contain int
instance variables for pennies
, nickels
, dimes
, and quarters
which are initially set to 0. It should contain void
instance (command) methods that model adding to or removing coins from the piggy bank that increment and decrement each of these instance variables (e.g., addPenny()
, removePenny()
, etc.).
Your PiggyBank
class should also contain int
instance (query) methods that return the number of each type of coin in the piggy bank. (e.g., getPennies()
, etc.). It should also contain two int
instance (query) methods, getDollars()
and getCents()
, that return the total number of dollars and cents, respectively, in the piggy bank. For instance, if the number of pennies is 247, the number of nickels is 31, the number of dimes is 6, and the number of quarters is 2 then getDollars()
should return 5 and getCents()
should return 12.
PiggyBank |
-int pennies
-int nickels -int dimes -int quarters |
+void addPenny()
+void addNickel() +void addDime() +void addQuarter() +void removePenny() +void removeNickel() +void removeDime() +void removeQuarter() +int getPennies() +int getNickels() +int getDimes() +int getQuarters() +int getDollars() +int getCents() |
Once you have completed the PiggyBank
class, perform the following actions in the Interactions panel, saving the interaction in a file named test.txt
:
- Create an instance of
PiggyBank
namedpig
. - Add 3 pennies, 4 nickels, 2 dimes, and 5 quarters to the piggy bank.
- Display the number of dollars and cents in the piggy bank.
- Remove 2 pennies, 1 nickel, 2 dimes, and 2 quarters to the piggy bank.
- Display the number of dollars and cents in the piggy bank.
- Add 5 pennies, 2 nickels, 1 dime, and 6 quarters to the piggy bank.
- Display the number of dollars and cents in the piggy bank.
For this assignment, you should put the PiggyBank.java
and test.txt
files in a subfolder named assignment3
in your folder of the ClassStore
.
Due Date: This assignment must be in the ClassStore
by 11:59pm on Tuesday, September 14th.