Assignment #2
Due: Tues, June 3
Objective
This assignment will involve practice with selection statements.
Task
You will write a program to compute shipping charges based on various
criteria. Write your program in a file with the following filename:
Description
The Fast Freight Shipping Company charges the following rates:
Weight of Package (in Kilograms) |
Rate per 500 Miles Shipped |
2 Kg or less | $1.10 |
Over 2 Kg, up through 6 Kg | $2.20 |
Over 6 Kg, up through 10 Kg | $3.70 |
Over 10 Kg, up through 20 Kg | $4.80 |
You will write a program that asks for the weight of the package
and the distance it is to be shipped, and then you will display the
charges. Here are the details:
- First, ask for the weight of the package (in kilograms). The user
can enter this as a decimal number, so use type double. Values
of 0 or less are invalid (i.e. a package has to weigh something). Do
not accept weights of more than 20 Kg either, as this is the maximum
weight the company will ship).
- If the user enters an invalid choice, print an appropriate error
message and abort the program. (See sample outputs for error
messages)
- Now ask the user to enter the distance to ship the package (in
miles). This will be entered as an integer. 0 miles or less is
considered invalid (must be a positive distance to ship). Also, do not
accept distances of more than 3000 miles -- consider 3000 miles to be
the company's maximum shipping distance
- If the user enters an invalid choice, print an appropriate error
message and abort the program. (See sample outputs for error
messages)
- For a valid weight and distance, compute the shipping charges
according to the chart above, noting that "rate per 500 miles shipped"
means that anything up to 500 miles is at the 500 mile rate, anything
above 500 up to 1000 miles is at the 1000 mile rate, and so on.
Example: shipping a 8 Kg package 1300 miles would be at the $3.70
per 500 miles rate, and since it's 1300 miles, it would be $11.10 (i.e.
$3.70 times 3)
- Print out the following results:
- The package weight (default format), in kilograms
- The shipping rate for this package, to 2 decimal places, (money
format)
- The number of miles chosen
- The calculated shipping cost, to 2 decimal places (money
format)
- See the Sample Runs below for expected output messages and
numerical output formats
- Also note: The System library has a method called
exit(), which will cause a program to terminate immediately.
It requires a parameter -- usually it is sufficient to just pass in the
value 0
Sample Runs
(user input is underlined, to distinguish it from output)
Sample run 1
Welcome to FFSC Shipping Calculator
Please enter the weight of the package, in Kg: 19.4
Please enter the distance to be shipped (in miles): 2318
Package weight = 19.4 Kg
Shipping rate = $4.80 per 500 miles
Number of miles = 2318
Total shipping charges = $ 24.00
Goodbye
Sample run 2
Welcome to FFSC Shipping Calculator
Please enter the weight of the package, in Kg: 8.4
Please enter the distance to be shipped (in miles): 1340
Package weight = 8.4 Kg
Shipping rate = $3.70 per 500 miles
Number of miles = 1340
Total shipping charges = $ 11.10
Goodbye
Sample run 3 (error case)
Welcome to FFSC Shipping Calculator
Please enter the weight of the package, in Kg: -5
Invalid package weight. Program aborted
Sample run 4 (error case)
Welcome to FFSC Shipping Calculator
Please enter the weight of the package, in Kg: 21.8
Cannot accept packages over 20 Kg. Program aborted
Sample run 5 (error case)
Welcome to FFSC Shipping Calculator
Please enter the weight of the package, in Kg: 17.4
Please enter the distance to be shipped (in miles): -10
Shipping distance must be positive. Program aborted
Sample run 6 (error case)
Welcome to FFSC Shipping Calculator
Please enter the weight of the package, in Kg: 14.6
Please enter the distance to be shipped (in miles): 3010
Cannot ship more than 3000 miles. Program aborted.
General requirements
- Use the Scanner class for input
- When you write source code, make sure it is clear, readable, and
well-documented. (See style guidelines posted on home page)
Submitting:
Use the Assignment 2 submission link on Blackboard. Submit
only your source code:
Shipping.java