| CIS 210 Home Page | Last updated 2007/09/23 13:24:48 |
This problem asks you to code a simple program that calculates the amount of change due. The change is to be shown as the number of bills and coins.
This problem is a simple simulation of such activity in a vending machine, concentrating on the calculation aspect. We should be able to write such a simulation in Java, using very simple constructs (arithmetic, assignment, and print statements).
Here are the specific requirements of the program:
An example of the use of this program is:
C:> java MakeChange Enter the cash received (in cents): 500 Enter the amount of the purchase: 212 The total change due is 288 cents: 2 dollar bills 3 quarters 1 dimes 0 nickels 3 pennies
(We are executing the program at the command prompt. Everything we type in is shown in italics, and the rest is what the program prints back to us.)
Since this is one of our first programs, we'll keep everything very simple and will not worry about checking for any error conditions. (E.g., if the cash received is less than the amount of the purchase.) Also, the program only needs to deal with one dollar bills, and does not concern itself with larger bills, dollar coins, or half dollar coins.
One more thing - we will enter amounts as whole numbers, i.e., the total number of cents. So if we want to simulate putting two dollars into the machine, we would enter "200" as the cash received. Likewise, if the amount of the purchase is $1.23, we would enter this as 123.
For example, the first few steps might be:
Let's start with a class and its main method:
public class MakeChange {
public static void main (String[] args) {
}
}
The first step is to get some input values. In Java 1.5, input
is most easily obtained using the Scanner class.
You can create a Scanner object associated with standard input and
read various values - such as integers.
We will go over more of the details of Scanner at the beginning of the second
week, but the code snippets below will give you what you need to do this assignment.
We'll use the System.out.print to produce the prompt we need to ask the user
to type in something:
/*
* Calculate change due:
* The user is prompted for cash received
* The user is prompted for the amount of the purchase
* The program shows the change due as number of
* dollar bills and coins.
* Note that amounts are entered as cents, i.e., as
* a whole integer.
* No error checking is done (e.g., if the amount received
* is less than the purchase).
* The coins used are quarters, dimes, nickels, and pennies.
*/
import java.util.*; // For Scanner class
public class MakeChange {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
// Prompt the user for the cash received
System.out.print("Enter the cash received (in cents): ");
int received = input.nextInt();
}
}
Notice that we have added an import statement for the Java Scanner class. Once this Scanner object is created, we may use its nextInt method to get an integer from the user. We have also added comments to document what our code is supposed to do.
At this point we have a simple program that prompts the user and reads an input that is expected to be a number, and stores the number in a variable named received. To check that everything is working, we could add a debugging line to print out this variable's value. (This should become common practice to print out values so that we can see that the values in our program are as expected - just don't forget to comment out or remove your debugging code before turning in your final solution.)
Similar code could be added to get the purchase amount.
// Prompt the user for the amount of the purchase
System.out.print("Enter the amount of the purchase: ");
int purchase = input.nextInt();
For the rest of the steps of the program, you will want to think about
what variables you might need and choose good meaningful names for them.
For example, you might want to have a variable named change
that is the amount of change to be returned, and
you will need to code statements to perform some arithmetic,
like calculating the value for your change variable:
int change = received - purchase;
System.out.println("The total change due is " + change + " cents:");
You will also need to figure out what variables and statements are
needed to determine the number of bills and coins, and the
sequence of those statements. And of course, you will need to
print out the values so that your program produces the output
in the format shown in the example.