CSci 101: Lab 2


Objectives

The objective of this lab is to design and implement your own Java program. Take your time on this lab and don't rush things. During the course of this assignment, you will learn how to:

Problem Description

Write a Java program that takes as input the price of an item (or set of items) and the cash tendered by the customer, and outputs the cash to be returned, keeping the number of coins to a minimum. Executing the completed program will produce output similar to what is given below, where the numbers in italics are entered at the keyboard by the user. Notice that this program is similar (but not identical) to the example program (Display 2.5) given on page 76 in your textbook.

Sample Execution

Below is a sample only. You might design your program differently, but in any case there should be (1) some introductory explanation, (2) prompts, (3) and output of the results of the computation.
  This program will calculate your change, given the cost of your
  purchases and the amount of money tendered.  The minimum number
  of coins will be returned in your change.

  Enter the total amount of your purchases:  $5.32
  Enter the amount of cash tendered:  $10.00

  The cash to be returned is $4.68
  The number of quarters returned is 2
  The number of dimes returned is 1
  The number of nickels returned is 1
  The number of pennies returned is 3

  Program terminating...

Activities

Don't begin programming right away. There are three stages to this lab: (1) Design, (2) Implementation, and (3) Documentation. Below you will find instructions that will help you to design and implement a program which solves the above problem. We begin with design, proceed with implementation, and finish with documentation. In practice, documentation should be done in the design and implementation stages, but to clarify what documentation actually is, it is made into a separate stage.

Design

First we need to consider the problem statement and make some decisions about the following aspects of the program we will eventually implement. Write down on a piece of paper the answers to the following questions.

  1. What variables and constants (if any) are needed in this program? What are their data types? Give each variable and constant an appropriate name.
  2. What user input is required? Write a sentence or two in English describing each input you expect the user to provide.
  3. What output does the program provide? Ex. Introductory message, prompts, results. For each type of output you think of, write down what the program should print to the screen.
  4. What computation(s) does your program need to perform? The answer to this question may affect the answer to questions 1 and 2 above if variables and/or constants are needed to calculate and store the results of your computations. Update your lists of variables and constants if necessary.

Implementation

Once we have a basic program design, we begin with the implementation. We write our program in small increments, compiling and testing along the way to make sure we are proceeding correctly. Follow the steps below carefully, making sure you are confident that the result of each step is correct before moving on to the next step.

  1. Create a new Java source file called lab2.java. This file contains the definition for the class lab2, with a main program definition that you are familiar with from lab1. Indeed, you might just copy part of lab1.java to get started with lab2.java.
  2. Use System.out.print and System.out.println statements to create your program's introductory output message.
  3. At this point you have the beginning of a program which will run. Save, compile and run your program. Fix any syntax errors which appear. Make sure the output appears correctly as you expect.
  4. Go back to your editor and add the variable and constant declarations you decided on in your design. This will not yet affect the program's performance, because you are not yet using these variables and constants.
  5. Save, compile and run your program. This step simply ensures that you have declared your variables and constants correctly. Fix any syntax errors which appear. The program's behavior should be the same now as it was after completion of step 3 above.
  6. Go back to your editor and add prompts for user input after the output of your introductory message. Use the SavitchIn class to read appropriate values from the user into variables that have been declared in your program. After getting the user's input, use System.out.println to echo the values that the user typed in back out to the screen.
  7. Save, compile and run your program. This step will ensures that you are reading the user’s input correctly.
  8. Go back to your editor and remove the last output statements created in step 6 above, echoing the user's input back out to the screen. These are not needed in the final program output.
  9. Add some of the calculations that your program needs to perform. Do these one or two at a time, adding print statements for any intermediate results, so you can see them on the screen and ensure that they are correct.
  10. Save, compile and run your program. Fix any syntax or semantic errors that occur.
  11. Repeat steps 9 and 10, removing extraneous output messages after ensuring correct results, until all of the calculations that your program needs to perform have been implemented and tested.
  12. Complete the final output of your program so that it matches what's given in the sample execution section above.

Documentation

Although we have completed the implementation of our program (meaning the computer understands what to do), we are not completely done yet. Assuming you chose good variable and constant names, it should be fairly easy for someone else to read your program and figure out what’s going on. But to make this task easier, and to help you if you ever go back to look at this program in the future, we add comments into the code to make it more readable for humans. Follow the steps below to complete this lab assignment.

  1. Add a comment "box" at the top of your program which includes:
  2. Add comments for your variables and constants. If these are very brief, they should be placed at the end of the associated declaration line, and you should attempt to align them in the same column whenever possible. Otherwise, longer comments should be placed before the corresponding variable or constant declaration. Comments should indicate what the constant value represents, or what the variable is used for.
  3. Examples:
       public static final int QVAL = 25; // value of a quarter
       double purchase;                   // amount of user’s purchase
       int quarters;                      // number of quarters needed in change
    
       - or -
    
       // the following integers store the number of coins needed in change
       int quarters, dimes, nickels, pennies;
    
  4. Add comments for program statements. It is not necessary to comment every line of code. For instance, the System.out.print instructions in this program are self-explanitory. But you need to make clear whats going on, especially for computations. Be sure to space and format your comments so that it is clear what the comment applies to.
  5. Examples:
       // get user's input
       System.out.print("Enter the total amount of your purchases: $");
       purchase = SavitchIn.readLineDouble();
       ...
       change = tendered - purchase;    // calculate total change due
       ...
       // figure out number of quarters needed, and then calculate remainder
       quarters = amount / QVAL;
       amount = amount % QVAL;
       ...
    


When you're finished, print the source code and the run of the program, and hand them in.