Question: Debugging a given program Even though programmerA was fired from his last place of employment, some desperate employer has unwittingly hired him to write a
Debugging a given program
Even though programmerA was fired from his last place of employment, some desperate employer has unwittingly hired him to write a shipping cost calculator program for their small packing company. You are being asked to correct the program. The idea behind the program is to allow the user to enter a shipping weight and shipping method. The cost of the shipping, based upon the package weight and method of shipping is calculated and then displayed. The table below details the calculation of the shipping cost:
| Ship Method | Weight | Cost |
| First Class | > 50.0 | $5 * weight |
| First Class | <= 50.0 | $50 |
| Second Class | > 50.0 | $2 * weight |
| Second Class | <=50.0 | $25 |
| Third Class | > 50.0 | $1 * weight (yes, that's just actually = to the weight) |
| Third Class | <= 50.0 | $15 |
The program should prompt the user for the package weight followed by the shipping class in the main method. From there, that information should be passed to a method called calculateShipping() which should return a double which is the shipping cost or -1.0 if the shipping cost cannot be calculated because the user entered an invalid shipping class.
The shipping cost is then passed to a message displayCost() which displays the calculated shipping cost. Be sure to use a println on your last line of output rather than a print. You may not delete all of ProgrammerA's code and re-write a new program. Instead, you should correct ProgrammerAs flawed code.
Enter shipping weight: 50 <--- user enters 50 Enter class (first, second, third): dunno <--- user enters shipping class Invalid shipping class. Program cannot continue. <-- this is a println
Enter shipping weight: 70 Enter class (first, second, third): firST Shipping Method: FIRST Shipping Cost: $350.00 Like this program? Hire me. Email: ProgrammerA@coding.edu
Required Decomposition:
You must use the following decomposition for your program:
public static double getShipWeight(Scanner keyboard) - Accepts the scanner object and prompts the user to enter the shipping weight. Returns the shipping weight entered by the user to the main.
public static String getShipClass(Scanner keyboard) - Accepts the scanner object and prompts the user to enter the shipping class. Returns the shipping class to the main.
public static double calculateShipping(double weight, String shipClass) - Accepts the weight of the item to be shipped and the shipping class. Calculates the appropriate shipping charge and returns it to the main. This method will return a -1.0 if the shipping charge cannot be calculated due to an invalid shipping class.
public static void displayResults(String shipClass, double shipCost) - Accepts the shipping class and the shipping cost and displays the appropriate output to the user with (2) decimal places using a printf. This method is called from the main only if the calculateShipping method did not return a -1.
Code to fix:
import java.util.Scanner;
public class ProgrammerA {
public static void main(String[] args) { Scanner keyboard = new Scanner(system.in);
//get shipping weight int weight = getShipWeight(keyboard);
//get shipping class String shipClass = getShipClass();
//calc shipping cost int shipCost = calculateShipping(shipClass, weight);
if (shipCost == -1) { //was an invalid shipping class System.out.println("Invalid shipping class. Program cannot continue."); } else { displayResults(shipClass, shipCost); } } public static double getShipWeight(Scanner keyboard) { //get shipping weight System.out.println("Enter shipping weight:"); double weight = keyboard.nextInt(); return weight; }
public static String getShipClass() { Scanner keyboard = new Scanner(System.in); //scanner object to pass around
String shipClass;
//get shipping method System.out.println("Enter class (first, second, third): "); shipClass = keyboard.nextLine();
return shipClass; }
//calculate shipping charge //returns -1 if the shipping class was invalid public static void calculateShipping(double weight, String shipClass) {
int charge = -1; //assume invalid double weight=weight; if (shipClass == "FIRST"); { if (weight > 50) { charge = weight * 5; } else { charge = 50; } } else if (shipClass == "SECOND") { if (weight > 50) { charge = weight * 2; } } else { charge = 25; } else if (shipClass == "THIRD") { if (weight > 50) { charge = weight * 1; } else { charge = 15; } } return (charge); }
//display shipping charge public static String displayResult(String shipClass, double shipCost); { System.out.println("Shipping Method: " + shipClass.toUpperCase()); System.out.printf("Shipping Cost: $%.2f " + shipCost); System.out.println("Like this program? Hire me. " + "Email: ProgrammerA@coding.edu"); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
