CS 161 Lab B - Calculating Change
Due Thurs Jan 24 at 11:59pm
Overview
In this lab you will write a simple program that acts like a basic cash register, reading in the price of the purchase and the cash provided in order to determine the amount and format of change to give. We will walk you through some of the steps to get started, but the programming will need to come from you!
This lab (and all subsequent labs) will be completed in pairs. Be sure to review the pair programming guidelines.
Objectives
- To practice using BlueJ to create programs
- To practice with variables, basic operations, and expressions
Necessary Files
You will not need any additional files for this lab.
Details
- First, read through the pair programming guidelines if you haven't already. You may also want to skim through assignment to get a general sense for what you'll be doing.
-
You'll need to make a new BlueJ project for this assignment. You can do this by opening BlueJ and select "Project > New Project" from the menu.
- If another project opens automatically when you open BlueJ, you can close it by selecting "Project > Close" from the menu. Sometimes just hitting the normal close button (the red x) will close the BlueJ program, rather than the project!
-
Create a new class. Call it
CashRegister
. Be sure that you have the name and capitalization correct! Double-click the class to open its editing window. - You'll need to make your class match the basic code template we talked about in class. Remove all of the code inside the class definition, and add in the main method. Also add a short description of the class to the class comment at the top, as well as your name. Once your done, your editing window should look something like this:
-
Now you're ready to begin writing your program! Remember to write your code inside the main method. Start by declaring and initializing variables to represent the number of bills and coins in the cash register (e.g.,
numberQuarters
. You should give it 25x $5 bills, 25x $1 bills, 40x quarters, 50x dimes, 40x nickels, and 50x pennies. What datatype should these variables be? - Declare and initialize two more variables: one that represents the cost of the purchase, and one that represents the amount paid by the customer. For example, the cost might be $1.61, and the customer might pay $5.00 dollars (note that you won't want to include the $ sign!)
-
Your task is to computer the amount of change that is due to the customer, and the number of dollars, quarters, dimes, nickels, and pennies that should be given back to the customer. Once you've calculated the numbers, print out the amount and form of change to give. Also subtract the number of bills and coins from the cash register's store, and print out the amount of money left over. So with the values listed above, your output should look something like:
Item cost: $1.61 Amount paid: $5.00 Change received: $3.39 (Give the customer: 0 fives, 3 ones, 1 quarters, 1 dimes, 0 nickels, 4 pennies) Left in cash register: 25 fives 22 ones 39 quarters 49 dimes 40 nickels 46 pennies
Note that the number of coins to give as change are all listed on one line, and the number of coins in the cash register are listed on separate lines (and are indended a few spaces). Try to match this formatting exactly!- Hint: You can use the DecimalFormat class (import java.text.DecimalFormat; see page 104) to limit values to 2 decimal places.
-
IMPORTANT HINT: because of how computers are made, doing math with doubles (e.g., subtracting the cost from the amount paid) is not always exact.
5.0 - 1.61
is3.3899999999999997
, not 3.39. This will cause problems when you try and give change--you may come up short a penny! In order to fix this, you can use a Java method calledMath.round()
. This method takes in a double as a parameter, and returns an int that is the value rounded to the nearest whole number. You'll also need to cast the result to an int using(int)
. The Math class is discussed in Chapter 3.7, and we'll talk about it tomorrow.
-
Once you've finished writing code that gives change, modify the program so that the user specifies the price of the item and the amount paid, instead of having those values hard-coded (that is, written directly in the source code). You will need to use a
Scanner
object to get input from the user. You can create this object with:Scanner scanner = new Scanner(System.in);
And you can get the number (in double precision) the user typed in with:
num1 = scanner.nextDouble();
Make sure to "prompt" the user (using a System.out.print() call) for what value they are providing.
- Remember to
import java.util.Scanner;
so you can use it!
- Remember to
- In the end, you should be able to run your main method from BlueJ, enter the cost of the purchase and the amount paid, and see the correct amount of change that should be provided!
Submitting
Upload the Java file of your CashRegister
class to the Lab B folder on the submission folder on hedwig. Make sure you upload your work to the correct folder! The lab is due at midnight on the day of the lab.
Grading
This assignment will be graded on the following criteria:
- Your program calculates and prints out the cost, amount paid, and amount of change to provide with proper formatting [15%]
- Your program calculates and prints out the format of the change (the number of fives, ones, quarters, dimes, nickels, and pennies). This should be printed on one line [35%]
- Your program prints out the number of bills and coints remaining in the register, on separate lines [25%]
- Your program reads gets the cost and amount paid from the user [25%]