Java Programming Main topics: User validated input, Basic String Methods, if & i
ID: 3676577 • Letter: J
Question
Java Programming
Main topics: User validated input, Basic String Methods, if & if - else Statements, Loop Statements
Please just focus on these main topics. We haven't learned anything past these topics. It can not have any complex things in it. Thanks.
Program Specification
Write a Java program that emulates a simple calculator:
• Declare and initialize a String variable (expression) to the empty string ""
• Prompt the user to input the left operand and read it into a double variable (lOperand)
• Declare and initialize a double variable (resultSoFar) to lOperand’s value
• Concatenate lOperand’s value to the expression string
• Repeat the following until the ( Exit ) option is selected:
1. Display a menu which gives the user the choice of the following menu options:
1 -> Multiplication
2 -> Division
3 -> Addition
4 -> Subtraction
5 -> Exit
and uses a user validation loop to read their response into a String variable.
Note: You must repeatedly get the users response until it is exactly one of the characters 1 2 3 4 5
2. If ( Exit ) was NOT selected:
(a) Concatenate the operator selected to the expression string
(b) Prompt the user to input the right operand and read it into a double variable (rOperand)
(c) Concatenate rOperand’s value to the expression string
(d) Compute the result of the arithmetic expression, based on the current value of resultSoFar (left operand), the operator indicated, and the value of rOperand. Store this result back into the variable resultSoFar
• Display the entire expression with its result.
Explanation / Answer
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
// to read user input
Scanner sc = new Scanner(System.in);
String expression = "";
double lOperand;System.out.print("Enter the left operand: ");
lOperand = sc.nextDouble();
double resultSoFar = lOperand;
expression = expression+lOperand;
double rOperand;
int option;
while(true){
System.out.println("Enter - 1. Mutiplication");
System.out.println(" 2. Division");
System.out.println(" 3. Addition");
System.out.println(" 4. Subtraction");
System.out.println(" 5. Exit");
// reading option
option = sc.nextInt();
if(option>5 || option < 1)
System.out.println("Invalid option");
else if(option == 1){
expression = expression+"*";
System.out.print("Enter right operand: ");
rOperand = sc.nextDouble();
//appending in expression
expression = expression+rOperand;
//updating result so far
resultSoFar = resultSoFar*rOperand;
}else if(option == 2){
expression = expression+"/";
System.out.print("Enter right operand: ");
rOperand = sc.nextDouble();
//appending in expression
expression = expression+rOperand;
//updating result so far
resultSoFar = resultSoFar/rOperand;
}else if(option == 3){
expression = expression+"+";
System.out.print("Enter right operand: ");
rOperand = sc.nextDouble();
//appending in expression
expression = expression+rOperand;
//updating result so far
resultSoFar = resultSoFar+rOperand;
}else if(option == 4){
expression = expression+"-";
System.out.print("Enter right operand: ");
rOperand = sc.nextDouble();
//appending in expression
expression = expression+rOperand;
//updating result so far
resultSoFar = resultSoFar-rOperand;
}else{
break; // existing from loop
}
}
System.out.println(expression+": "+resultSoFar);
}
}
/*
Output:
Enter the left operand: 2
Enter - 1. Mutiplication
2. Division
3. Addition
4. Subtraction
5. Exit
1
Enter right operand: 2
Enter - 1. Mutiplication
2. Division
3. Addition
4. Subtraction
5. Exit
2
Enter right operand: 2
Enter - 1. Mutiplication
2. Division
3. Addition
4. Subtraction
5. Exit
3
Enter right operand: 2
Enter - 1. Mutiplication
2. Division
3. Addition
4. Subtraction
5. Exit
5
2.0*2.0/2.0+2.0: 4.0
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.