CMSC 230 Summer, 2003

The documentation for G&T P-2.3 programming project (partial):

Description

Goodrich and Tamassia, Chapter 2, problem P-2.3
A program that will implement a change calculator
By Nicholas Duchon
February - July, 2003  - updated Jun 20, 2017
CMSC 230, section 6981
Instructor: Nicholas Duchon

The original problem statement: Write a Java program that can "make change". Your program should take two number as input, one that is a monetary amount charged and the other that is a monetary amount given. It should then return the number of each kind of bill and coin to give back as change for the difference between the amount given and amount charged. The values assigned to the bills and coins can be based on the monetary system of any current or former government. Try to design your program so that it returns the fewest number of bills and coins as possible.

Additional requirements presented in another statement of the problem (in a previous semester, Spring, 2003):

  1. Use a basic unit, say dracma, and allow a finite number of additional fractions and multiples. For example, (3/1, 5/1, 1/1, 1/100, 1/360, 90/360).
  2. (SUGGESTION, you may choose another approach if you wish.) This would be a good problem for a Rational class.
  3. The input should also specify the names of the units.
  4. Read the data from stdin as an application.
  5. As an Applet:?
    1. Use a TextArea for input
    2. Use a TextArea for output
  6. (OPTIONAL) Create a calculator on the fly, based on the input in the TextArea - one button for each unit.
  7. (OPTIONAL) Use JColorChooser to specify the color the monetary units.

This project is designed to implement the calculator as an applet and provide a demonstration for the Collections class.

Design

Classes:

The first two classes are from the Sun One Studio to help control the layout of the panels. These classes implement the LayoutManager2 interface and allow absolute coordinates in pixels for object positioning. See the documentation that comes with the source code for these classes for more information.

As a general comment, you should note the following features of this code:

The Rational class is a support class implementing rational numbers as the quotient of two int's. The instances of the class are always reduced as part of any operation. The class provides for instantiating rational numbers, adding, subtracting, multiplying and dividing them. Also a toString method is provided, as well as the methods the Comparable interface needs. This last is useful in conjuction with standard Java algorithms such as Collections.sort(). The class also has a static readRational method and a main method to test this class.

The Denomination class is the basic class implementing the requirements of this problem. Denomination extends Rational, with the additional fields name, abbreviation. Three additional fields are used in the GUI interfaces: color and denButton used in the keypad implementation of the calculator, and a reference to the output text area for posting messages from this class. The output fields are the tendered amount and due amount text fields of the keypad calculator, depending on the mode of the calculator. The arithmetic methods of this class are inherited from the Rational class, so this class has no arithmetic explicitly listed in its methods. This class includes various get and set methods and a toString method.

When the Denomination class is instantiated:

The JButton listener is set to call the denButtonHandler method. The event handler either adds the value of the associated instance to the appropriate tendered or due field (left mouse button) or presents the user with a GUI configuration control (an associated instance of the DenominationProperties class).

The ChangeCalculator class extends JApplet. It uses an ArrayList to hold the denominations that are defined by the user, and a lot of instances of GUI items related to the main frame, the keypad frame and the denomination properties frames.

Adding keys to the keypad is automatically formatted according to the GridLayout (5,5,2,2). The important number is the first 5, meaning 5 rows. The GridLayout automatically calculates the desired number of columns and balances the buttons so that each column has about the same number of rows.

Method
Frame
Comments
ChangeCalculator
main
constructor - calls initComponents
initComponents
main
creates all GUI components and configures listeners for static components
(everything except the keypad buttons)
helpButtonActionPerformed
main
prints help in right hand text area (RHTA)
loadButtonPressed
main
reads the data in left hand text area (LHTA) and interprets the data as denomination specifications, using parseDenomination methos
computeButtonPressed
main
calculates change and reports the result in the RHTA
keypadCalculatorButtonClicked
main
makes the keypad calculator interface visible
parseDenomination
main
reads the LHTA and creates instances of Denomination
toValueString
main
presents a rational number as the corresponding set of denominations
calcTendButtonClicked
keypad
activates the amount tendered text field
calcDueButtonClicked
keypad
activates the amount due text field
calcCalcChangeButtonClicked
keypad
computes the change and shows the result in the lower right hand text area
ChangeColorHandler
denomination
responds to the "Change Color" button pressed on the denomination modifier frame - this method only reports that the button has been pressed by sending a message to standard output

The following methods, which seem to be the guts of the program, are notable for their shortness - reflecting the appropriateness of the underlying classes:

On the other hand, the loadButtonPressed is a rather long method, doing the following:

User's Guide

The program is located in the following directory:
http://sandsduchon.org/duchon/www/cs230/p2.3
which can be accessed through the web using the following url:
http://sandsduchon.org/duchon
and following the link to p2.3.

This program is an applet.
The program was developed in a Windows (XP and 2000) environment using: java version "1.4.0_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0_03-b04)
Java HotSpot(TM) Client VM (build 1.4.0_03-b04, mixed mode)

The applet has a "Help" button to get you started.


      

Test Plan

Add the following list of denominations, one at a time, to see the effect on the keypad layout. Do a "Load Denominations" after each line is added, followed by the "Keypad Calculator" button.

1 1 a a
2 1 b b
3 1 c c
4 1 d d
5 1 e e
6 1 f f
7 1 g g
8 1 h h
9 1 i i
10 1 j j
11 1 k k

Obviously this is not a very interesting set of denominations, but it is interesting to see how the GridLayout works.

Assessment

  1. The project currently does not actually produce the minimum number of bills. This is a hard problem. Consider a currency with the following denominations and change of 12, 18, etc.:

    This turns out to really hard. For some discussions, see the following links:

  2. It looks like the ChangeCalculator class should be broken down into at least three classes, one for each frame type.

Lessons Learned