Question
Main topics: User validated input Basic String Methods if & if - else Statements Loop Statements Program Specification: Write a Java program that emulates a
Main topics:
User validated input Basic
String Methods
if & if - else Statements
Loop Statements
Program Specification:
Write a Java program that emulates a simple calculator:
Declare and initialize a String variable (expression) to "0"
Declare and initialize an int variable (resultSoFar) to 0
Display the current expression to the screen
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 -> Add 1
2 -> Multiply by 2
3 -> 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
2. If ( Exit ) was NOT selected:
(a) Add the selected operation string to the end of the expression string: +1 or *2
(b) Add the character ( to the front of the expression string
(c) Add the character ) to the end of the expression string
(d) Display the current expression to the screen
(e) Update the current result of the arithmetic expression: ++resultSoFar or resultSoFar *= 2
Display the entire expression with its result.
Grading:
Performance Indicator [1] [2] [3]
Readability and documentation 1 2 2
Use of loop(s) operators 1 2 2
Functional requirements 2 3 4
Efficiency 1 2 2
Sample run(s):
0
---------------------------------------------
1 -> Add 1
2 -> Multiply by 2
3 -> Exit
---------------------------------------------
Choose an operator from the menu above : 1
---------------------------------------------
(0+1)
---------------------------------------------
1 -> Add 1
2 -> Multiply by 2
3 -> Exit
---------------------------------------------
Choose an operator from the menu above : 2
---------------------------------------------
((0+1)*2)
---------------------------------------------
1 -> Add 1
2 -> Multiply by 2
3 -> Exit
---------------------------------------------
Choose an operator from the menu above : 2
---------------------------------------------
(((0+1)*2)*2) ---------------------------------------------
1 -> Add 1
2 -> Multiply by 2
3 -> Exit
---------------------------------------------
Choose an operator from the menu above : 1
---------------------------------------------
((((0+1)*2)*2)+1)
---------------------------------------------
1 -> Add 1
2 -> Multiply by 2
3 -> Exit ---------------------------------------------
Choose an operator from the menu above : 2
---------------------------------------------
(((((0+1)*2)*2)+1)*2)
---------------------------------------------
1 -> Add 1
2 -> Multiply by 2
3 -> Exit ---------------------------------------------
Choose an operator from the menu above : 1
---------------------------------------------
((((((0+1)*2)*2)+1)*2)+1)
---------------------------------------------
1 -> Add 1
2 -> Multiply by 2
3 -> Exit
---------------------------------------------
Choose an operator from the menu above : 2
---------------------------------------------
(((((((0+1)*2)*2)+1)*2)+1)*2)
---------------------------------------------
1 -> Add 1
2 -> Multiply by 2
3 -> Exit
---------------------------------------------
Choose an operator from the menu above : 3
---------------------------------------------
(((((((0+1)*2)*2)+1)*2)+1)*2) = 22
---------------------------------------------
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started