3. Modify the evaluate() method in RPN.java to add the commands listed below. These commands can appear anywhere in your input. Modify the RPN.java file so that it detects these tokens and then executes them appropriately. o 'ac' clears the whole stack. Should work for any values on the stack. o 'pop' to remove the last value entered (i.e., top value of the stack). Requires the stack to have at least one value. o 'swap' to swap the top two elements of the stack. Requires the stack to have at least 2 values. o decide how to handle a division by O, don't let the code throw an exception.
RPN.java
import java.util.Scanner;
/**
* Reverse Polish Notation calculator. It evaluates
* a string with expressions in RPN format and prints
* the results. Exampel of a stack use.
*/
public class RPN
{
/**
* Given a string, return an integer version of
* the string. Check if the string contains only
* numbers, if so, then does the conversion. If
* it does not, it reurns 0.
* @param t string with numeric token
* @return int version of the numeric token in t
*/
public int getValue(String t)
{
if (t.matches("[0-9]+")) {
return Integer.parseInt(t);
}
else {
return 0;
}
}
/**
* Evaluates a single token in an RPN expression. If it is
* a number, it pushes the token to the stack. If it is an
* operator, then it pulls 2 numbers from the stack, performs
* the operation and pushes back into the stack the result.
* @param token to be evaluated
* @param stack holding values for expression evaluation
*/
public boolean evaluate(String token, StackADT<String> stack)
{
boolean isANumber = token.matches("[0-9]+");
if (isANumber) {
stack.push(token);
return true;
}
else if (token.equals("+") && (stack.numElements() >= 2)) {
String b = stack.pop();
String a = stack.pop();
int result = getValue(a) + getValue(b);
stack.push("" + result);
System.out.println(a + " + " + b + " = " + result);
return true;
}
else if (token.equals("-") && (stack.numElements() >= 2)) {
String b = stack.pop();
String a = stack.pop();
int result = getValue(a) - getValue(b);
stack.push("" + result);
System.out.println(a + " - " + b + " = " + result);
return true;
}
else if (token.equals("*") && (stack.numElements() >= 2)) {
String b = stack.pop();
String a = stack.pop();
int result = getValue(a) * getValue(b);
stack.push("" + result);
System.out.println(a + " * " + b + " = " + result);
return true;
}
else if (token.equals("/") && (stack.numElements() >= 2)) {
String b = stack.pop();
String a = stack.pop();
int result = getValue(a) / getValue(b);
stack.push("" + result);
System.out.println(a + " / " + b + " = " + result);
return true;
}
else {
System.out.println("Input not recognized [" + token + "]. Stack cleared.");
stack.clear();
return false;
}
}
/**
* Evaluates a full line of input with an RPN expression.
* Splits the line into tokens and calls the evaluate() to
* do the work.
* @param line to be divided up into token
* @param stack holding values for expression evaluation
*/
public void evaluateLine(String line, StackADT<String> stack)
{
Scanner sn = new Scanner(line);
while (sn.hasNext()) {
String token = sn.next();
evaluate(token, stack);
}
}
}
This java program deals with exception handling along with different methods of stack class.
Swap option executes the logic as follows
x = 24, y = 10
x = x +y; ( x is now 34, y is 10 )
y = x - y; ( y is now 24, x is 34)
x = x - y; ( x is 10, y is 24)
It swaps the values of x and y without using third variable.
The full error free code is as follows:
Step by step
Solved in 3 steps with 1 images