Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/*This program is meant to perform simple mathematical calculations // However, it has at least a dozen bugs (syntactical and/or logical). */ Your job is

/*This program is meant to perform simple mathematical calculations // However, it has at least a dozen bugs (syntactical and/or logical). */ Your job is to debug the program so that it works as intended.

public class Calc { public static void main(Sting[] args) { boolean done = false; Scanner console = new Scanner (System.in); while (!done){ displayMenu(); selection = getUserSelection(console); done = processSelection(selection, console); } System.out.println("Thank you for using this program. Goodbye."); }

private static boolean processSelection(String selection, Scanner console) { boolean done = false; if (!selection.equalsIgnoreCase("E")){ if (selection.equalsIgnoreCase("U")){ calculateResults(console); } else if (selection.equalsIgnoreCase("H")){ //No need to write a Help Menu at this time, but logic and program execution // should still operate normally and display original menu after message prints System.out.print("Help Menu is under construction, " + "hence not available at this time."); } else { System.out.println("Incorrect entry...please try again!"); } } else { done = true; } return done; } private static void calculateResults(Scanner console) { //displayCalculatorInstructions(); double operand1 = console.nextDouble(); char operator = console.next().charAt(0); double operand2 = console.next(); double result = 0.0; boolean isOperatorValid = true; if (operator == '+'){ result = operand1 + operand2; } else if (operator == '-'){ result = operand1 - operand2; } else if (operator == '*'){ result = operand1 + operand2; } else if (operator == '/'){ if (operand2 != 0.0){ result = operand1/operand2 } else { result = Double.NaN; //Returns Not a Number } } else if (operator == '^'){ result = Math.pow(operand2, operand1); } else { isOperatorValid = false; } if (isOperatorValid){ System.out.println(operand1 +" "+operator+""+operand2+" = " + result); //One could also use printf to control the precision of result, but not necessary now. } }

private static void displayCalculatorInstructions() { System.out.println("Enter a mathematical expression to evaluate."); System.out.println("Valid operations are: +, -, /, *, ^ (for exponents)."); System.out.println("Input expression using spaces between the operands (numbers) " + "and the operator, followed by Enter."); System.out.println("Here is the valid format:"); System.out.println("\t "); System.out.print("Your expression: "); } //The method header for getUserSelection has no errors. private static String getUserSelection(Scanner console) { String selection = console.nextInt(); return selection; }

private static void displayMenu() { System.out.println("Enter one these options:"); System.out.println("\tH for Help"); System.out.println("\tC for using calculator"); System.out.println("\tE for exiting this program"); System.out.print("Your selection: "); } }

Can you try to debug the code? And comments wherre and how to fix it?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions

Question

Knowledge of process documentation (process flow charting)

Answered: 1 week ago