Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, Can anyone please help me debug this calculator program? It is supposed to do four math operations and contain three different menu options for

Hello, Can anyone please help me debug this calculator program? It is supposed to do four math operations and contain three different menu options for the user to select. Thank you much.

import java.util.*; //this was needed in order to use the scanner for input.

public class Calculator {

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

private static boolean processSelection(String selection, Scanner console) { boolean done = false; if (selection.equalsIgnoreCase("Q")){ //result = displayCalculatorInstructions(console); } if (selection.equalsIgnoreCase("U")){ result = caluculateResults(console); } else if (selection.equalsIgnoreCase("H")){ //nothing to do here...let the code loop around one more time } else { System.out.println("Incorrect entry...try again!"); } } else { done = true; } return done;

//NOTE: the method header below is fine -- it contains NO BUG! private static void caluculateResults(Scanner console) { displayCalculatorInstructions(); double operand1 = console.nextDouble(); char operator = console.next().charAt(0); double operand2 = console.nextDouble(); int result = 0; //set as an integer. boolean isOperatorValid = true; if (operator == '+'){ result = (int) (operand1 + operand2);// changed to integer for if/else if statements } else if (operator == '-'){ result = (int) (operand1 - operand2); //added subtraction } else if (operator == '*'){ result = (int) (operand1 * operand2); } else if (operator == '/'){ if (operand2 != 0.0){ result = (int) (operand1/operand2); //changed to division symbol } else {result = (int) Double.NaN; //changed from double result equals... } } else if (operator == '^'){ result = (int) Math.pow(operand1, operand2); //added integer } else { isOperatorValid = false; } if (isOperatorValid){ System.out.println("operand1+", "+operator+", "+operand2+", "=", "result"); //would be nice use printf to control the precision of result } }

private static void displayCalculatorInstructions() { System.out.println("Enter a mathematical expression to evaluate"); System.out.println("Valid operations are: +, -, /, *, ^ for power"); System.out.println("Expression are entered with spaces between the values and operator"); System.out.println("Here is the valid format:"); System.out.println("\t"); System.out.print("Your expression: "); }

private static String getUsersSelection(Scanner console) { String selection = console.toString(); //added toString return selection; }

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

}

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

Joe Celkos Data And Databases Concepts In Practice

Authors: Joe Celko

1st Edition

1558604324, 978-1558604322

More Books

Students also viewed these Databases questions

Question

What does the start( ) method defined by Thread do?

Answered: 1 week ago

Question

Explain the function and purpose of the Job Level Table.

Answered: 1 week ago