Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA: WRITE IN \"SWITCH-STATEMENT!\" THIS CODE NEEDS TO BE IN \"SWITCH-STATEMENT

ID: 3792989 • Letter: J

Question

JAVA: WRITE IN "SWITCH-STATEMENT!"

THIS CODE NEEDS TO BE IN "SWITCH-STATEMENT"

IN SWITCH STATEMENT

The Fast Freight Shipping Company charges the following rate: Weight of Package Shipping Rate Per 500 miles 2 pounds or less $1.10 Over 2 pound but no more than 6 pounds $2.20 Over 6 pound but no more than 10 pounds $3.70 $4.80 Over 10 Pounds The shipping rate per 500 miles are not prorated. For example, if a 2-pound package is shipped 550 miles, the shipping charges will be $2.20. If a 6.5-pound package is shipped 1150 miles, the shipping charges will be $11.10. Write a program that asks for user to enter the weight of a package, and the number of miles shipped. The program will then calculate and display the shipping charges for the package.

Explanation / Answer

import java.util.Scanner;
class HelloWorld{
public static void main(String args[]){
   double rate = 0.0,price = 0.0 ;
   int distance = 0;
   Scanner sc=new Scanner(System.in);
   
   System.out.println("Enter the weight of a package");
   double weight = sc.nextDouble();
   System.out.println("Enter the number of miles shipped");
   int miles =sc.nextInt();
   double chck1 = Math.ceil(weight);
   int chck2 = (int)chck1;
   switch(chck2)
   {
    case 0 :
    case 1 :
    case 2 :
          rate = 1.10;
        
          while(miles != 0)
          {
      miles = miles / 500 ;
      distance = distance + 1;
          };
    price = rate * distance;
    break;
    case 3 :
    case 4 :
    case 5 :
case 6 :
          rate = 2.20;
        
          while(miles > 0)
          {
      miles = miles - 500 ;
      distance = distance + 1;
          };
    price = rate * distance;
    break;


   
    case 7 :
    case 8 :
    case 9 :
case 10:
            rate = 3.70;
        
          while(miles > 0)
          {
      miles = miles - 500 ;
      distance = distance + 1;
    
          };
    price = rate * distance;
    break;
  
             
    default:
            rate = 4.80;
        
          while(miles > 0)
          {
     miles = miles - 500 ;
     distance = distance + 1;
   
          };
    price = rate * distance;
    break;
  
   }

   System.out.println("The shipping charges for the package is $: "+price);
}
}