The redundant code will be eliminated by adding a new method to compute and print the cost
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 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);
}
}
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill