In: Computer Science
5) Create the following in a Java program
Create a scanner
Prompt the user to enter the name where the box of mail is shipping
from and create the variable and relate to scanner
Prompt the user to enter the name of destination where the box of
mail will be shipped and create the variable and relate to
scanner
Prompt the user to enter the weight of the package and create
variable to relate to scanner
Calculate cost of shipping
If the weight of box of mail is above 50 the box cannot not be
shipped
inside of else create double variable where is called cost of
pound
then you do more condition where if weight is between o and 1 cost
of pound is 3.5
then if weight is less equal to 3 cost of pound is 5.5
then if weight is less equal to 10 cost of pound is 8.5
everything else cost of pound is 10.5
Display inside of condition shipping cost by multiplying cost of
pound with weight
In case of any query do comment. Please rate answer as well. Thanks
Code:
import java.util.*;
public class ShippingCostCalculator
{
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
//read from where shipping is happening
System.out.print("Enter the name where the box of mail is shipping from: ");
String from = scnr.nextLine();
//read where shipping is going
System.out.print("Enter the name of destination where the box of mail will be shipped: ");
String to = scnr.nextLine();
//read the weight of the package
System.out.print("Enter the weight of the package: ");
int weight = scnr.nextInt();
if (weight > 50)
System.out.println("Box can not be shipped"); //if weight is above 50, can't ship
else{
double costOfPound =0.0;
//now calculate the cost of pound
if(weight ==0 || weight == 1)
costOfPound = 3.5;
else if(weight <= 3)
costOfPound = 5.5;
else if(weight <= 10)
costOfPound = 8.5;
else
costOfPound = 10.5;
//print the shipping cost
System.out.printf("Shipping Cost: $%.2f\n", costOfPound * weight);
}
}
}
==============screen shot of the code=========
output: