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 (ListStack.java, 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:
1. Implement a menu-driven 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 post-fix 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 post-fix notation, you
should print the post-fix 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 post-fix notation expression
i. In this option, your program should ask if the user would like
to enter a new post-fix expression or use the post-fix
expression that was converted in part c.
ii. If the user selects to use the post-fix 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 post-fix expression then make
sure that it is entered correctly, you have to do this manually,
no need to program the verification process.
e. Quit
2. The menu-driven 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:
1. Enter a fully parenthesized expression.
2. Evaluate a fully parenthesized expression.
3. Convert a fully parenthesized expression to post-fix.
4. Evaluate post-fix
5. Quit
Enter your choice: StackDriver.java file
import java.util.Scanner; public class StackDriver { public static void main(String []args){ Scanner in = new Scanner(System.in); System.out.print("Enter any parenthesized expression: "); String s = in.nextLine(); // check to see if it is a legal parenthesization if(isLegal(s)){ System.out.println(s +" 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 (((1+2)*3)-((5+4)/3)) evaluateExpr(s); }else System.out.println(s +" 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 isLegal(String str){ int i; ListStack s = new ListStack(); for(i =0; i < str.length(); i++) if(str.charAt(i)=='(') s.push('('); else if(str.charAt(i)==')') s.pop(); return s.isEmpty(); }// Evaluate the expression, but the expression must be fully parenthesized // in other words it will not evaluate 9+5, but it will evaluate (9+5)// also it will not evaluate (8*4)+7, but will evaluate ((8*4)+7)// go thru the string and // if you see a number push on s1,// if you see an operator push on s2// if you see a )// pop operand2 and operand1 from stack 1(in this order)// then pop the operator from stack 2 and // apply the operator to the operands // push the result back on stack 1// finally at the end print the result and pop it off of stack 1. public static void evaluateExpr(String str){ int i; ListStack s1= new ListStack(); ListStack s2= new ListStack(); for(i =0; i < str.length(); i++){ if(Character.isDigit(str.charAt(i))) s1.push((int)(str.charAt(i)-'0')); else if(str.charAt(i)=='+'|| str.charAt(i)=='-'|| str.charAt(i)=='*'|| str.charAt(i)=='/'|| str.charAt(i)=='%') s2.push(str.charAt(i)); else if(str.charAt(i)==')'){ int opnd1, opnd2; char oper = s2.topAndPop(); opnd2= s1.topAndPop(); opnd1= s1.top

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!