CS 1063 Lab 3: Use and
Construct User-Defined Objects
The CoinCounter Class
Objectives
- Demonstrate your ability to use a class given the public interface
- Construct and use Objects
Hand-in Requirements
All projects and laboratories will be submitted electronically through
webCT. Zip up your entire project folder to submit as the
source. (Right click on the project folder and follow the
SentTo link.) The project folder should include the
following:
- Answers to questions as a Word or plaintext document
- CoinCounterTest.java
- CoinCounter.java
Details
Grocery stores and banks have coin exchange machines that allow
customers to deposit unsorted coins and receive the equivalent amount
of cash in return. A coin exchange machine has two components: a
coin sorter that separates the coins by type and a coin counter, which
given the numbers of coins of each type, figures out the cash
equivalent. In this laboratory you will construct and use a
CoinCounter class to represent the second component of the
coin exchange machine. In order to do this you will develop a test
program called CoinCounterTest to test the
CoinCounter class.
Objects of type CoinCounter allow the user to make
deposits, get the total value of the money deposited, and print the
totals of the each of the coins along with the total value of the
deposits.
Public Interface for the CoinCounter Class
- public CoinCounter( )
Constructs and initializes the number of
coins of each type in the
machine.
The number of Quarters, Dimes, Nickels and Pennies will all be 0
- public double getTotalDeposit( )
Returns the total value of coins deposited in $
- public void print( )
Output the total
value of quarters, total value of dimes, etc., and the amount of the
total deposit in a nice format
- public String toString()
Returns the total value of quarters, total
value of dimes, etc., and the amount of the total deposit
in the form of a String
- public
void deposit (int quarters, int dimes, int nickels, int
pennies)
Update the totals to reflect this deposit.
- public void clear()
Resets the number of coins in CoinCounter to 0
The number of
quarters = 0, dimes = 0, nickels = 0 and pennies = 0
Setup:
- Folder name: CoinCounter
- Open a new project named CoinCounter.
- Save the project in the CoinCounter folder.
- New class: CoinCounterTest
public class CoinCounterTest {
public static void main(String[] args) {
}
}
Save the class in the coincounter folder.
Download the following file CoinCounter.java
and save it in your CoinCounter
folder.
Open CoinCounter.java in DrJava.
Save the project.
Part 1: In your test program do the following:
- Write client code (in the CoinCounterTest.java file) to
create
a CoinCounter object called counter.
- Write client code to call the print method of counter.
- 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.
- 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.
- 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.
- 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.
- Write client code to print bankCounter
using the print method.
- Write client code to clear bankCounter
and print bankCounter using the toString method.
Part 2: Answer the following questions
- What type of value does getTotalDeposit return?
- Draw a picture of the object variable counter and
the object after the first deposit (7 quarters, 3 dimes, 10 nickels, and
17 pennies).
- Suppose you created a CoinCointer object called count2
and then called count2.deposit(25, 8, 5, 10). What would
count2.getTotalDeposit() return?