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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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);
       }
   }
}
 

Here is the transcription of the image suitable for an Educational website:

---

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.
   - `ac` clears the whole stack. Should work for any values on the stack.
   - `pop` to remove the last value entered (i.e., top value of the stack). Requires the stack to have at least one value.
   - `swap` to swap the top two elements of the stack. Requires the stack to have at least 2 values.
   - decide how to handle a division by 0, don't let the code throw an exception.

4. If errors are found in the `evaluate()` or `evaluateLine()`, your code should return `false` and clear the stack. Do not just print error messages.

---
Transcribed Image Text:Here is the transcription of the image suitable for an Educational website: --- 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. - `ac` clears the whole stack. Should work for any values on the stack. - `pop` to remove the last value entered (i.e., top value of the stack). Requires the stack to have at least one value. - `swap` to swap the top two elements of the stack. Requires the stack to have at least 2 values. - decide how to handle a division by 0, don't let the code throw an exception. 4. If errors are found in the `evaluate()` or `evaluateLine()`, your code should return `false` and clear the stack. Do not just print error messages. ---
Expert Solution
Step 1

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:

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education