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

Develop a simple text-based calculator that queries the user to enter a value, a

ID: 3585910 • Letter: D

Question

Develop a simple text-based calculator that queries the user to enter a value, an arithmetic operator (+,-,*,/,%), and then another operand (value). If the user enters "q" at any time, the application should quit. If the user attempts division by zero (illegal operation in a computer), the application should prevent it. The application should perform the indicated arithmetic and display the results to the user. The application must be driven by a loop iand should employ a decision structure such as if statements or case statements.

Explanation / Answer

ArthimeticOperations.java

import java.util.Scanner;

public class ArthimeticOperations {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

char ch = 'y';

while(ch != 'q') {

System.out.println("Please enter the first integer number: ");

int a = scan.nextInt();

System.out.println("Please enter the second integer number: ");

int b = scan.nextInt();

System.out.println("Please enter the arithmetic operator: ");

char operator = scan.next().charAt(0);

double result = 0;

if(operator == '+'){

result = a + b;

}

else if(operator == '-'){

result = a - b;

}

else if(operator == '*'){

result = a * b;

}

else if(operator == '/'){

if(b != 0){

result = a / b;

}

else{

System.out.println("The denominator is zero.");

}

}

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

System.out.println("Do you want to continue (q to quit) or press any char: ");

ch = scan.next().charAt(0);

}

}

}

Output:

Please enter the first integer number:
2
Please enter the second integer number:
3
Please enter the arithmetic operator:
+
Result: 5.0
Do you want to continue (q to quit) or press any char:
w
Please enter the first integer number:
4
Please enter the second integer number:
3
Please enter the arithmetic operator:
-
Result: 1.0
Do you want to continue (q to quit) or press any char:
q