Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a class Lab that reads and evaluates postx expressions contained in a le. Each line of the le contains a postx expression with integers

Write a class Lab that reads and evaluates postx expressions contained in a le. Each line of the le contains a postx expression with integers as operands and +, -, * and / as operators. Consecutive operands and operators are separated by spaces. Note the following requirements for the lab:

The main method in Lab.java should do the following. 1. ask the user for the name of the le containing the expressions, 2. for each line of the le, read the postx expression, call the method evaluatePostfix on this expression, and print the expression and its evaluation on the console.

The method to evaluate the postx expression in Lab.java must have the following signature: public static int evaluatePostfix(String expr). Your implementation must use the Scanner class to parse each line of the le to obtain the postx expressions and then use a stack to evaluate each of these expressions.

Test your code using lab.dat as your input data le. You will also need the following les : StackInterface.java, StackVectorBased.java, StackException.java.

lab.dat

5 4 - 12 - 10 62 + / 10 7 * 43 - 15 / 17 39 - + 22 11 / 19 * 14 2 + 2 * - 40 3 * 66 - 5 / 29 +

StackInterface.java

/** Generic Stack Interface */

public interface StackInterface {

/** Determines whether the stack is empty. * @return true if the stack is empty; false otherwise */ public boolean isEmpty(); /** * Removes all the items from the stack. * PostCondition: Stack is empty. */ public void popAll(); /** Adds an item to the top of a stack. *Postcondition: If insertion is successful, newItem is on the top * of the stack * @param newItem is the item to be added. * @throws Some implementations may throw StackException when * newItem cannot be placed on the stack. */ public void push(E newItem) throws StackException;

/** * Removes the top of a stack. * @return If the stack is not empty, the item that was added most * recently is removed from the stack and returned. * @throws StackException if the stack is empty. */ public E pop() throws StackException; /** Retrieves the top of a stack. * @return If the stack is not empty, the item that was added most * recently is returned. The stack is unchanged. * @throws StackException if the stack is empty. */ public E peek() throws StackException;

} // end StackInterface

StackVectorBased.java

/** Implementation of a Generic Stack using a Vector we consider the * top of the stack to be the first element in the Vector. Hence a * push adds an element in the front of the vector, and a * pop removes the first element of the vector. * @author St\'ephane */

import java.util.Vector;

public class StackVectorBased implements StackInterface { /** Vector used for the stack */ private Vector stack; /** default constructor */ public StackVectorBased() { stack = new Vector(); } // end default constructor

/** Tests if this stack has no elements. * @return true if this list has no elements; * false otherwise. */ public boolean isEmpty() { return stack.isEmpty(); } // end isEmpty

/** Adds an item to the top of a stack. *Postcondition: If insertion is successful, newItem is on the top * of the stack * @param newItem is the item to be added. * @throws Some implementations may throw StackException when * newItem cannot be placed on the stack. */ public void push(E newItem) throws StackException { stack.add(0, newItem); } // end push /** * Removes all the items from the stack. * PostCondition: Stack is empty. */ public void popAll() { stack.removeAllElements(); } // end popAll /** * Removes the top of a stack. * @return If the stack is not empty, the item that was added most * recently is removed from the stack and returned. * @throws StackException if the stack is empty. */ public E pop() throws StackException { if (!isEmpty()) { return stack.remove(0); } else { throw new StackException("StackException on " + "pop: stack empty"); } // end if } // end pop /** Retrieves the top of a stack. * @return If the stack is not empty, the item that was added most * recently is returned. The stack is unchanged. * @throws StackException if the stack is empty. */ public E peek() throws StackException { if (!isEmpty()) { return stack.firstElement(); } else { throw new StackException("Stack exception on " + "peek - stack empty"); } // end if } // end peek } // end StackArrayBased

StackException.java. public class StackException extends java.lang.RuntimeException { public StackException(String s) { super(s); } // end constructor } // end StackException

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

Recommended Textbook for

Principles Of Database Systems With Internet And Java Applications

Authors: Greg Riccardi

1st Edition

020161247X, 978-0201612479

More Books

Students also viewed these Databases questions