Question: The redundant code will be eliminated by adding a new method to compute and print the cost based on item weight. Update the program below

The redundant code will be eliminated by adding a new method to compute and print the cost based on item weight.

Update the program below to add a new method calculateShipping that has one formal parameter for weight. The method will need a local variable for cost. The method should test the weight and print the corresponding cost.

Update the main method to replace the existing code with 3 calls to calculateShipping, each passing an actual value for weight. The main method will no longer need local variables.

Confirm that the new version of the program produces the same output as the original version.

 

public class ShippingCostCalculator {


 

public static void main(String[] args) {


 

   double weight1, weight2, weight3;

   double cost1, cost2, cost3;


 

   weight1 = 22.0;

   weight2 = 10.0;

   weight3 = 12.0;


 

   //calculate cost for item#1

   if (weight1 < 15.0)

   {

      cost1 = 9.95;

   }

   else

   {

      cost1 = 12.95;

   }

   System.out.println(cost1);


 

   //calculate cost for item#2

   if (weight2 < 15.0)

   {

      cost2 = 9.95;

   }

   else

   {

      cost2 = 12.95;

   }

   System.out.println(cost2);


 

   //calculate cost for item#3

   if (weight3 < 15.0)

   {

      cost3 = 9.95;

   }

   else

   {

      cost3 = 12.95;

   }

   System.out.println(cost3);

  }

}


 

Step by Step Solution

3.43 Rating (156 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

public class ShippingCostCalculator public static void mainSt... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!