Problem 1.2

CIS 210 Home Page Last updated 2007/09/23 13:24:48

Problem 1.2: Making Change

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.

Problem Description

Imagine that you are programming a vending machine that makes change for its purchases. Certainly such a machine will need to figure out how much change to give when a purchase is made, but it will also need to figure out how to dispense the change. That is, the machine will need to determine exactly what bills and coins to give back as change.

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.

Getting Started - design on paper first

Before jumping into TextPad and trying to write code, think about the steps necessary to solve this problem and write down on paper a high level description of what you think your program should do. Specifying the steps your program takes at this general level results in a sort of pseudo code: a programmatic description of what your program does, but not at the precise level of a programming language.

For example, the first few steps might be:

  1. Prompt the user and get the amount they are giving
  2. Prompt the user and get the amount of the purchase
  3. Calculate the difference between the cash received and the purchase amount, and call this the change to be given
  4. Calculate the number of one dollar bills in the change
  5. ...
Writing Java code from your design Now that you have a design on paper, you can start to write the Java code. Remember that a Java program consists of class definitions, and a Java application begins execution with the main method of a class. For this first program, we are not going to be concerned about writing a nice object oriented program, but rather focus on just the sequence of steps needed to solve our problem. So to keep things simple, we'll just place all of the code into our main method.

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.

Coding Restrictions

Follow the instructions above carefully in coding a solution to this problem. You should not use loops or conditionals in your code since we have not covered these yet, and you will lose points if you do use loops or conditionals. Assignment statements using arithmetic expressions will be sufficient to solve this problem.

Incremental Code Development

One of the best approaches to writing code is to do it a little bit at a time. Start with the simplest thing possible and make sure that works, and then add a little more of your design and check that the additions work. When you try to add too much at once and get a lot of compile and/or runtime errors, it can be very difficult to locate the cause of the problems. But if you proceed by small amounts from your last working code, you have a much better chance of identifying easily where things go wrong.

Testing your code

Make sure you try your code with a variety of input values. Your code might work fine with one set of input values and not with another. Of course, you cannot try all possible values since there are infinitely many, but choosing a representative sample will help to convince you that your program is working correctly.

Turn in

The final solution turned in should be your MakeChange.java. This should be a working program - that is, your code must compile, and when it is run it should behave according to the requirements given for this problem. Make sure that you have removed or commented out any debugging statements. Your code should be neat (use indentation as shown in the book examples and in class to convey the structure of the program) and properly commented.