I need help with this in Java. Write a program that will calculate the shipping
ID: 3601483 • Letter: I
Question
I need help with this in Java.
Write a program that will calculate the shipping charge for a company. The following table shows the shipping rates for the company:
Weight of Package (in lb) | Rate per 200 miles shipped
Less than 5 lb | $1.10
Between 5 lb and 9 lb | $2.50
Between 10 lb and 50 lb | $4.90
More than 50 lb | $7.50
The shipping charges per 200 miles are not prorated. For example, if a 5 lb package is shipped 250 miles, the charges would still be $2.50.
Here is what you need to do:
1. Ask the user to enter the weight of the package.
2. Ask the user to enter the number of miles the package will be shipped.
3. Calculate the shipping cost and show it in the screen.
Explanation / Answer
ShippingCost.java
import java.util.Scanner;
public class ShippingCost {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the weight of the package: ");
double weight = scan.nextDouble();
System.out.println("Enter the number of miles the package will be shipped: ");
int miles = scan.nextInt();
double charge ;
if(weight > 50) {
charge = 7.5 * (miles/200);
} else if(weight >= 10 && weight <= 50) {
charge = 4.9 * (miles/200);
} else if(weight >= 5 && weight <= 9) {
charge = 2.5 * (miles/200);
} else {
charge = 1.1 * (miles/200);
}
System.out.println("The shipping charge for a company is: "+charge);
}
}
Output:
Enter the weight of the package:
5
Enter the number of miles the package will be shipped:
250
The shipping charge for a company is: 2.5