NB: Can someone help fix this code for me please? When I run the program, I keep
ID: 3744582 • Letter: N
Question
NB: Can someone help fix this code for me please? When I run the program, I keep getting a compiler error "Unreachable Statement". package week_1; import static input.InputUtils.doubleInput; /** * * A parcel delivery company charges the following rates to ship a parcel. • Up to 10 pounds: $2.15 per pound • Up to 20 pounds: $1.55 per pound • Up to 30 pounds: $1.15 per pound The shipping company does not ship parcels that weigh over 30 pounds. So, a parcel that weighs 17 pounds will cost $1.55 x 17 = $26.35. Write a program that asks the user for the weight of a parcel and displays whether it can be shipped, and what it will cost. Optional extra: the most obvious solution to this problem uses if statements for the price bands. Can you think of a different way? Hint – loops and arrays of price and max weights for price? */ public class Question_6_Parcel_Delivery { public double MAX_WEIGHT = 30; // Use this in the canShip method public static void main(String[] args) { // Don't modify the code in this method. Question_6_Parcel_Delivery delivery = new Question_6_Parcel_Delivery(); double weight = doubleInput("Enter weight of parcel"); boolean canShip = delivery.canShip(weight); if (canShip) { double price = delivery.calculatePrice(weight); System.out.println(String.format("It will cost %.2f to send your %.2f pound parcel", price, weight)); } } public boolean canShip(double weight) { // TODO return false if parcel weighs 0 or less if (weight < 1) return false; // TODO return false if parcel weighs more than MAX_WEIGHT. Use the MAX_WEIGHT variable in this code else if (weight > MAX_WEIGHT) return false; // TODO otherwise, the parcel is more than 0 and less than or equal to MAX_WEIGHT. Return true. else return true; // Replace this with your code } public double calculatePrice ( double weight) { // TODO assume parcel is a valid weight. Figure out price to ship this parcel. if (weight <= 10) //Replace this with your calculated price return weight * 2.15; if (weight > 10 && weight <= 20) ; return weight * 2.55; if (weight < 20 && weight <= 30); return weight* .15; } }
Explanation / Answer
package week_1; import static input.InputUtils.doubleInput; /** * A parcel delivery company charges the following rates to ship a parcel. ** • Up to 10 pounds: $2.15 per pound * • Up to 20 pounds: $1.55 per pound * • Up to 30 pounds: $1.15 per pound *
* The shipping company does not ship parcels that weigh over 30 pounds. *
* So, a parcel that weighs 17 pounds will cost $1.55 x 17 = $26.35. *
* Write a program that asks the user for the weight of a parcel and displays whether it can be shipped, and what it will cost. *
* Optional extra: the most obvious solution to this problem uses if statements for the price bands. Can you think of a different way? Hint – loops and arrays of price and max weights for price? */ public class Question_6_Parcel_Delivery { public double MAX_WEIGHT = 30; // Use this in the canShip method public static void main(String[] args) { // Don't modify the code in this method. Question_6_Parcel_Delivery delivery = new Question_6_Parcel_Delivery(); double weight = doubleInput("Enter weight of parcel"); boolean canShip = delivery.canShip(weight); if (canShip) { double price = delivery.calculatePrice(weight); System.out.println(String.format("It will cost %.2f to send your %.2f pound parcel", price, weight)); } } public boolean canShip(double weight) { // TODO return false if parcel weighs 0 or less if (weight < 1) return false; // TODO return false if parcel weighs more than MAX_WEIGHT. Use the MAX_WEIGHT variable in this code else if (weight > MAX_WEIGHT) return false; // TODO otherwise, the parcel is more than 0 and less than or equal to MAX_WEIGHT. Return true. else return true; // Replace this with your code } public double calculatePrice(double weight) { // TODO assume parcel is a valid weight. Figure out price to ship this parcel. if (weight 10 && weight
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.