Question
The following tasks are based on ArrayStack.java, LinkedStack.java, and EvaluateDemonstration.java. Please follow all instructions! a. Modify the ArrayStack and LinkedStack classes to have a print
The following tasks are based on ArrayStack.java, LinkedStack.java, and EvaluateDemonstration.java. Please follow all instructions!
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 LinkedStack for storing operations. Note that the results should not change, even though the stacks use different implementations.
c. Use your new print methods from (a) to print the stack values after each character in the expression. Provide your program output. Below is an example of partial output for the expression ((6+9)/3). Not all output is shown.
ArrayStack.java is available here: http://www.cs.colorado.edu/~main/edu/colorado/collections/ArrayStack.java
LinkedStack.java is available here: http://www.cs.colorado.edu/~main/edu/colorado/collections/LinkedStack.java
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( )
throw new IllegalArgumentException("Illegal expression");
operand2 = numbers.pop( );
operand1 = numbers.pop( );
// 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+)?.*?");
}
character: Numbers- operators- 8 Character:Numbers-Operators Character:6 Numbers-6 operators- Character Numbers-6 Operators-+ Character:9 Numbers-6,9 Operators-+ character: Numbers- operators- 8 Character:Numbers-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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started