Site Navigation

Site Search

CS 110: Lab 2

Learning Objectives

After completion of this lab, you should be able to

Work collaboratively as a pair programming team

All labs in CS 110 will be done as pair programming teams. Your partner for today's lab is listed in the table below:

Hebeler 203
Grader: Vinh Tran; Assistant: Daniel Carpenter
Team 1
Aguilar Jr, Jaime
Awan, Samara
Team 2
Amezcua Gutierrez, Edson
Barrett Wright, Matthew
Team 3
Baird II, Owen
Bloom, Thomas
Team 4
Berman, Jake
Cuddington, Christopher
Team 5
Coudriet, Blake
Field, Sarah
Team 6
Erickson, Joel
Hansen, Mitchell
Team 7
Goeke, Maxwell
Hastings, Jake
Team 8
Harris, Alexander
Hogan, Martin
Team 9
Heflick, Elizabeth
Ling, Nathan
Team 10
McCauley, Rylee
Nash, Christopher
Team 11
Millard, Ryan
Olivares, Scott
Team 12
Olden, Gregory
Shearer, Stetson
Team 13
Quayle, Weston
Wagster, Nathan
Team 14
Straub-Walden, Andrew
Smith, Stephanie
Team 15
Williamson, Sarah
Akana, Christopher
Fill in: Millard, Mikel

Note: Partners will change every week.

You may wish to review basic pair programming guidelines before you begin.

You should change roles every 10 to 15 minutes.

Introduction

Chapter 1 of the textbook shows the code for calculating gross pay based on hourly pay rate and number of hours worked. While correct, it does not take into account getting paid at overtime rates when working more than 40 hours a week or deducting for taxes.

In this lab, you are given the beginning Java source code that calculates a user's gross pay taking into account overtime pay. This will give you practice reading and understanding Java code, even before actually learning the concepts

To help you practice debugging, there are a few errors in the code, both syntax errors and a logic error.

After debugging the program to correctly calculate gross pay, you will modify the program to deduct a flat 33% of gross pay for taxes.

Use test-driven development for developing software programs

Begin by developing test cases for this program. When developing test cases, it is a good idea to test boundary values. Assuming correct input for this program, there are four boundary values for testing purposes:

  1. no hours worked
  2. less than 40 hours worked
  3. exactly 40 hours worked
  4. over 40 hours worked

Using a calculator (Windows calculator works fine), calculate the gross pay, taxes, and net pay for the four test cases given below.

  1. Assume any hours worked over 40 are paid at time and a half
  2. Taxes are 33% of gross pay
Test Cases
Hours WorkedHourly Pay RateGross PayTaxesNet Pay
0$9.25   
39$10.50   
40$11.00   
41$10.00   

Write your calculated test case values in the Lab 2 assignment sheet. You will turn in this sheet at the end of lab for credit.

Use jGRASP to create the program Pay.java

The Java source code below is a simple program to calculate gross pay based on user's hours worked and hourly pay rate.

Note: it has been seeded with errors.

  1. Create a new Java file named Pay.java and copy and paste the source code below into your program.
import java.util.Scanner;

public class Pay
{
   public static void main(String [] args)
   {
   
   // Declare Variables
      double hoursWorked;      
      double hourlyPayRate;
      double grossPay;
   
   // Create a Scanner object
      Scanner keyboard = new Scanner(System.in);
   
   // Get the hours worked and the hourly pay rate
      System.out.print("How many hours did you work? ");
      hours = keyboard.nextDouble();
      System.out.print("How much do you get paid per hour? ");
      hourlyPayRate = keyboard.nextDouble();
   
   // Calculate gross pay
      if (hoursWorked <= 40)
      {
         grossPay = hoursWorked * hourlyPayRate;
      }
      else
      {
         grossPay =  (hoursWorked) * (1.5 * hourlyPayRate));
      }
   
   // Display the results
      System.out.println("You earned $" + grossPay)
   }
}

Fix syntax errors in a Java program

With programming, there are may possible types of errors. Two general categories of errors we will examine in today's lab are syntax errors and logic errors

