May you help revise my code please? The Assignment: Build a basic console based calculator program. The
Question:
May you help revise my code please?
The Assignment:
Build a basic console based calculator program.
The calculator can operate in two modes: Standard and Scientific modes.
The Standard mode will allow the user to perform the following operations:
(+, -, *, /) add, subtract, multiply, and divide
The Scientific mode will allow the user to perform the same functionality as the Standard
add, subtract, multiply, and divide (+, -, *, / ) plus the following:
sin x, cos x, tan x. (sin x, cos x, tan x)
The calculator should be able to perform addition, subtraction, multiplication, and division of two or more numbers but perform sin x, cos x, and tan x of one number only (the number is in radians).
1. The calculator program will first ask the user for the mode to operate in (Standard or Scientific)
Sample Output:
Enter the calculator mode: Standard/Scientific?
Scientific
2. The program should then ask the user for the operation to execute (+, -, *, /, sin x, cos x, tan x)
Sample Output for Standard mode:
The calculator will operate in standard mode.
Enter '+' for addition, '-' for subtractions, '*' for multiplication, '/' for division
Sample Output for Scientific mode:
Enter '+' for addition, '-' for subtractions, '*' for multiplication, '/' for division, 'sin' for sin x, 'cos' for cos x, 'tan' for tan x:
sin
2a. If the user enters an invalid operator, output a message telling the user the input is invalid and re-prompt the user for the operation again.
Sample Output for Scientific mode:
Enter '+' for addition, '-' for subtractions, '*' for multiplication, '/' for division, 'sin' for sin x, 'cos' for cos x, 'tan' for tan x:
division
Invalid operator division
Enter '+' for addition, '-' for subtractions, '*' for multiplication, '/' for division, 'sin' for sin x, 'cos' for cos x, 'tan' for tan x:
+
3. In order to know how many times the user will need to perform the operation, prompt the user for the number of floating point values they want to enter (All numbers in this program are double), then ask the user to enter the numbers.
Sample Output:
How many numbers do you want to add?:
3
Enter 3 numbers:
4
35
9
In this example the calculator will calculate 4 + 35 + 9. The calculated result will be 48.
The calculator should be able to perform the following operations:
2+3 = 5
2+6+1+1+1 = 11
1-2-90 = -91
10*2*3 = 60
10/2/5 = 1
cos(0) = 1
sin(0) = 0
tan(0) = 0
Note: This calculator does NOT support multiple operations of varying operators in the expressions like: 10+2-8
Note: Multiple numbers are only acceptable for the operations of (+, -, *, / ) not for (sin,cos,tan)
4. Display the result
Sample Output:
Result: 50.0
5. Finally, output the result to the user and ask the user if he/she wants to start over.
Sample Output:
Do you want to start over? (Y/N)
Y
Full Sample Output:
Enter the calculator mode: Standard/Scientific?
Scientific
Enter '+' for addition, '-' for subtractions, '*' for multiplication, '/' for division, 'sin' for sin x, 'cos' for cos x, 'tan' for tan x:
addition
Invalid operator addition
Enter '+' for addition, '-' for subtractions, '*' for multiplication, '/' for division, 'sin' for sin x, 'cos' for cos x, 'tan' for tan x:
+
How many numbers do you want to add?:
3
Enter 3 numbers:
4
35
9
Result: 48.0
Do you want to start over? (Y/N)
Y
Enter the calculator mode: Standard/Scientific?
Scientific
Enter '+' for addition, '-' for subtractions, '*' for multiplication, '/' for division, 'sin' for sin x, 'cos' for cos x, 'tan' for tan x:
cos
Enter a number in radians to find the cosine
0
Result: 1
Do you want to start over? (Y/N)
N
Goodbye
"
My code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the calculator mode: Standard/Scientific?");
String mode = input.next();
if (mode.equalsIgnoreCase("standard")) {
System.out.println("The calculator will operate in standard mode.");
System.out.println("Enter '+' for addition, '-' for subtractions, '*' for multiplication, '/' for division");
String operator = input.next();
if (operator.equals("+")) {
System.out.println("How many numbers do you want to add?");
int num = input.nextInt();
double sum = 0;
for (int i = 0; i < num; i++) {
System.out.println("Enter number " + (i + 1));
double number = input.nextDouble();
sum = sum + number;
}
System.out.println("Result: " + sum);
} else if (operator.equals("-")) {
System.out.println("How many numbers do you want to subtract?");
int num = input.nextInt();
double difference = 0;
for (int i = 0; i < num; i++) {
System.out.println("Enter number " + (i + 1));
double number = input.nextDouble();
if (i == 0) {
difference = number;
} else {
difference = difference - number;
}
}
System.out.println("Result: " + difference);
} else if (operator.equals("*")) {
System.out.println("How many numbers do you want to multiply?");
int num = input.nextInt();
double product = 1;
for (int i = 0; i < num; i++) {
System.out.println("Enter number " + (i + 1));
double number = input.nextDouble();
product = product * number;
}
System.out.println("Result: " + product);
} else if (operator.equals("/")) {
System.out.println("How many numbers do you want to divide?");
int num = input.nextInt();
double quotient = 0;
for (int i = 0; i < num; i++) {
System.out.println("Enter number " + (i + 1));
double number = input.nextDouble();
if (i == 0) {
quotient = number;
} else {
quotient = quotient / number;
}
}
System.out.println("Result: " + quotient);
} else {
System.out.println("Invalid operator " + operator);
}
} else if (mode.equalsIgnoreCase("scientific")) {
System.out.println("The calculator will operate in scientific mode.");
System.out.println("Enter '+' for addition, '-' for subtractions, '*' for multiplication, '/' for division, 'sin' for sin x, 'cos' for cos x, 'tan' for tan x:");
String operator = input.next();
if (operator.equals("+")) {
System.out.println("How many numbers do you want to add?");
int num = input.nextInt();
double sum = 0;
for (int i = 0; i < num; i++) {
System.out.println("Enter number " + (i + 1));
double number = input.nextDouble();
sum = sum + number;
}
System.out.println("Result: " + sum);
} else if (operator.equals("-")) {
System.out.println("How many numbers do you want to subtract?");
int num = input.nextInt();
double difference = 0;
for (int i = 0; i < num; i++) {
System.out.println("Enter number " + (i + 1));
double number = input.nextDouble();
if (i == 0) {
difference = number;
} else {
difference = difference - number;
}
}
System.out.println("Result: " + difference);
} else if (operator.equals("*")) {
System.out.println("How many numbers do you want to multiply?");
int num = input.nextInt();
double product = 1;
for (int i = 0; i < num; i++) {
System.out.println("Enter number " + (i + 1));
double number = input.nextDouble();
product = product * number;
}
System.out.println("Result: " + product);
} else if (operator.equals("/")) {
System.out.println("How many numbers do you want to divide?");
int num = input.nextInt();
double quotient = 0;
for (int i = 0; i < num; i++) {
System.out.println("Enter number " + (i + 1));
double number = input.nextDouble();
if (i == 0) {
quotient = number;
} else {
quotient = quotient / number;
}
}
System.out.println("Result: " + quotient);
} else if (operator.equals("sin")) {
System.out.println("Enter a number in radians to find the sine");
double radians = input.nextDouble();
double result = Math.sin(radians);
System.out.println("Result: " + result);
} else if (operator.equals("cos")) {
System.out.println("Enter a number in radians to find the cosine");
double radians = input.nextDouble();
double result = Math.cos(radians);
System.out.println("Result: " + result);
} else if (operator.equals("tan")) {
System.out.println("Enter a number in radians to find the tangent");
double radians = input.nextDouble();
double result = Math.tan(radians);
System.out.println("Result: " + result);
} else {
System.out.println("Invalid operator " + operator);
}
} else {
System.out.println("Invalid mode " + mode);
}
System.out.println("Do you want to start over? (Y/N)");
String response = input.next();
if (response.equalsIgnoreCase("y")) {
main(args);
} else {
System.out.println("Goodbye");
}
}
}
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill