Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

StackListBased class: import java.util.LinkedList; public class StackListBased { private LinkedList items; private int top; public void createStack() { items = new LinkedList(); top = -1;

image text in transcribed

image text in transcribed

StackListBased class:

import java.util.LinkedList;

public class StackListBased { private LinkedList items; private int top;

public void createStack() { items = new LinkedList(); top = -1; }

public boolean isEmpty() { return top == -1; }

public void popAll() { while (!isEmpty()) { items.remove(top--); } }

public void push(int elem) { items.add(elem); top ++; }

public void pop() { if (isEmpty()) { System.out.println("Stack is empty!!"); } else { System.out.println("Popping : " + items.remove(top --)); } }

public int peek() { if (isEmpty()) { System.out.println("Stack is empty!!"); return -1; } else { System.out.println("Popping : " + items.get(top)); return items.get(top); } } }

Design another class named InfixCalculator to implement an infix calculator using your previously implemented InfixCalculator class must accept infix expressions from the user and evaluate them with method evaluateInfix. This method will first convert the infix expression to postfix expression (method convertPostfix), and then evaluate the resulting postfix expression (method getPostfix). Use only the operators +, -, *, and /. You can assume that the infix expression is syntactically correct and that the unary operators are illegal. However, the infix expression should class StackListBased. The a. allow for any type of spacing between operands, operators, and parentheses b. allow for multi-digit integer operands The client program (exterior to your class) will read the infix expression to evaluate from the keyboard as follows: Enter the infix expression to evaluate: Suppose that the user enters the expression: (10 3 * 4/ 6) The client program will then proceed to create an object of your class with the user- entered expression and evaluate it the method evaluateInfix() The output for some example infix operations should appear as follows: infix: (10 3 * 4 / 6) postfix: 10 3 4* 6 / + resuIT: 12 infix: 12*3 - 4 (18 / 6) postfix: 12 3 * 4 - 18 6/+ result: 35 infix: 35 42* 17 /2 10 postfix: 35 42 17 * 2 / - 10 -+ result: -312 infix: 3 * (4 + 5) postfix: 3 4 5 * result: 27 infix: 3 * 17 - (5+2))/(2+3) postfix: 3 17 5 2+ - * 2 3/ result: 6 Design another class named InfixCalculator to implement an infix calculator using your previously implemented InfixCalculator class must accept infix expressions from the user and evaluate them with method evaluateInfix. This method will first convert the infix expression to postfix expression (method convertPostfix), and then evaluate the resulting postfix expression (method getPostfix). Use only the operators +, -, *, and /. You can assume that the infix expression is syntactically correct and that the unary operators are illegal. However, the infix expression should class StackListBased. The a. allow for any type of spacing between operands, operators, and parentheses b. allow for multi-digit integer operands The client program (exterior to your class) will read the infix expression to evaluate from the keyboard as follows: Enter the infix expression to evaluate: Suppose that the user enters the expression: (10 3 * 4/ 6) The client program will then proceed to create an object of your class with the user- entered expression and evaluate it the method evaluateInfix() The output for some example infix operations should appear as follows: infix: (10 3 * 4 / 6) postfix: 10 3 4* 6 / + resuIT: 12 infix: 12*3 - 4 (18 / 6) postfix: 12 3 * 4 - 18 6/+ result: 35 infix: 35 42* 17 /2 10 postfix: 35 42 17 * 2 / - 10 -+ result: -312 infix: 3 * (4 + 5) postfix: 3 4 5 * result: 27 infix: 3 * 17 - (5+2))/(2+3) postfix: 3 17 5 2+ - * 2 3/ result: 6

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions