Site Navigation

Site Search

CS 110: Lab 9

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
Ling, Nathan
Team 2
Amezcua Gutierrez, Edson
Nash, Chris
Team 3
Baird, Owen
Olivares, Scott
Team 4
Berman, Jake
Shearer, Stetson
Team 5
Coudriet, Blake
Wagster, Nathan
Team 6
Erickson, Joel
Smith, Stephanie
Team 7
Goeke, Max
Awan, Samara
Team 8
Harris, Alexander
Bloom, Thomas
Team 9
Heflick, Liz
Olden, Greg
Team 10
McCauley, Rylee
Cuddington, Chris
Team 11
Millard, Ryan
Straub-Walden, Andy
Team 12
Barrett Wright, Matthew
Williamson, Sarah
Team 13
Millard, Mike
Hastings, Jake
Team 14
Field, Sarah
Quayle, Weston
Team 15
Akana, Chris
Hogan, Martin
Fill in: Hansen, Mitchell

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.

Use test-driven development for developing software programs

Lab 9 is based on Programming Challenge #6: ShippingCharges class.

The Fast Freight Shipping Company charges the following rates:

Shipping Rates
Weight of Package (in kilograms)Rate per 500 Miles Shipped
2 Kg or less$1.10
Over 2 Kg but not more than 6 Kg$2.30
Over 6 Kg but not more than 10 Kg$3.50
Over 10 Kg$4.70

The shipping charges per 500 miles are not prorated. For example if a 2 Kg package is shipped 550 miles, the charges would be $2.20

Test your understanding by completing the test case table for various weights and miles shipped

Test Cases
Weight (in Kg)Miles ShippedTotal Cost
1.6 Kg250 miles 
2.3 Kg750 miles 
7.4 Kg1000 miles 
9.8 Kg1250 miles 
12.5 Kg1550 miles 

Create the Java ShippingCharges class

  1. Design and implement the Java class ShippingCharges.java that stores the weight of a package and the miles shipped and has a method that returns the shipping charges
    • You need two private instance variables:
      1. one to store the weight (double)
      2. and the other to store the miles shipped (int)
    • The constructor method should accept two parameters:
      1. package weight (double)
      2. miles to be shipped (int)
    • The method getShippingCharges() should calculate and return the total shipping charges (as a double) based on the values saved in the object's instance variables

Change Pair Partner Roles

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

Test your ShippingCharges class with a main method

  1. Create a new Java class, ShippingChargesDemo.java, and copy and paste the main method below that reads in a weight and miles shipped from the user and then displays the total charges.

Note: this code is provided below. Test and ensure that it works correctly with your ShippingCharges class.

import java.util.Scanner;
import java.text.DecimalFormat;

public class ShippingChargesDemo
{
   public static void main(String [] args)
   {
   // local variables
      double weight;
      int miles;
         
   // Create a Scanner object
      Scanner keyboard = new Scanner(System.in);
         
   // Create a DecimalFormat object
      DecimalFormat money = new DecimalFormat("0.00");
       
   // Get the package weight and miles shipped
      System.out.print("Enter the package weight: ");
      weight = keyboard.nextDouble();        
      System.out.print("Enter the miles shipped: ");
      miles = keyboard.nextInt();
   
   // Create a ShippingCharges object      
      ShippingCharges myPackage = new ShippingCharges(weight, miles);
   
   // Display the total charges      
      System.out.println("Total Charges will be $" +
         money.format(myPackage.getShippingCharges()));
   }
}
  1. Test your program by running it and verifying that the answers your program produces match the answers you hand-calculated as the test cases.

Sample session

Enter the package weight: 9.5
Enter the miles shipped: 750
Total Charges will be $7.00

Change Pair Partner Roles

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

Use your class with a windows style program

  1. Create a third new Java class, WindowsDemo.java, that duplicates the functionality of the ShippingChargesDemo program using the JOptionPane.

weight JOptionPane

miles shipped JOptionPane

total charges JOptionPane

To Receive Credit

You are allowed to drop one lab grade for the entire quarter.