Write a method called CalculateShipping that computes and returns the shipping c
ID: 3885696 • Letter: W
Question
Write a method called CalculateShipping that computes and returns the shipping cost of an item for an online store.
The store's shipping policy states that shipping costs 75 cents per item, plus 25 cents for each ounce of product weight. There is an exception for items priced $100.00 or more, for which shipping is free.
So for an item that costs $12.99 and weighs 6 ounces, the shipping cost is computed as $0.75 + 6 * $0.25 = $2.25. For an item that costs $150.00, shipping costs $0.00.
Also write calls to the method that compute the shipping costs for the 2 above examples, placing the results in variables named cheapItem and expensiveItem.
Explanation / Answer
class Main
{
// method that takes 2 parameters
// amount of the item
// weight of the item
// returns the total (including shipping cost)
public static double CalculateShipping(double amount, double weight)
{
double total=0;
// checking if the price is more than 100
if(amount>=100)
return total;
// calculating the shipping cost
else
total = 0.75+weight*0.25;
// returning total
return total;
}
public static void main(String[] args)
{
// sample run for cheap and costly items
double cheapItem = CalculateShipping(12.99, 6);
double expensiveItem = CalculateShipping(100,10);
System.out.printf("Cheap: %f Costly: %f",cheapItem, expensiveItem);
}
}
Output Cheap: 2.250000 Costly: 0.000000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.