Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Answer in java please Scenario: Write a calculator program that keep reading ope

ID: 3766902 • Letter: A

Question

Answer in java please

Scenario: Write a calculator program that keep reading operations and double numbers from user, and print the result based on the chosen operation using while loop. The loop must stop if the user enters Q letter NOTE: no need to write two classes. Typical run of the program: Console test1 [Java Application] CAProgram Files Javaljre1.8.0 Enter an operation (+,-, *,/), to quit: + Enter your first number: 2.5 Enter your second number: 2 Result= 4 . 5 Enter an operation (+,-,*,/, to quit: - Enter your first number: 5 Enter your second number: 4 Result= 1 . Enter an operation (+,-,*,l), to quit: Enter your first number:2 Ente Your second number: Result- 8.0 Enter an operation (+,-,*,/), to quit: / Enter your first number: 10 Enter your second number: 2 Result-5.0 Enter an operation (+,,*,/, Q to quit: Q You calculator has been ended!

Explanation / Answer


package basicCalculator;

import java.util.Scanner;

public class Calculation

{

private float firstOperannd;

private float secondOperannd;

private float result;

private int operator;

private Scanner input = new Scanner(System.in);

private boolean exitCalculator = false;

public void startCalculator()

{

while (!exitCalculator)

{

System.out.println("Enter + for addition "+"Enter - for subtraction "+"Enter * for multiplication "+"Enter / for division "+"Enter Q for Quit: ");

operator = input.nextInt();

switch (operator)

{

case '+':

result = add();

System.out.println("Result is " + result);

break;

case '-':

  result = subtract();

  System.out.println("Result is " + result);

  break;

case '*':

  result = multiply();

  System.out.println("Result is " + result);

break;

case '/':

  result = divide();

  System.out.println("Result is " + result);

break;

case 'Q':

exitCalculator = true;

System.out.println("Calculator program Terminated ");

break;

default:

  System.out.println("Please provide proper input ");

}

}

}

private float add()

{

System.out.println("Enter first number : ");

firstOperannd = input.nextInt();

System.out.println("Enter second number : ");

secondOperannd = input.nextInt();

return firstOperannd + secondOperannd;

}

private float subtract()

{

System.out.println("Enter first number : ");

firstOperannd = input.nextInt();

System.out.println("Enter second number : ");

secondOperannd = input.nextInt();

return firstOperannd - secondOperannd;

}

private float multiply()

{

System.out.println("Enter first number : ");

firstOperannd = input.nextInt();

System.out.println("Enter second number : ");

secondOperannd = input.nextInt();

return firstOperannd * secondOperannd;

}

private float divide()

{

System.out.println("Enter first number : ");

firstOperannd = input.nextInt();

System.out.println("Enter second number : ");

secondOperannd = input.nextInt();

return firstOperannd / secondOperannd;

}

}

There are many point that need to be explained here so be patient. First lets get our code up and running.

so create next class class class ProgramLauncher and insert the code below

package basicCalculator;

public

public static void main(string[] args)

{

Calculation calculator = new Calculation();

calculator .startCalculator();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote