Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with the following problem. ArrayStack.java, LinkedStack.java, and EvaluateDemonstration.java are at the bottom. For part c, ignore the check your answers from problem

I need help with the following problem. ArrayStack.java, LinkedStack.java, and EvaluateDemonstration.java are at the bottom. For part c, ignore the "check your answers from problem #2." Please make it look like the sample output.

image text in transcribed

ArrayStack.java:

import java.util.EmptyStackException; public class ArrayStack implements Cloneable { private E[ ] data; private int manyItems; public ArrayStack( ) { final int INITIAL_CAPACITY = 10; manyItems = 0; data = (E[]) new Object[INITIAL_CAPACITY]; } public ArrayStack(int initialCapacity) { if (initialCapacity  clone( ) { // Clone an ArrayStack.  ArrayStack answer; try { answer = (ArrayStack) super.clone( ); } catch (CloneNotSupportedException e) { // This exception should not occur. But if it does, it would probably indicate a  // programming error that made super.clone unavailable. The most comon error  // The most common error would be forgetting the "Implements Cloneable"  // clause at the start of this class.  throw new RuntimeException ("This class does not implement Cloneable"); } answer.data = data.clone( ); return answer; } public void ensureCapacity(int minimumCapacity) { E biggerArray[ ]; if (data.length // EmptyStackException is from java.util and its constructor has no argument.  throw new EmptyStackException( ); return data[manyItems-1]; } public E pop( ) { if (manyItems == 0) // EmptyStackException is from java.util and its constructor has no argument.  throw new EmptyStackException( ); return data[--manyItems]; } public void push(E item) { if (manyItems == data.length) { // Double the capacity and add 1; this works even if manyItems is 0. However, in  // case that manyItems*2 + 1 is beyond Integer.MAX_VALUE, there will be an  // arithmetic overflow and the bag will fail.  ensureCapacity(manyItems*2 + 1); } data[manyItems] = item; manyItems++; } public int size( ) { return manyItems; } public void trimToSize( ) { E trimmedArray[ ]; if (data.length != manyItems) { trimmedArray = (E[]) new Object[manyItems]; System.arraycopy(data, 0, trimmedArray, 0, manyItems); data = trimmedArray; } } } 

LinkedStack.java:

import java.util.EmptyStackException; public class LinkedStack implements Cloneable { private Node top; public LinkedStack( ) { top = null; } @SuppressWarnings("unchecked") public LinkedStack clone( ) { // Clone a LinkedStack.  LinkedStack answer; try { answer = (LinkedStack) super.clone( ); } catch (CloneNotSupportedException e) { // This exception should not occur. But if it does, it would probably indicate a  // programming error that made super.clone unavailable. The most comon error  // The most common error would be forgetting the "Implements Cloneable"  // clause at the start of this class.  throw new RuntimeException ("This class does not implement Cloneable"); } // The generic listCopy method gets the type of E from top.  answer.top = Node.listCopy(top); return answer; } public boolean isEmpty( ) { return (top == null); } public E peek( ) { if (top == null) // EmptyStackException is from java.util and its constructor has no argument.  throw new EmptyStackException( ); return top.getData( ); } public E pop( ) { E answer; if (top == null) // EmptyStackException is from java.util and its constructor has no argument.  throw new EmptyStackException( ); answer = top.getData( ); top = top.getLink( ); return answer; } public void push(E item) { top = new Node(item, top); } public int size( ) { // The generic listLength method gets the type of E from top.  return Node.listLength(top); } } 

EvaluateDemonstration.java:

import java.util.Stack; import java.util.Scanner; import java.util.regex.Pattern; public class EvaluateDemonstration { public static void main(String[ ] args) { Scanner stdin = new Scanner(System.in); String expression; double answer; System.out.println("Please type an arithmetic expression made from"); System.out.println("unsigned numbers and the operations + - * /."); System.out.println("The expression must be fully parenthesized."); do { System.out.print("Your expression: "); expression = stdin.nextLine( ); try { answer = evaluate(expression); System.out.println("The value is " + answer); } catch (Exception e) { System.out.println("Error." + e.toString( )); } } while (query(stdin, "Another string?")); System.out.println("All numbers are interesting."); } public static boolean query(Scanner input, String prompt) { String answer; System.out.print(prompt + " [Y or N]: "); answer = input.nextLine( ).toUpperCase( ); while (!answer.startsWith("Y") && !answer.startsWith("N")) { System.out.print("Invalid response. Please type Y or N: "); answer = input.nextLine( ).toUpperCase( ); } return answer.startsWith("Y"); } public static double evaluate(String s) { Scanner input = new Scanner(s); Stack numbers = new Stack( ); Stack operations = new Stack( ); String next; char first; while (input.hasNext( )) { if (input.hasNext(UNSIGNED_DOUBLE)) { next = input.findInLine(UNSIGNED_DOUBLE); numbers.push(new Double(next)); } else { next = input.findInLine(CHARACTER); first = next.charAt(0); switch (first) { case '+': // Addition  case '-': // Subtraction  case '*': // Multiplication  case '/': // Division  operations.push(first); break; case ')': // Right parenthesis  evaluateStackTops(numbers, operations); break; case '(': // Left parenthesis  break; default : // Illegal character  throw new IllegalArgumentException("Illegal character"); } } } if (numbers.size( ) != 1) throw new IllegalArgumentException("Illegal input expression"); return numbers.pop( ); } public static void evaluateStackTops(Stack numbers, Stack operations) { double operand1, operand2; // Check that the stacks have enough items, and get the two operands.  if ((numbers.size( ) // Carry out an action based on the operation on the top of the stack.  switch (operations.pop( )) { case '+': numbers.push(operand1 + operand2); break; case '-': numbers.push(operand1 - operand2); break; case '*': numbers.push(operand1 * operand2); break; case '/': // Note: A division by zero results in POSTIVE_INFINITY or  // NEGATIVE_INFINITY.  numbers.push(operand1 / operand2); break; default : throw new IllegalArgumentException("Illegal operation"); } } public static final Pattern CHARACTER = Pattern.compile("\S.*?"); public static final Pattern UNSIGNED_DOUBLE = Pattern.compile("((\d+\.?\d*)|(\.\d+))([Ee][-+]?\d+)?.*?"); }
3. The following tasks are based on ArrayStack.java, LinkedStack.java, and EvaluateDemonstration.java from the textbook. a. Modify the ArrayStack and LinkedStack classes to have a "print" method that prints all of the stack values in a row, separated by spaces b. Create a program "Problem3.java", based on EvaluateDemonstration.java, to use ArrayStack for storing numbers and Linked the stacks use different implementations. Use your new "print" methods from (a) to check your answers from problem #2 by printing the stack values after each character in the expression. Provide your program output in your report. Below is an example of partial output for the expression ((6+9)/3). Not all output is shown. Stack for storing operations. Note that the results should not change, even though c. CharacterNumbers-Operators- CharacterNumbers-Operators- Character:6 Numbers-6 Operators- Character: Numbers-6 Operators Character:9 Numbers-6,9 Operators-+

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

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

13th Edition Global Edition

1292263350, 978-1292263359

More Books

Students also viewed these Databases questions