Using if statements to clean up output
COSC 135 A, February 19
Tom Linton, Central College
In this activity, you will use if statements to improve code that calculates
the number of dollars, quarters, dimes, nickels, and pennies needed to make
change for a monetary amount provided by the user. Briefly, you will eliminate
output similar to "0 dimes" and change output from things like "1 nickels"
to the more appropriate "1 nickel".
- The source code for a Java application that correctly calculates the
number of dollars, quarters, dimes, nickels, and pennies for a given number
of cents, is located in the folder G:\Lintont\spring03\prog1\chapter6 (this
contains an entire BlueJ project that
uses the javabook
package). Grab a copy of that folder and copy it to your H:\ drive. Open
the project in BlueJ and edit the header comments to include your name(s).
You can also access just the source code file (on-line version only) by clicking
here.
- Look over the source code, to understand the logic of its design.
Using BlueJ, run the program and enter 200, 80, and 30 as inputs to see the
need for improving the output when certain values are either zero, or one.
- Use an if statement to fix things so that whenever numDollars
ends up being zero, the output doesn't mention dollars. For now, fix only
the part of the program related to numDollars being 0. For example,
if you run your updated code on the input 80, you should get:
You have 80 cents, which is equivalent to
3 quarters, 0 dimes, 1 nickels, and 0 pennies.
- Now, modify your code so that if numDollars ends up being
equal to 1, the output contains "1 dollar" rather than "1 dollars" (and
it still handles zero dollars correctly). Test this code on a few inputs
from 50 to 299 (you select which ones, but be sure to try values below 100,
between 100 and 200, and over 200) to ensure your code correctly handles
inputs that result in zero, one, or two dollars. Again, only worry about
the dollars, not the quarters etc. at this point. Try to use nested ifs
for this part (so your code should now contain "else if", rather than several
non-overlapping ifs). For example, if numDollars >= 2 is determined
first, you don't waste time re-checking whether or not numDollars ==
1, or numDollars == 0. (unless the test numDollars >=
2 failed). Similarly, if you first determine that numDollars ==
0 is true, you don't re-check whether or not numDollars == 1.
- Once you have dollars behaving properly, you should be able to mimic
the dollars code (copy, paste, and edit will be useful here) to ensure that
both dollars and quarters produce nice output. Update the output code for
quarters, so that you get nice output (in terms of just dollars and quarters)
for inputs like 16, 115, 222, 56, and 106.
- Clean up the code so that it properly handles dollars, quarters,
dimes, nickels, and pennies. Whenever the input is positive, you should
not have any zeros in your output, and every "1" should be followed by dollar,
quarter, dime, nickel, or penny (instaed of dollars, quarters, dimes, nickels,
or pennies respectively).
- Finally, modify your code so that an input of k, which is 0 or a
negative value, outputs: You have k cents, which requires no coins.
- Be sure the names of all persons with who you worked appears in the
header comment, and use the digital drop box on blackboard to hand in your
final source code file (MakeChange.java).