Syntax Errors are errors in the grammar of the programming language. Examples of syntax errors are spelling mistakes in variable names, missing semicolons, unpaired parenthesis or curly braces, etc.

Syntax Errors are caught by the compiler and listed out with line number and a description of the error found. As you will find, compilers are occasionally wrong in either the location or description of the error.

All syntax errors must be corrected before the program can be run. Once the program is able to run, this does not mean that it is correct, only that it contains no syntax errors.

Some experience is needed to interpret compiler error messages. Some are fairly straightforward, for example

Pay.java:29: ';' expected
      grossPay =  (hoursWorked) * (1.5 * hourlyPayRate));
                                                       ^

means that in file Pay.java on line 29 a semicolon ';' was expected but instead it saw the character ')'

Some are a little bit harder to interpret. For example

Pay.java:18: cannot find symbol
symbol  : variable hours

means that in file Pay.java on line 18 the variable hours was used and the compiler was cannot find this symbol hours. Variable hours has not been declared as a variable in this program.

  1. Compile the Pay.java program and find and correct the three syntax errors present in the program
    • You may need to recompile the program after you fix each error to identify more errors
    • When all three syntax errors are corrected, the program should compile without errors

Change Pair Partner Roles

Change pair partner roles. The driver should become the navigator, the navigator should become the driver.

Fix logic errors in a Java program

Even though your program should now compile without errors and produce output, a logic error remains.

Logic Errors are errors in the logic of the algorithm. Examples of logic errors are statements out of order, errors in a formula, or missing steps.

Programs with logic errors can still run and give you output, but it may be the wrong output. Since jGRASP cannot generate a list of logic errors in your program, you may not realize you have errors unless you carefully check your output with your hand-calculated expected results.

  1. Run your program with the four test cases you hand-calculated answers for and compare your program's result with your calculated expected results for gross pay.
    • Note: The program does not yet calculate taxes or net pay, this will be added later
  2. On the Lab 2 assignment sheet, write down the math formula needed to calculate gross pay when hours worked for the week are greater than 40
   grossPay = write out the formula
  1. Identify the error in your program's code and fix the error in the program
  2. Recompile and re-run your program with the four test cases and ensure the results produced by the program are correct

Change Pair Partner Roles

Change pair partner roles. The driver should become the navigator, the navigator should become the driver.

Modify your program to add another feature

While the Pay program may now calculate gross pay correctly, in the real world, nothing can be said to be certain except death and taxes. Assume that the tax man takes a flat 33% of gross wages as taxes.

  1. On the Lab 2 assignment sheet, write down the math formula needed to calculate taxes and net pay assuming that 33% of gross pay is deducted as taxes
   taxes = write out the formula
   
   netPay = write out the formula
  1. Modify the Pay program to calculate and display taxes and net pay.
    • Percentages are entered as double values in Java, so 33% would be 0.33 in your program
  2. Recompile and re-run your program, comparing the answers it produces to your hand-calculated test cases.
How many hours did you work? 15
How much do you get paid per hour? 10.00
You earned $150.0
Taxes are $49.5
Your net pay is $100.5

Change Pair Partner Roles

Change pair partner roles. The driver should become the navigator, the navigator should become the driver.

Use named constants in your programs

In most cases, using literal values (like a tax rate of 0.33) inside of formulas are considered bad style.

  1. Modify your program to add a named constant for the TAX_RATE. Set its value to 0.33
// Declare named constant
   final double TAX_RATE = 0.33;
  1. Change the Java code to remove all uses of the literal value 0.33 in the calculations, replacing it with your named constant TAX_RATE
  2. Compile and run your program and ensure that the output is correct
  3. Modify the TAX_RATE named constant to reduce the tax rate to 30%
    • Note: only the value assigned to the named constant should change. The statements inside your code should all reference the named constant.
  4. Verify that the output produced by your program at the new tax rate is correct (use a calculator)

To Receive Credit

You are allowed to drop one lab grade for the entire quarter. No late lab or program assignments are accepted.