Question

Write a Java program which allows the user to perform simple tasks on a calculator. A...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1


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 + "\n");
}
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 + "\n");
}
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 + "\n");
}
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 + "\n");
}
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 + "\n");
}
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 + "\n");
}
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 + "\n");
}
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 + "\n");
}
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 + "\n");
}
}

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

Add a comment
Know the answer?
Add Answer to:
Write a Java program which allows the user to perform simple tasks on a calculator. A...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write an assembler program that asks the user (as shown below) for two integers and a...

    Write an assembler program that asks the user (as shown below) for two integers and a single character representing the arithmetic operations: addition, subtraction, multiplication and integer division (displays both quotient and remainder). Perform the requested operation on the operands and display the result as specified below. Assume SIGNED NUMBERS. The program should not divide by 0! If the user attempts to divide by 0, display the error message: "Cannot divide by zero." If this error occurs, the program should...

  • 1) [5 pts] Build a simple calculator using switch statements which can perform the four operations...

    1) [5 pts] Build a simple calculator using switch statements which can perform the four operations +, -, *,/. Write a program that asks the user to enter two numbers/operands. Then prompt the user to enter the operator +,, ). Perform the corresponding operation and display the answer to user. The format Example of multiplication Microsoft Visual Studio Debug Console an operator (+, -, *, ): Enter two operands: 25.5 2.314 25.5 2.314597 2) [5 pts] Create a program to...

  • Write a java program that asks the user to enter 2 integers, obtains them from the...

    Write a java program that asks the user to enter 2 integers, obtains them from the user, determines if they are even or odd numbers and prints their sum, product, difference, and quotient (Division).

  • Write a program which: - prompts the user for 2 numerical inputs - stores their two...

    Write a program which: - prompts the user for 2 numerical inputs - stores their two inputs in variables as floats - Use a while in loop to ask a user for a valid operation: (if the user's input operation isn't one of those listed above, instead print a message telling them so, and continue asking for the valid operation) - prompts the user for an arithmetic operation ( + - * / %) - stores the user's input as...

  • Write a program which asks the user to enter an integer. Use switch statement to write...

    Write a program which asks the user to enter an integer. Use switch statement to write out the numeric word (such as ONE) if the user's input is 1; (see the sample run output for the usr input 2 or 3); otherwise, write OUT OF RANGE. Below are few sample runs: If the user enters a 1, the program will print: ONE TWO THREE Or, if the user enters a 2, the program will print: TWO THREE Or, if the...

  • 1. (sumFrom1.cpp) Write a program that will ask the user for a positive integer value. The...

    1. (sumFrom1.cpp) Write a program that will ask the user for a positive integer value. The program should use the for loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50. If the user enters a zero or negative number, a message should be given and the program should not continue (see Sample Run...

  • Write a C++ program that does the following: Asks the user to enter 3 integers, Obtains...

    Write a C++ program that does the following: Asks the user to enter 3 integers, Obtains the numbers from the user, Prints the largest number and then the smallest of the numbers, If the numbers are equal, prints the message: "These numbers are equal." Prints the sum, average, and product of the 3 numbers.

  • In Java Write a program that will ask the user for integers and print if the...

    In Java Write a program that will ask the user for integers and print if the integer is positive, negative or zero. The program should continue asking for integer until the user enters a negative value.

  • C++ coding answer 5. Write a program that asks the user for a positive integer value....

    C++ coding answer 5. Write a program that asks the user for a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50. Input Validation: Do not accept a negative starting number.

  • Write a PYTHON program that allows the user to navigate the lines of text in a...

    Write a PYTHON program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT