62.3k views
4 votes
The shipping charges per 500 miles is not prorated. For example, if a 2 pound package is shipped 550 miles, the charges would be $2.20. Write code that calculates the shipping charge based on user input.

User Inthu
by
5.4k points

1 Answer

5 votes

Answer:

See code and explanation below.

Step-by-step explanation:

Assuming this complete problem: "Shipping Charges The Fast Freight Shipping Company charges the following rates: Weight of Package Rate per 500 Miles Shipped 2 pounds or less $1.10 Over 2 pounds but not more than 6 pounds $2.20 Over 6 pounds but not more than 10 pounds $3.70 Over 10 pounds $3.80 The shipping charges per 500 miles are not prorated. For example, if a 2-pound package is shipped 550 miles, the charges would be $2.20. Write a program that asks the user to enter the weight of a package and then displays the shipping charges. "

For this case we can create the following Java code:

// Java Scanner to take regular expressions

import java.util.Scanner;

// Definition of the variables

class Main {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

//INPUT S : we define the weigth and the charge based on the weigth

double pound, shipCharge, total;

final double weightCharge1=1.10, weightCharge2=2.20, weightCharge3=3.70, weightCharge4=3.80;

//OUTPUT S: We ask for the weigth and would be stored on pound variable

// We define the if conidtion based on the restrictions given

System.out.print("Enter the package weight(lbs):");

pound=input.nextDouble();

//Complete the remain conditions

if(pound>=0.01 && pound<=2){

shipCharge=pound*weightCharge1;

}

else if(pound>=2.01 && pound<=6){

shipCharge=pound*weightCharge2;

}

else if(pound>=6.01 && pound<=10){

shipCharge=pound*weightCharge3;

}

else if(pound>=10.01){

shipCharge=pound*weightCharge4;

}

else{

shipCharge=-1;

}

//OUTPUT: We print the final solution and if we have an error we ask for other value for the input

System.out.println();

if(shipCharge!=-1){

total=shipCharge;

System.out.printf("The Weight is: %,.2f lbs\\",pound);

System.out.printf("The total Shipping Charge: $%,.2f\\",total);

}

// Invalid case

else

System.out.println("Invalid input, must enter a value higher than 0.01");

}

}

User Jarell
by
5.4k points