In: Computer Science
1 Objective The purpose of this assignment is to test your familiarity with Java I/O, and if-else statements. Please submit your file as ”Shipping.java”
2 The Backstory Amazon.com wants to try out it’s new ‘Delivery by Drone” service and has recruited you to write a Java console application to calculate the shipping cost. They are going to charge the customer based on various criteria, as shown below:
Weight (in kg) | Rate per 50 miles |
2 or less Over 2 Kg, up through 6 Kg Over 6 Kg, up through 10 Kg Over 10 Kg, up through 20 Kg |
$5.10 $10.18 $22.43 $40.60 |
Amazon Prime members get a 10% discount.
3 Specifications
• 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 50 miles shipped” means that anything up to 50 miles is at the 50 mile rate, anything above 50 up to 100 miles is at the 100 mile rate, and so on.
• Ask if the user is an Amazon Prime member. If the user says Yes, give them a 10% discount.
• 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
4 Sample Runs
Sample Run 1:
Welcome to Amazon Shipping Calculator
Please enter the weight of the package, in Kg: 19.4
Please enter the distance to be shipped (in miles): 2318 Are you an Amazon Prime member? (Yes/No): No
Package weight = 19.4 Kg Shipping rate = $40.60 per 50 miles Number of miles = 2318
Total shipping charges = $ 1908.20
Goodbye
Sample Run 2:
Welcome to Amazon Shipping Calculator
Please enter the weight of the package, in Kg: 8.4 Please enter the distance to be shipped (in miles): 134
Are you an Amazon Prime member? (Yes/No): Yes
Package weight = 8.4 Kg Shipping rate = $22.43 per 50 miles Number of miles = 134
Total shipping charges = $ 60.56
Goodbye
Sample Run 3 (error case): Welcome to Amazon Shipping Calculator
Please enter the weight of the package, in Kg: -5 Invalid package weight. Program aborted
Sample Run 4 (error case): Welcome to Amazon Shipping Calculator
Please enter the weight of the package, in Kg: 21.8 Cannot accept packages over 20 Kg. Program aborted
We are required to calculate the total shipping charge based on the given rate and the user inputs. The following java program takes the weight and the distance from the user. It also asks whether the user is a Prime member or not and calculates the final price. If the input is invalid, it displays the error message and the program terminates.
import java.util.Scanner; // Import the Scanner class
class Shipping{ // Shipping class
public static void main(String[] args) {
Scanner input = new Scanner(System.in); // Create a Scanner
object
System.out.println("Welcome to Amazon Shipping Calculator"); //
welcome message
System.out.println("Please enter the weight of the package, in
Kg:"); // asks user to input weight of the package
double myDouble = input.nextDouble(); // read the weight
if(myDouble <= 0){ // if weight is less or equal to zero, show
invalid input message
System.out.println("Invalid Input, weight should be greater than
zero");
System.exit(0); //terminate the program
}
else if(myDouble > 20){ // if weight is greater than 20, show
invalid input message
System.out.println("Invalid Input, weight should be less than
20");
System.exit(0);
}
System.out.println("Please enter the distance to be shipped (in
miles):"); // asks user to input distance
int myInt = input.nextInt(); // reads distance
if(myInt <= 0){ //if distance is less or equal to zero, show
invalid message
System.out.println("Invalid Input, must be a positive distance to
ship");
System.exit(0);
}
else if(myInt > 3000){ // if distance is greater than 3000, show
invalid message
System.out.println("Invalid Input, distance should be less than
3000 miles");
System.exit(0);
}
int d = myInt/50; //Here, d is the quotient when distance is
divided by 50
if(myInt%50 > 0){
d=d+1; // If the remainder is greater than zero, increment d. So,
that d can be multipled with rate to get the total price
}
double price =0;
if(myDouble <= 2){ // to calculate the price based on
rates
price = 5.1*d;
}
else if(myDouble <= 6){
price = 10.18*d;
}
else if(myDouble <= 10){
price = 22.43*d;
}
else if(myDouble <= 20){
price = 40.6*d;
}
System.out.println("Are you an Amazon Prime member? (Yes/No):"); //
asks whether if user is prime
String myString = input.next(); // reads user input(Yes/No)
if(myString.equals("Yes")){ // If user is prime user, give 10%
discount
price = price*0.9;
}
else if(myString.equals("No")){ // If not, price remains the
same
price = price;
}
else{
System.out.println("Invalid input, Please enter (Yes/No):");
System.exit(0);
}
System.out.println("Package weight ="+myDouble+"Kg"); //prints
package weight
if(myDouble <= 2){ //to print shipping rate based on
conditions
System.out.println("Shipping rate =$5.10 per 50 miles");
}
else if(myDouble <= 6){
System.out.println("Shipping rate =$10.18 per 50 miles");
}
else if(myDouble <= 10){
System.out.println("Shipping rate =$22.43 per 50 miles");
}
else if(myDouble <= 20){
System.out.println("Shipping rate =$40.60 per 50 miles");
}
System.out.println("Number of miles ="+myInt); // prints number of
miles
String priceInString = String.format ("%,.2f", price); //for the
money format
System.out.println("Total Shipping charges =$"+priceInString);
//Prints the shipping charge
System.out.println("Goodbye"); //prints the goodbye message
}
}
Output: