Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a Java project using source code given below to recreate user interaction to accept infix expressions as in PostFixTester. Complete the implementation of the

Create a Java project using source code given below to recreate user interaction to accept infix expressions as in PostFixTester.

Complete the implementation of the ArrayStack class. Specifically, complete the implementation of the isEmpty(), size(), and toString() methods. The output of toString() will be in the same form as generated by Arrays.toString() and ArrayList.toString() and contain only valid data elements.

Should contain the following 5 source code files:

InfixGenerator.java

InfixGenTester.java

EmptyCollectionException.java

ArrayStack.java

StackADT.java

Use your completed ArrayStack to implement a postfix to infix translator in a class called "InfixGenerator". The InfixGenerator class will contain the functionality of the translation and be invoked from main().

**Make sure that your InfixGenerator class is using your ArrayStack class and not the Java Collections Stack class.

In addition, you will create a java class called InfixGenTester.java that contains the main() method and your utilization of the translation exposed by InfixGenerator. Use as much as you find useful from the PostfixEvaluator/PostfixTester class examples from chapter 3.

Hint: Much like the PostFix evaluator pushes the Integer result of a calculation back onto the stack, push the String result of the operands/operator string concatenation onto the stack. The use of parentheses for each translation is encouraged to strictly maintain order of operations.

To test your implementation of the ArrayStack.toString() method your program will output the contents of the stack after every token of the postfix expression is processed. Each time a data item is pushed the stack will display and each time an operator is encountered the stack will be displayed suffixed with the operator. The last line of output will be the end result of the conversion. (see example below). System.out.print()/println() may only be used in main().

Sample Code:

StackADT.java

import java.util.Arrays; import java.util.Stack; /** * An array implementation of a stack in which the bottom of the * stack is fixed at index 0. * * @author Lewis and Chase * @version 4.0 */ public class ArrayStack implements StackADT { private final static int DEFAULT_CAPACITY = 100; private int top; private T[] stack; /** * Creates an empty stack using the default capacity. */ public ArrayStack() { this(DEFAULT_CAPACITY); } /** * Creates an empty stack using the specified capacity. * * @param initialCapacity the initial size of the array */ public ArrayStack(int initialCapacity) { top = 0; stack = (T[]) (new Object[initialCapacity]); } /** * Adds the specified element to the top of this stack, expanding * the capacity of the array if necessary. * * @param element generic element to be pushed onto stack */ public void push(T element) { if (size() == stack.length) expandCapacity(); stack[top] = element; top++; } /** * Creates a new array to store the contents of this stack with * twice the capacity of the old one. */ private void expandCapacity() { stack = Arrays.copyOf(stack, stack.length * 2); } /** * Removes the element at the top of this stack and returns a * reference to it. * * @return element removed from top of stack * @throws EmptyCollectionException if stack is empty */ public T pop() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("stack"); top--; T result = stack[top]; stack[top] = null; 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 stack is empty */ public T peek() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("stack"); return stack[top - 1]; } /** * Returns true if this stack is empty and false otherwise. * * @return true if this stack is empty */ public boolean isEmpty() { return (top == -1); } /** * Returns the number of elements in this stack. * * @return the number of elements in the stack */ public int size() { return (top + 1); } /** * Returns a string representation of this stack. * a string * @return representation of the stack */ @Override public String toString() { String result = " "; T current = top; while (current != null) { result += current.getElement() + " "; current = current.getNext(); } return result + ""; } } EmptyCollectionException.java 
/** * Represents the situation in which a collection is empty. * * @author Lewis and Chase * @version 4.0 */ public class EmptyCollectionException extends RuntimeException { /** * Sets up this exception with an appropriate message. * @param collection the name of the collection */ public EmptyCollectionException(String collection) { super("The " + collection + " is empty."); } } 

InfixGenerator.java

import java.util.Scanner; import java.util.Stack; public class InfixGenerator { 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 InfixGenerator() { 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; } } 

InfixGenTester.java

import java.util.Scanner; /** * Demonstrates the use of a stack to evaluate postfix expressions. * * @author Lewis and Chase * @version 4.0 */ public class InfixGenTester { /** * Reads and evaluates multiple postfix expressions. */ public static void main(String[] args) { String expression, again; int result; Scanner in = new Scanner(System.in); do { InfixGenerator evaluator = new InfixGenerator(); 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")); } } 

ArrayStack.java

import java.util.Arrays; /** * An array implementation of a stack in which the bottom of the * stack is fixed at index 0. * * @author Lewis and Chase * @version 4.0 */ public class ArrayStack implements StackADT { private final static int DEFAULT_CAPACITY = 100; private int top; private T[] stack; /** * Creates an empty stack using the default capacity. */ public ArrayStack() { this(DEFAULT_CAPACITY); } /** * Creates an empty stack using the specified capacity. * @param initialCapacity the initial size of the array */ public ArrayStack(int initialCapacity) { top = 0; stack = (T[])(new Object[initialCapacity]); } /** * Adds the specified element to the top of this stack, expanding * the capacity of the array if necessary. * @param element generic element to be pushed onto stack */ public void push(T element) { if (size() == stack.length) expandCapacity(); stack[top] = element; top++; } /** * Creates a new array to store the contents of this stack with * twice the capacity of the old one. */ private void expandCapacity() { stack = Arrays.copyOf(stack, stack.length * 2); } /** * Removes the element at the top of this stack and returns a * reference to it. * @return element removed from top of stack * @throws EmptyCollectionException if stack is empty */ public T pop() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("stack"); top--; T result = stack[top]; stack[top] = null; 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 stack is empty */ public T peek() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("stack"); return stack[top-1]; } /** * Returns true if this stack is empty and false otherwise. * @return true if this stack is empty */ public boolean isEmpty() { // To be completed as a Programming Project } /** * Returns the number of elements in this stack. * @return the number of elements in the stack */ public int size() { // To be completed as a Programming Project } /** * Returns a string representation of this stack. * @return a string representation of the stack */ public String toString() { // To be completed as a Programming Project } } 

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