Fundamental Programming Concepts
Summer 2000

Lab 3

Overview
Warning: this lab is more difficult than the first two labs. You will need to read it carefully -- preferably away from the keyboard -- before you begin programming. This is the level of difficulty that you can expect from future labs in the course.

This lab will introduce almost nothing new about Java. Instead, it will give you a chance to practice and develop the programming skills we've been working on so far:

  • Getting input with the Console class
  • Mathematical operations
  • Using methods, return values, parameters, and arguments

You may find it useful in this lab, and future labs, to change the font that the Metrowerks editor uses. To change it to a much more usable font:

  1. Go to the Edit menu and choose Preferences
  2. On the left hand panel, click on Font & Tabs
  3. Change the font on the right hand panel to Courier New
  4. Click Save, then close the dialog

 

Part I: Producing a Bill

The Cayuga Carpet Company has asked you to write a computer program to calculate the amount a customer should be charged and produce a bill for the customer. You will call this project CarpetBill.

The input will be:

  • the customer's name
  • square yards (sq. yd.) of carpet purchased
  • carpet cost per sq. yd.
  • labor cost per sq. yd.
  • fee for floor preparation
  • percentage discount

The output will be a bill stating:

  • the customer's name
  • square yards of carpet purchased
  • carpet cost per sq. yd.
  • labor cost per sq. yd.
  • floor preparation fee
  • the charge for the carpet
  • the charge for the labor
  • the amount of the discount
  • the amount of sales tax
  • the total charge to the customer

The company has given you the following information to help in writing the program:

  • Customers are not allowed to purchase a fraction of a square yard
  • The carpet charge is equal to the number of square yards purchased times the carpet cost per sq. yd.
  • The labor charge is equal to the number of square yards purchased times the carpet cost per sq. yd, plus the floor preparation fee
  • The percentage discount only applies to the carpet charge, not the labor charge
  • Customers are charged 8% sales tax on the carpet; there is no sales tax on the labor 

Here is an example of how one run of the program might occur:

Cayuga Carpet Company Billing Program

Enter the customer's name: Michael Clarkson
Enter the number of square yards purchased: 17
Enter the cost of the carpet per sq. yd.: 18.50
Enter the cost of labor per sq. yd.: 3.50
Enter the floor preparation fee: 38.50
Enter the customer's discount percentage: 2.0



     Cayuga Carpet Company
-------------------------------

Customer: Michael Clarkson
Amount purchased: 17 sq. yd.

Materials
    Cost per sq. yd.: $18.50
    Carpet cost: $314.50
    Discount: $6.29
    Carpet charge: $308.21

Labor
    Cost per sq. yd.: $3.50
    Labor cost: $59.50
    Floor prep fee: $38.50
    Labor charge: $98.00

Sales tax: $24.66

Total charge: $430.87

Formatting Monetary Amounts for Output

The following method will change a monetary amount stored in a double into a String that is suitable for being printed to the screen:

/**
 * Yield a string containing the amount in the argument formatted 
 * as currency in the default locale.
 */
static String formatCurrency(double amount) {
    return java.text.NumberFormat.getCurrencyInstance().format(amount);
}

Here's an example of how to use it:

double price = 1.00;
System.out.println("Price of an item from a dollar store = " + formatCurrency(price));

Additional Requirements

In order to give you more practice with certain aspects of variables, and to introduce you to a common framework for programs, your main method must be divided into sections as follows (although your exact comments need not be identical to those shown below):

public static void main(String[] args) {

    /////////////////////////////////////////////////////////////////
    // Variable declarations
    /////////////////////////////////////////////////////////////////

    ...

    /////////////////////////////////////////////////////////////////
    // Get all the input
    /////////////////////////////////////////////////////////////////

    ...

    /////////////////////////////////////////////////////////////////
    // Calculate bill
    /////////////////////////////////////////////////////////////////

    ...

    /////////////////////////////////////////////////////////////////
    // Output bill
    /////////////////////////////////////////////////////////////////

    ...

}

Follow these guidelines:

  • All of your variables must be declared in the declaration section, including a specification comment for each variable. Each variable should have a well-chosen and descriptive name. See the style guide for comments and names. You'll need many variables -- your instructor's solution used 14.
  • The sales tax percentage should be represented as a constant (LL p. 63).
  • All input must be done in the input section.
  • All calculation must be done in the calculation section. This means that this is the only section in your program that you are allowed to write arithmetic operations.
  • You are allowed to use the + operator as the concatenation operator in the output section.

The rationale for these restrictions is twofold:

  1. To force you to practice programming with several variables
  2. To introduce you to the common technique of thinking of programs as occuring in three phases: input, processing, and output. This will be a useful paradigm as you start to encounter larger and more difficult problems.
Part II: Solving Systems of Equations
Write a program that will solve a system of two equations using Cramer's Rule. The input will be the coefficients and constants in the system -- these will all be integers. The output will be the solution to the system. You will call this project EquationSolver.

As an example, here is how one run of the program might occur:

Enter the coefficients and constants for this system of equations:
    ax + by = c
    dx + ey = f

Enter a: 1  
Enter b: 2
Enter c: 5
Enter d: 2
Enter e: -1
Enter f: 0

The solution to the system of equations:
    1x + 2y = 5
    2x + -1y = 0

is:
    x = 1.0
    y = 2.0
 

Cramer's Rule

Cramer's Rule is a method for solving a system of linear equations. If you have two equations with variables x and y written as:

ax + by = c
dx
+ ey = f

then the solution for x and y can be found using the following equations:

For help on the math notation used here, see this explanation.

Important: Cramer's rule will not work if the expression (ae - bd) is equal to zero! We'll soon learn how to have the program guard against this possibility. You don't have to worry about it for now, though -- just avoid giving your program such inputs.

Additional Requirements

  • You should divide your program into input, processing, and output sections again.
  • You should have a specification-comment explaining the roles of your variables. Names such as a and x are certainly permissible in this assignment. You need not have a separate variable declaration section.
  • You should write a method that calculates determinants (see the explanation of Cramer's Rule) and use it in your calculations.
Submitting
You should submit the entire folder containing your project for both of the programs in this lab.

Your folders should contain at least the following files:

  • CarpetBill
    • CarpetBill.mcp
    • CarpetBill.java
    • CS99.jar
  • EquationSolver
    • EquationSolver.mcp
    • EquationSolver.java
    • CS99.jar

You will submit your lab using FTP, not on a floppy. Follow these instructions to use FTP.

Don't forget to include a comment at the beginning of each of your classes, similar to Lab 1. Also, make sure to follow the style guidelines for all your variables and methods.