Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm having trouble with PostfixTester.java and PostfixEvaluator.java. I think my LinkedStack.java is correct but it may need fixing. All the code for PostfixTester and PostfixEvaluator

image text in transcribed

I'm having trouble with PostfixTester.java and PostfixEvaluator.java. I think my LinkedStack.java is correct but it may need fixing. All the code for PostfixTester and PostfixEvaluator is not required but some of it may help with the solution.

**PLEASE MAKE SURE OUTPUT IS THE SAME AS EXAMPLE OUPUT**

PostfixTester.java

import java.util.Scanner;

public class PostfixTester { /** * Reads and evaluates multiple postfix expressions. */ public static void main(String[] args) { String expression, again; int result; Scanner in = new Scanner(System.in); do { PostfixEvaluator evaluator = new PostfixEvaluator(); System.out.println("Enter a valid post-fix expression one token " + "at a time with a space between each token (e.g. 5 4 + 3 2 1 - + *)"); System.out.println("Each token must be an integer or an operator (+,-,*,/)"); expression = in.nextLine();

result = evaluator.evaluate(expression); System.out.println(); System.out.println("That expression equals " + result);

System.out.print("Evaluate another expression [Y/N]? "); again = in.nextLine(); System.out.println(); } while (again.equalsIgnoreCase("y")); } }

PostfixEvaluator.java

import java.util.Stack; import java.util.Scanner;

public class PostfixEvaluator { private final static char ADD = '+'; private final static char SUBTRACT = '-'; private final static char MULTIPLY = '*'; private final static char DIVIDE = '/';

private Stack stack;

/** * Sets up this evalutor by creating a new stack. */ public PostfixEvaluator() { stack = new Stack(); }

/** * Evaluates the specified postfix expression. If an operand is * encountered, it is pushed onto the stack. If an operator is * encountered, two operands are popped, the operation is * evaluated, and the result is pushed onto the stack. * @param expr string representation of a postfix expression * @return value of the given expression */ public int evaluate(String expr) { int op1, op2, result = 0; String token; Scanner parser = new Scanner(expr);

while (parser.hasNext()) { token = parser.next();

if (isOperator(token)) { op2 = (stack.pop()).intValue(); op1 = (stack.pop()).intValue(); result = evaluateSingleOperator(token.charAt(0), op1, op2); stack.push(new Integer(result)); } else stack.push(new Integer(Integer.parseInt(token))); }

return result; }

/** * Determines if the specified token is an operator. * @param token the token to be evaluated * @return true if token is operator */ private boolean isOperator(String token) { return ( token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/") ); }

/** * Peforms integer evaluation on a single expression consisting of * the specified operator and operands. * @param operation operation to be performed * @param op1 the first operand * @param op2 the second operand * @return value of the expression */ private int evaluateSingleOperator(char operation, int op1, int op2) { int result = 0;

switch (operation) { case ADD: result = op1 + op2; break; case SUBTRACT: result = op1 - op2; break; case MULTIPLY: result = op1 * op2; break; case DIVIDE: result = op1 / op2; }

return result; } }

LinearNode.java

public class LinearNode { private LinearNode next; private T element; /** * Creates an empty node. */ public LinearNode() { next = null; element = null; } /** * Creates a node storing the specified element. * @param elem element to be stored */ public LinearNode(T elem) { next = null; element = elem; } /** * Returns the node that follows this one. * @return reference to next node */ public LinearNode getNext() { return next; } /** * Sets the node that follows this one. * @param node node to follow this one */ public void setNext(LinearNode node) { next = node; } /** * Returns the element stored in this node. * @return element stored at the node */ public T getElement() { return element; } /** * Sets the element stored in this node. * @param elem element to be stored at this node */ public void setElement(T elem) { element = elem; } }

LinkedStack.java

public class LinkedStack implements StackADT { private int count; private LinearNode top;

/** * Creates an empty stack. */ public LinkedStack() { count = 0; top = null; }

/** * Adds the specified element to the top of this stack. * @param element element to be pushed on stack */ public void push(T element) { LinearNode temp = new LinearNode(element);

temp.setNext(top); top = temp; count++; }

/** * Removes the element at the top of this stack and returns a * reference to it. * @return element from top of stack * @throws EmptyCollectionException if the stack is empty */ public T pop() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("stack");

T result = top.getElement(); top = top.getNext(); count--; return result; } /** * Returns a reference to the element at the top of this stack. * The element is not removed from the stack. * @return element on top of stack * @throws EmptyCollectionException if the stack is empty */ public T peek() throws EmptyCollectionException { if (isEmpty())

throw new EmptyCollectionException("stack");

return top.getElement(); }

/** * Returns true if this stack is empty and false otherwise. * @return true if stack is empty */ public boolean isEmpty() { return top == null; } /** * Returns the number of elements in this stack. * @return number of elements in the stack */ public int size() { return count; }

/** * Returns a string representation of this stack. * @return string representation of the stack */ public String toString() { LinearNode temp = top;

String result = "";

while(temp != null) {

result = result + temp.getElement()+" ";

temp = temp.getNext(); } return result; } }

StackADT.java

public interface StackADT { public void push(T element); public T pop(); public T peek(); public boolean isEmpty(); public int size(); public String toString(); }

EmptyCollectionException.java

public class EmptyCollectionException extends RuntimeException { public EmptyCollectionException(String collection) { super("The " + collection + " is empty."); } }

Create a new Java project called "Lab2". Complete the implementation of the LinkedStack class presented in chapter 4 removing the "jsjf" package references. Specifically, complete the implementation of the isEmpty, peek, size and tostring methods. Use your completed LinkedStack to implement the postfix calculator from chapter 3 that previously used the collections Stack class. Copy over any code needed from the PostfixEvaluator and PostfixTester classes. Your Lab2 project will contain the following java files: PostfixTester, PostfixEvaluator, LinearNode, LinkedStack, StackADT and EmptyCollectionException) Essentially you are replacing the Stack class from the chapter 3 example with the LinkedStack class. LinkedStack.toString() must return a string in the same format as ArrayList.toString 0 or Arrays.toString(). Similar to the last lab the PostfixEvaluator.evaluate() method will display the contents of the stack after every push() operation and before each calculation. Different from the last lab you will use System.out.println() in the evaluate() method since that method has an int return value. The PostfixEvaluator class will also be modified to accept a new operator for doing exponentiation. The character to use will be the carat, " N " character. As an example, if the evaluate() method were given the string " 23 " the correct calculated return value would be 8 . An example session: Enter a valid post-fix expression one token at a time with a space between each token (e.g. 54+ 321+) Each token must be an integer or an operator (+,,,/) 54+321+2 i [5] [4,5] [4,5]+ [9] [3,9] [2,3,9] [1,2,3,9] [1,2,3,9] [1,3,9] [1,3,9]+ [4,9] [4,9] [36] [2,36] [2,36] ^ [1296] That expression equals 1296 Evaluate another expression [Y/N] ? n

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Question

How is the NDAA used to shape defense policies indirectly?

Answered: 1 week ago