Question: n this assignment you are to start with the code base posted on the course s page on Canvas ( ListStack . java, ListNode.java, and
n this assignment you are to start with the code base posted on the courses page
on Canvas ListStackjava, ListNode.java, and StackDriver.java and modify the code
to implement additional functionality.
The only file you should be editing for this assignment is StackDriver.java which
already has certain functionality implemented in it such as evaluateExpr, and
isLegal. You must keep this functionality as is without editing.
Your program should implement the following functionality:
Implement a menudriven interface to be able to:
a Read an expression, check if its legal Use existing functionality
b Evaluate the inputted expression
i Only evaluate if the user inputted an expression, otherwise
indicate that no expression has been entered.
ii Once you evaluate this expression, its result should be printed
to the screen. Once evaluation is finished, the program must
require you to enter a new expression if this option is selected
again. But selecting this option should not prevent you from
performing part c with the same expression.
c Convert a fully parenthesized expression to postfix notation
i Only convert if the user inputted an expression, otherwise
indicate that no expression has been entered.
ii Once you convert the expression to postfix notation, you
should print the postfix notation to the screen. Once the
conversion is finished, the program must require you enter a
new expression if this option is selected again. But selecting
this option should not prevent you from performing part b
with the same expression.
d Evaluate a postfix notation expression
i In this option, your program should ask if the user would like
to enter a new postfix expression or use the postfix
expression that was converted in part c
ii If the user selects to use the postfix expression used in part c
then you need to verify that there is an expression to evaluate,
otherwise print an error message.
iii. If the user selects to enter a new postfix expression then make
sure that it is entered correctly, you have to do this manually,
no need to program the verification process.
e Quit
The menudriven interface should be an infinite loop that will only quit if the
user Quits the program, otherwise it should keep asking the user to select an
option.
The menu:
Enter a fully parenthesized expression.
Evaluate a fully parenthesized expression.
Convert a fully parenthesized expression to postfix.
Evaluate postfix
Quit
Enter your choice: StackDriver.java file
import java.util.Scanner; public class StackDriver public static void mainString args Scanner in new ScannerSystemin; System.out.printEnter any parenthesized expression: ; String s innextLine; check to see if it is a legal parenthesization ifisLegals System.out.printlns is a legal parenthesization"; now that you agree that it is a legally parenthesized expression, Let's go ahead and evaluate it Make sure you write two operands per operator. make sure that all the operands are digits, the code doesn't handle variables example evaluateExprs; else System.out.printlns is a not legal parenthesization"; go thru the string and every time it sees a it pushes it on the stack, and it will only pop a off the stack when it encounteres a public static boolean isLegalString str int i; ListStack s new ListStack; fori ; i strlength; i ifstrcharAti spush; else ifstrcharAti spop; return sisEmpty; Evaluate the expression, but the expression must be fully parenthesized in other words it will not evaluate but it will evaluate also it will not evaluate but will evaluate go thru the string and if you see a number push on s if you see an operator push on s if you see a pop operand and operand from stack in this order then pop the operator from stack and apply the operator to the operands push the result back on stack finally at the end print the result and pop it off of stack public static void evaluateExprString str int i; ListStack s new ListStack; ListStack s new ListStack; fori ; i strlength; i ifCharacterisDigitstrcharAti spushintstrcharAti; else ifstrcharAti strcharAti strcharAti strcharAti strcharAti spushstrcharAti; else ifstrcharAti int opnd opnd; char oper stopAndPop; opnd stopAndPop; opnd stop
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
