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

Write a Java program which allows the user to perform simple tasks on a calculat

ID: 3875670 • Letter: W

Question

Write a Java program which allows the user to perform simple tasks on a calculator. A series of methods allows the user to select an operation to perform and then enter operands. The first method displays a menu, giving the user the choice of typing in any one of the following:

+, -, *, /, or % representing the usual arithmetic operators (Each operation should be done on numbers negative(-) to positive(+), positive(+) to negative(-), negative(-) to negative(-), and positive(+) to positive(+)

A representing the average of two numbers

X representing the maximum of two numbers

M representing the minimum of two numbers

S representing the square of a number

Q indicating the user wants to quit the program

The program reads the user's response into a variable of type char. Using a switch statement, the program determines which method to call to process the user's request. For example, if the user enters +, a method is called which prompts the user to enter two integers. The method then finds the sum of the two integers and the method prints the results of the operation. If the user enters X, a method asks for two integers and finds the larger of the two. If the user enters S, a method asks for one value and finds the square of that value. If the user enters Q, the program stops.

For each calculation performed, the method prints the operation requested, the user's original input, and the result.

Note: All output must be sent to a file

Sample Output:

Operation: addition augend: 25

addend: 35

sum: 60

Explanation / Answer


Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
The project should be refreshed to see the output file generated.

Calculator.java
==============
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Calculator {
private static Scanner keyboard = new Scanner(System.in);
private static PrintWriter outFile;
private static void menu()
{
String choice = " ";
while(!choice.equalsIgnoreCase("Q"))
{
System.out.println("A - average of two numbers");
System.out.println("X - maximum of two numbers");
System.out.println("M - minimum of two numbers");
System.out.println("S - square of a number");
System.out.println("Q - quit the program");
System.out.println("Enter any of the letters mentioned above or arithmetic operators(+ - / * %)");
choice = keyboard.next().toUpperCase();
switch(choice)
{
case "+":
add();
break;
case "-":
subtract();
break;
case "*":
multiply();
break;
case "/":
divide();
break;
case "%":
modulo();
break;
case "A":
average();
break;
case "X":
maximum();
break;
case "M":
minimum();
break;
case "S":
square();
break;
case "Q":
break;
default:
System.out.println("Invalid choice of operation");
}
}
}
public static void main(String[] args) {
String filename;
System.out.println("Enter output filename: ");
filename = keyboard.next();
try {
outFile = new PrintWriter(new File(filename));
menu();
outFile.close();
System.out.println("Output written to output file " + filename);
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
private static void add()
{
int n1, n2, ans;
System.out.println("Addition");
System.out.print("Enter number1: ");
n1 = keyboard.nextInt();
System.out.print("Enter number2: ");
n2 = keyboard.nextInt();
ans = n1 + n2;
System.out.println("Sum = " + ans);
//write to output file
outFile.println("Operation: Addition");
outFile.println("Number1: " + n1);
outFile.println("Number2: " + n2);
outFile.println("Sum: " + ans + " ");
}
private static void subtract()
{
int n1, n2, ans;
System.out.println("Subtraction");
System.out.print("Enter number1: ");
n1 = keyboard.nextInt();
System.out.print("Enter number2: ");
n2 = keyboard.nextInt();
ans = n1 - n2;
System.out.println("Difference = " + ans);
//write to output file
outFile.println("Operation: Subtraction");
outFile.println("Number1: " + n1);
outFile.println("Number2: " + n2);
outFile.println("Difference: " + ans + " ");
}
private static void multiply()
{
int n1, n2, ans;
System.out.println("Multiplication");
System.out.print("Enter number1: ");
n1 = keyboard.nextInt();
System.out.print("Enter number2: ");
n2 = keyboard.nextInt();
ans = n1 * n2;
System.out.println("Product = " + ans);
//write to output file
outFile.println("Operation: Multiplication");
outFile.println("Number1: " + n1);
outFile.println("Number2: " + n2);
outFile.println("Product: " + ans + " ");
}
private static void divide()
{
int n1, n2, ans;
System.out.println("Division");
System.out.print("Enter number1: ");
n1 = keyboard.nextInt();
System.out.print("Enter number2: ");
n2 = keyboard.nextInt();
ans = n1 / n2;
System.out.println("Quotient = " + ans);
//write to output file
outFile.println("Operation: Division");
outFile.println("Number1: " + n1);
outFile.println("Number2: " + n2);
outFile.println("Quotient: " + ans + " ");
}
private static void modulo()
{
int n1, n2, ans;
System.out.println("Modulo");
System.out.print("Enter number1: ");
n1 = keyboard.nextInt();
System.out.print("Enter number2: ");
n2 = keyboard.nextInt();
ans = n1 % n2;
System.out.println("Remainder = " + ans);
//write to output file
outFile.println("Operation: Modulo");
outFile.println("Number1: " + n1);
outFile.println("Number2: " + n2);
outFile.println("Remainder: " + ans + " ");
}
private static void average()
{
int n1, n2;
double ans;
System.out.println("Average");
System.out.print("Enter number1: ");
n1 = keyboard.nextInt();
System.out.print("Enter number2: ");
n2 = keyboard.nextInt();
ans = (n1 + n2)/2.0;
System.out.println("Average = " + ans);
//write to output file
outFile.println("Operation: Average");
outFile.println("Number1: " + n1);
outFile.println("Number2: " + n2);
outFile.println("Average: " + ans + " ");
}
private static void maximum()
{
int n1, n2, ans;
System.out.println("Maximum");
System.out.print("Enter number1: ");
n1 = keyboard.nextInt();
System.out.print("Enter number2: ");
n2 = keyboard.nextInt();
if(n1 > n2)
ans = n1;
else
ans = n2;
System.out.println("Maximum = " + ans);
//write to output file
outFile.println("Operation: Maximum");
outFile.println("Number1: " + n1);
outFile.println("Number2: " + n2);
outFile.println("Maximum: " + ans + " ");
}
private static void minimum()
{
int n1, n2, ans;
System.out.println("Minimum");
System.out.print("Enter number1: ");
n1 = keyboard.nextInt();
System.out.print("Enter number2: ");
n2 = keyboard.nextInt();
if(n1 < n2)
ans = n1;
else
ans = n2;
System.out.println("Minimum = " + ans);
//write to output file
outFile.println("Operation: Minimum");
outFile.println("Number1: " + n1);
outFile.println("Number2: " + n2);
outFile.println("Minimum: " + ans + " ");
}
private static void square()
{
int n1, ans;
System.out.println("Square");
System.out.print("Enter a number: ");
n1 = keyboard.nextInt();
ans = n1 * n1;
System.out.println("Square = " + ans);
//write to output file
outFile.println("Operation: Square");
outFile.println("Number: " + n1);
outFile.println("Square: " + ans + " ");
}
}

output
Enter output filename:
output.txt
A - average of two numbers
X - maximum of two numbers
M - minimum of two numbers
S - square of a number
Q - quit the program
Enter any of the letters mentioned above or arithmetic operators(+ - / * %)
+
Addition
Enter number1: 4
Enter number2: 5
Sum = 9
A - average of two numbers
X - maximum of two numbers
M - minimum of two numbers
S - square of a number
Q - quit the program
Enter any of the letters mentioned above or arithmetic operators(+ - / * %)
%
Modulo
Enter number1: 8
Enter number2: 3
Remainder = 2
A - average of two numbers
X - maximum of two numbers
M - minimum of two numbers
S - square of a number
Q - quit the program
Enter any of the letters mentioned above or arithmetic operators(+ - / * %)
A
Average
Enter number1: 5
Enter number2: 9
Average = 7.0
A - average of two numbers
X - maximum of two numbers
M - minimum of two numbers
S - square of a number
Q - quit the program
Enter any of the letters mentioned above or arithmetic operators(+ - / * %)
m
Minimum
Enter number1: 4
Enter number2: -2
Minimum = -2
A - average of two numbers
X - maximum of two numbers
M - minimum of two numbers
S - square of a number
Q - quit the program
Enter any of the letters mentioned above or arithmetic operators(+ - / * %)
x
Maximum
Enter number1: 3
Enter number2: 5
Maximum = 5
A - average of two numbers
X - maximum of two numbers
M - minimum of two numbers
S - square of a number
Q - quit the program
Enter any of the letters mentioned above or arithmetic operators(+ - / * %)
s
Square
Enter a number: -5
Square = 25
A - average of two numbers
X - maximum of two numbers
M - minimum of two numbers
S - square of a number
Q - quit the program
Enter any of the letters mentioned above or arithmetic operators(+ - / * %)
q
Output written to output file output.txt




output file: output.txt
=================
Operation: Addition
Number1: 4
Number2: 5
Sum: 9
Operation: Modulo
Number1: 8
Number2: 3
Remainder: 2
Operation: Average
Number1: 5
Number2: 9
Average: 7.0
Operation: Minimum
Number1: 4
Number2: -2
Minimum: -2
Operation: Maximum
Number1: 3
Number2: 5
Maximum: 5
Operation: Square
Number: -5
Square: 25

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