Project 1 JAVA Program Write a basic forth interpreter that does math. You can a
ID: 3878730 • Letter: P
Question
Project 1 JAVA Program
Write a basic forth interpreter that does math. You can also view this as writing a "simulator"
of an HP calculator (like the 10C). You must use a stack for all the math manipulations. You do
not have an = (equals sign). Along with the basic numeric operations, implement an operator
that will pop the top element of the stack and print it out. implement an operator that will
non-destructively print the stack. When you print the stack with this operator you must mark
the top of the stack.
Implement the 4 math operators:
+ pop the top 2 elements, push the result of adding them.
- pop the top 2 elements of the stack, subtract them, push the result
* pop the top 2 elements, multiply them, push the result
/ pop the top 2 elements, divide them, push the result
. pop the top element and display it
.S print the contents of the stack, mark the top. Don't pop anything!
Anytime you accept input as valid, print ok
Display errors like stack underfow and stack overfow. Tis type of error shouldn’t cause your
program to abort. Te stack underfow error should leave an empty stack. You can decide the
consequences of a stack overfow (either push the new number and delete the bottom number
of the stack or simply refuse to modify the stack)
You may use arrays to implement the stack, use 25 as an upper limit for the number of entries.
Tere are a lot of decisions you will need to make. You can decide how to print the stack and
whether to print top to bottom or bottom to top. You can print it out as a single line or one
entry to a line. You must document when you make one of these decision that will afect how
your “language” works.
Te stack does not contain zeros by default. Do not worry about numeric overfows.
Example:
52
ok
37
ok
5
ok
9
ok
13
ok
.S
Explanation / Answer
import java.util.Scanner; import java.util.Stack; public class PostFixEvaluator { public static double evaluate(String expression) { Scanner scanner = new Scanner(expression); Stack operands = new Stack(); while (scanner.hasNext()) { if (scanner.hasNextDouble()) { operands.push(scanner.nextDouble()); } else { double operand2 = operands.pop(); double operand1 = operands.pop(); String operator = scanner.next(); switch (operator) { case "+" : operands.push(operand1 + operand2); break; case "-" : operands.push(operand1 - operand2); break; case "*" : operands.push(operand1 * operand2); break; case "/" : operands.push(operand1 / operand2); break; case "^" : operands.push(Math.pow(operand1, operand2)); break; } } } scanner.close(); return operands.pop(); } public static void main(String[] args) { System.out.println("Enter a postfix expression:"); Scanner scanner = new Scanner(System.in); String inputStr = scanner.nextLine(); scanner.close(); System.out.println(PostFixEvaluator.evaluate(inputStr)); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.