LAB EXERCISE 9:  Shipping Charges

Goal: In this exercise, you will learn how to:
1. Insert the Main Method (Main Entry Point to the Java Program)
2   Import a Java Class Library
3.  Declare Variables
4.  Create a Constructor (Special Method)
5.  Create a Method and accept arguments
6.  Create an Object from the Class
7.  Use an If-Then-Else-If statement
8.  Relational and Logical Operators to test a condition and execute code
8.  Call a Method and pass arguments

Program Specifications:
The Freight Shipping Company charges the rates listed in the following table:
Weight of Package (in pounds)               Shipping Rate per Mile
4lb or less                                                        $0.01
Over 4lb, but not more than 13lbs                   $0.015
Over 13lbs, but not more than 22lbs               $0.02
Over 22lbs                                                       $0.025
Create an application that allows the user to input the weight of the package and the distance it is to be shipped.  Then display the shipping charges using the formula:

Shipping Charges = distance & shipping rate per mile.

 

A. Pre-requisites:
1. Create a folder on your desktop Exercise-9

2. Launch Java EE- Eclipse
Note:  You will need to use the Java Perspective Workbench for this exercise

3. Setup your Eclipse Workspace to point to the Exercise-9 folder
a. Select File-> Switch Workspace
b. Browse and select your Exercie-9 folder as your Workspace.



B. Requirements:

1.  Create a Java Project and name it as ShippingCharges

2.  Create the first Class that will have the Main Method

a.       Name the Class as MainApp

b.       Choose the main method to insert into the class

        

3.  Create the second Class that will have the Constructor and Methods

a.      Name the Class as PackageShip

b.      Do not select the main method stub

   

C. Requirements for the PackageShip Class:

1.  Insert the import java.util.Scanner class which will allow data to be inputted from the console.

 

2.  Declare the variables- packageWeight, packageDistance, shipRate, and shipCharge using the double data type

3.  Create the Constructor (special method) called PackageShip() that will prompt the user to input the data

a.  Declare the scanner input class objects

b.  Prompt the user to input the data.  

c.   Assign the inputted data to the variable

d.  Close the scanner input class object
Below is the code you can copy and paste

// declare the scanner object used to input the package weight from the console
Scanner inputWeight= new Scanner(System.in);
System.out.print("Enter the package weight (pounds): ");
packageWeight = inputWeight.nextDouble();

// declare the scanner object used to input the distance travel to ship package
Scanner inputDistance= new Scanner(System.in);
System.out.print("Enter the travel shipping distance (miles): ");
packageDistance= inputDistance.nextDouble();

// close the scanner input objects
inputWeight.close();
inputDistance.close();

 

4.  Create the Method called  getShippingRate with a return data type of double

a.  This method should accept the argument parameters (double weight)

b.  Use an if-then-else-if statement to test the condition – weight.
If the condition is true, execute the code and return the shipRate result

 

 

 

 

 

 

 

 

 

 

 

5.  Create a Method called calcShippingCharge with a return data type of double.

a.  This method should accept the argument parameters (double rate, double distance)

b.   The formula to calculate the shipCharge = rate * distance

c.    Return the result.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


D. Requirements for the MainApp Class:

1.  Add comments (documentation)– Program Description, Author and Date

2.  Create the instance of object called product from the PackageShip class

3.  a. Declare weight variable as double and assign the value from the product.packageWeight object’s variable
b. Declare distance variable as double and assign the value from the product.packageDistance object’s variable

4.  Declare the rate variable as double and assign the return value from the called object method- product.getShippingRate(weight).  Pass the weight value as arguments.

5.  Declare the charge variable as double and assign the return value from the called object method - product.calcShippingCharge(rate, distance).  Pass the rate and distance values as arguments.

6.  Display the returned results on the console.  Below is the code you can copy and paste.

// display the returned results on the console
System.out.println("Shipping Rate: " + rate);
System.out.print("Shipping Cost: ");
System.out.format("$%.02f", charge);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


E. Test:  

1.   Save your Java code

2.   Compile and run your Java program.

3.   Verify there is no syntax, logical or run-time errors.

4.   Use the following set of test data to determine if the application is calculating properly:

Weight (lbs)     Distance (miles)      Shipping Rate          Shipping Charge
        3                      100                  $0.01                       $1.00
        11                    200                  $0.15                       $3.00

        17                    750                  $0.02                       $15.00

        33                    2000                $0.025                     $50.00            

       

F. Submit your exercise in the Canvas Lab Exercise #9 Drop Box.

1. Submit the screen shot of the Eclipse Workbench window showing the Console output screen.
You can use Paint (save as JPG) or Word to paste the screenshot.

2. Zip up and submit the compressed ShippingCharges subfolder that is in the Exercise-09 folder.
NOTE: Right click on the
subfolder and select Send to “Compress Folder”.  The file will have a file extension of .zip.