Question

In: Computer Science

1 Objective The purpose of this assignment is to test your familiarity with Java I/O, and...

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

Solutions

Expert Solution

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:


Related Solutions

The purpose of this assignment is to test your familiarity with Java I/O statements and if-else statements.
ObjectiveThe purpose of this assignment is to test your familiarity with Java I/O statements andif-else statements. This assignment also tests your understanding of the basics of Javaprogramming and execution, like top-down control flows, translating the logical solutionto a problem into code, and integrating the new concepts you just learned with theolder concepts, including data types and variables, declaration, initialization andassignments, arithmetic operators, etc.Please submit your source code (i.e. your java file) on Canvas under HW1.ProblemIt’s the year 2030 and H&R...
Objective: The purpose of this assignment is to test student skills to determine functional and non-functional...
Objective: The purpose of this assignment is to test student skills to determine functional and non-functional requirements for ticket-issuing system. An automated ticket-issuing system sells rail tickets. The system is secured enough because only authenticated users are able to access. The system is developed with high performance speed and storage. Users select their destination and input a credit card and a personal identification number. The rail ticket is issued and their credit card account charged. When the user presses the...
I/O Lab Java Purpose To practice the input and output concepts discussed in this module. Specifically,...
I/O Lab Java Purpose To practice the input and output concepts discussed in this module. Specifically, reading from and writing to local files, and formatting output. Instructions Read in file input.csv and generate a cleanly formatted table in output.txt. See the samples below. input.csv name,ID,salary,years experience foo,1,13890,12 bar,2,2342,3 baz,3,99999,24 output.txt Name | ID | Salary | Years experience -----+----+--------+----------------- Foo | 1 | 13890 | 12 Bar | 2 | 2342 | 3 Baz | 3 | 99999 | 24
Java File I/O Assignment: 1. Write a generic LinkedList class referencing page 2 and page 3....
Java File I/O Assignment: 1. Write a generic LinkedList class referencing page 2 and page 3. a. The class must be named LinkedList. b. The class must provide the methods listed above for constructing, accessing, and manipulating LinkedList objects for a generic type. c. Other than for testing purposes, the LinkedList class should do no input or output. d. The package must enable the provided tests. 2. Test this class using JUnit. a. A suite of JUnit tests have been...
Java Programming Project 6: File I/O Purpose: To practice reading from as well as writing to...
Java Programming Project 6: File I/O Purpose: To practice reading from as well as writing to text files with the help of Scanner class methods, and PrintStream class methods. You will also learn to implement some simple Exception Handling. Carefully examine and follow ALL the program specifications. Take a look at the PPT slides for Chapter 7 File I/O for examples that will help with this program. Hotel Expense Recording Keeping: A hotel bookkeeper enters client hotel expenses in a...
Objective: The purpose of this assignment is to introduce declaration of list objects, the forstatement and...
Objective: The purpose of this assignment is to introduce declaration of list objects, the forstatement and the def keyword used to define functions. Problem: Write a Python module (a text file containing valid Python code) named p3.py. This file will contain the following.  Definition of a list containing strings which are in turn integers. These integers represent years, which will be used as inputs to the next item in the file  Definition of a function named isLeap. This...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods.(Need Comment, Write by Java Code) Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use and re-use of methods with input validation. Instructions It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example, “I dn'ot gvie a dman for a man taht...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
IN JAVA Minimal Documentation Required (no javadoc) Purpose The purpose of this assignment is to introduce...
IN JAVA Minimal Documentation Required (no javadoc) Purpose The purpose of this assignment is to introduce you to basic operations on a linked list. Specifics Design a program that generates a linked list of randomly generated Integer objects. Present a menu at program start that gives the user the following options (most of these options will have corresponding methods in a Linked List class): 1. Create a new list. The size will be specified by the user, make sure a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT