Home / Essays / CPS 150 Assignment 3 The Change Maker

CPS 150 Assignment 3 The Change Maker

CPS 150 Assignment 3 The Change Maker
Objectives:
a) arithmetic using integer and real (floating point) values,
b) use of integer division and integer remainder (% in Java) operators,
c) data type conversions (type casting),
d) decision making using if statements,
e) using methods to modularize programs, arguments/parameters for communication among methods (class level
variables are not allowed in this assignment, except the Scanner attached to System.in).
f) using some String class methods .charAt() .substring() .length()
g) using Math.round.
Note: The How To 2.1 (pages 56 – 58) in the textbook describes a problem and its solution similar in nature to this
assignment but you should use techniques described here and not what is given in the book.
The scenario
Your friend Joe the Cashier works the graveyard shift at the local convenience store. His cash register tells him how
much change he needs to return to his customer, but he is having trouble figuring out what bills and coins to give to the
customer to make up the change due. You have agreed to write a Change Maker program for him. The requirements of
this program are discussed below.
Input Data
There is only one item of input data: The change due to the customer. This will be a dollars and cents figure, expressed
as a decimal fraction, such as 37.67. This will be stored in a variable of type double.
Output
The output will contain the number of each type of bill and coin to be given to the customer. Some of these numbers
could be 0 and those would be omitted from the output. Here is a sample interaction between the program and the
user:
Assumptions
We assume that the register contains an ample supply of bills ($20, $10, $5, $1) and coins (quarters, dimes, nickels,
pennies) so we can always make up the change due using fewest number of bills and coins. We further assume that $20
is the largest denomination that we will use.
Page 2 of 3
Strategy to solve this problem
First we need the whole numbers of dollars and cents from the real number (that may contain a decimal fraction)
representing the change due. So from 37.67 we need to get 37 and 67. A real value can be assigned to an integer
variable (using a cast operator) to extract the whole number part of the real value. The difference between the real
value and the whole number part can be multiplied by 100 (round the difference to nearest int) to get the total number
of cents. Now we work with the dollars and the cents figures separately. We work from larger to smaller denominations.
We can use a variable to keep track of how many dollars (cents) has not yet been accounted for by the bills (coins) the
program has decided to use for change making. I give an example of how the calculation could proceed.
1. Initially, we have 37 dollars to make change for.
2. Decide how many $20 bills we need. 37/20 = 1, so we need 1 $20 bill. 37%20 = 17, so we still need to make
change for 17 dollars.
3. Decide how many $10 bills we need. 17/10 = 1, so we need 1 $10 bill. 17%10 = 7, so we still need to make
change for 7 dollars.
4. Decide how many $5 bills we need. 7/5 = 1, so we need 1 $5 bill. 7%5 = 2, so we still need to make change for 2
dollars.
5. The number of dollars left over at this stage is the number of $1 bills we need.
The numbers of coins of various denominations can be calculated in a similar way.
Nicknames for the bills
The bills are often referred to by the names of the Historical Persons whose pictures appear on them. So a $20 bill is
called a Jackson, $10 — Hamilton, $5 – Lincoln, $1 – Washington.
Singular and plural forms of the bill and coin names
The plural form of all the coin/bill names (except “Penny” used for 1¢ coin) can be formed by just adding an ‘s’ to the
singular form. But the coin name “Penny”, has the plural “Pennies”. This is handled by checking if the last character in
the name is ‘y’. If not, just concatenate an ‘s’ to the end of the singular name to form the plural. Otherwise, form a
substring by removing the ‘y’ and add “ies” to that substring to form the plural. We will handle this by defining a method
that gives us the plural form from the singular. The output should use the plural form of the bill/coin names when
appropriate.
Program organization
The program is to be written using several methods in addition to the “main” method.
main –This method prints the sign-on message and describes its purpose. It then prompts the user to enter the change
due as a double value and accepts the input into a double variable (no validation necessary). It then calculates
two integer values (dollars and cents in the change due). It then calls the methods showDollars and showCents
with appropriate arguments. Finally it prints the sign-off message.
void showDollars(int dollars) – This method accepts an int parameter representing the whole number of dollars in the
change due. It calculates the numbers of bills of various denominations to be returned and call the last method
to print the number and the name for each denomination of bills used.
void showCents(int cents) – This method is similar to the method above but it deals with the coins to be returned, so it
receives a parameter representing the cents part of the change due.
void showNumberAndName(int number, String name) – This method receives a number of a particular bill/coin and the
name of that bill/coin as part of returned change. If the number is 0, this method returns without producing any
output. If the number is 1 then it prints 1 followed by the name. If the number is > 1 then it prints the number
followed by the plural form of the name.
String plural(String name) – This method returns the parameter with an ‘s’ added to the end, when the parameter does
not end with ‘y’. Otherwise it creates the plural form as described before and returns that.
Structure chart
Page 3 of 3
This is a chart that shows which methods call which other method(s). This chart only includes the methods you need to
write. The methods used from the Java system library are not shown here.
main
showDollars showCents getDouble
showNumberAndName
|
plural
Data types for variables: You should have only one double variable in your program to accept the user input. Once that
value is used to calculate whole numbers of dollars and cents, all further calculations should use int variables and
integer arithmetic only. The methods will need to declare local variables for their use.
How to submit your work
Submit a Word document containing the source code and sample output as before. Please use the Submission template
Word document (isidore Resources) provided to add your source code and output screen shot in appropriate pages.
Another screen shot from the completed program
An issue with Math.round() in Java
Math.round() has two versions: a) a version that accepts a float argument and returns an int value, b) another version
that accepts a double argument and returns a long value. Since we mostly use types int and double, we will need to
typecast the return value from Math.round(). Use the following as a guide:
int-variable-name = (int)Math.round( double-expression );

WPMessenger