Computer Science 220: Computer Programming I

Spring 2001

Programming Assignment # 1

Due: Noon, Wednesday, January 31, 2001


Design and implement a program that simulates the actions of a cash register for Your Favorite 
Shop.  The Your Favorite Shop only sells 5 items.  They are:
·                Elmo dolls for $22.94
·                Luvs diapers for $11.53
·                Book bags for $29.95
·                Calculators for $19.00, and
·                Candy bars for 69˘
The Your Favorite Shop is located in a county that charges sales tax at a rate of 6%.
The program needed by the Your Favorite Shop must print a cash register receipt that lists all
items purchased with sales price, the purchase total before taxes, the amount of tax charged, and the total 
sales amount.  It must look like the following (which assumes three items were purchased):
        Your Favorite Shop
        Elmo Doll      $22.94
        Luvs Diapers   $11.53
        Luvs Diapers   $11.53
        Subtotal       $46.0
        Tax            $2.76
        Total          $48.76
Thanks for shopping at Your Favorite Shop
 Your assignment is to write a Java program that prints the cash register receipt for the 
following purchase:  3 packages of Luvs diapers, 6 candy bars, 1 calculator, 1 book bag and 2 Elmo dolls.  
Dollar amounts printed on the cash register receipt must be limited to 2 digits to the right of the decimal 
point.  (Your use of the Java language for this assignment is limited to those concepts covered through 
section 2.7 in the text.)
Documentation of your program should follow the samples in the text and shown in class, but 
must contain the following “opening” information:
   Class:            CMPS 220, Section ??  
   Assignment:       HMWK1
   Due Date:         
   Certification of Authenticity (include one of the following):
                   I certify that this lab is entirely my own work.  
                   I certify that this lab is my own work, but I received some assistance from:

                               <Name(s) of classmate(s) you worked with>
                               <Describe nature of collaboration – be specific!>
Task:  <Describe nature of task to be carried out.  Use your own words.  Be thorough and careful.>
Input:  <Describe the input and its format required of the program.>
Output: <Describe the nature and look of the output produced.>
1.       Instructions on how to submit your assignment will be provided soon.
2.       24-hour blackout period:  You may ask questions about the assignment up until 24-hours before it is due.              
3.       Upon receipt of your graded assignment, you have one week to ask the teacher any questions regarding your grade.
Task: This program simulates a simple cash register.
It computes the cost of a purchase including sales
tax using a tax rate of 6%.
Input: None
Output: A cash register receipt, showing the cost of
each item purchased, a subtotal, the tax charged,
and a final purchase total. 
public class CashRegister {
   public static void main(String[] args) {
      // Declare fixed item prices
      final float ELMO_DOLL = 22.94F;
      final float LUVS_DIAPERS = 11.53F;
      // Declare tax rate
      final float COUNTY_TAX_RATE = 0.06F;
      // Declare variables
      float subtotal = 0;   // Used to accumulate cost of purchase
      float taxCharge;     // Tax owed on purchase
      float purchasePrice;     // Final purchase price
      // Print receipt header
      System.out.println("\tYour Favorite Shop");
      // Compute subtotal as each item "scanned" and print line on receipt
      subtotal = subtotal + ELMO_DOLL;  // Elmo doll scanned
      System.out.println("\tElmo Doll\t$" + ELMO_DOLL);
      subtotal = subtotal + LUVS_DIAPERS; // Diapers purchased
      System.out.println("\tLuvs Diapers\t$" + LUVS_DIAPERS);
      subtotal = subtotal + LUVS_DIAPERS; // 2nd pack of diapers purchased
      System.out.println("\tLuvs Diapers\t$" + LUVS_DIAPERS);
      // Print purchase price, before taxes
      System.out.println("\tSubtotal\t$" + subtotal);
      // Compute and print county taxes owed
      taxCharge = subtotal * COUNTY_TAX_RATE;
      System.out.println("\tTax\t\t$" + taxCharge);
      // Computer and print total purchase price
      purchasePrice = subtotal + taxCharge;
      System.out.println("\tTotal\t\t$" + purchasePrice);
      // Print closing message on receipt
      System.out.println("\nThanks for shopping at Your Favorite Shop");