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, Stack stack)             throws             ArithmeticException     {         boolean isANumber = token.matches("[0-9]+");         if (isANumber) {             stack.push(token);             return true;         }         else if (token.equals("+") && (stack.size() >= 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.size() >= 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.size() >= 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.size() >= 2)) {             String b = stack.pop();             String a = stack.pop();             if (getValue(b) == 0) {                 throw new ArithmeticException("Cannot divide by zero!");             }             int result = getValue(a) / getValue(b);             stack.push("" + result);             System.out.println(a + " / " + b + " = " + result);             return true;         }         else if (token.equals("ac") && (stack.size() >= 0)) {             System.out.println("Input recognized as [" + token + "]. so Stack cleared.");             stack.clear();             return true;         }         else if (token.equals("pop") && (stack.size() >= 1)) {             String p = stack.pop();             System.out.println("The popped up element is " + p);             return true;         }         else if (token.equals("swap") && (stack.size() >= 2)) {             System.out.println("Last two Stack elements Before Swaping ");             String b = stack.pop();             System.out.println(b + " ");             String a = stack.pop();             System.out.println(a + " ");             int x = getValue(a);             int y = getValue(b);             x = x + y;             y = x - y;             x = x - y;             stack.push("" + x);             stack.push("" + y);             System.out.println();             System.out.println("Last two Stack elements After Swaping ");             while (!(stack.empty())) {                 System.out.println(stack.pop() + " ");             }             return true;         }         else {             System.out.println("Input not recognized [" + token + "]");             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, Stack stack)      * {      * Scanner sn = new Scanner(line);      * while (sn.hasNext()) {      * String token = sn.next();      * evaluate(token, stack);      * }      * } */     public static void main(String[] args)     {         Stack stack = new Stack();         RPN r = new RPN();         Scanner sn = new Scanner(System.in);         while (sn.hasNext()) {             String token = sn.next();             boolean b = r.evaluate(token, stack);             if (!b) {                 System.exit(0);             }         }     } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
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, Stack<String> stack)
            throws
            ArithmeticException
    {
        boolean isANumber = token.matches("[0-9]+");
        if (isANumber) {
            stack.push(token);
            return true;
        }
        else if (token.equals("+") && (stack.size() >= 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.size() >= 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.size() >= 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.size() >= 2)) {
            String b = stack.pop();
            String a = stack.pop();
            if (getValue(b) == 0) {
                throw new ArithmeticException("Cannot divide by zero!");
            }

            int result = getValue(a) / getValue(b);
            stack.push("" + result);
            System.out.println(a + " / " + b + " = " + result);
            return true;

        }
        else if (token.equals("ac") && (stack.size() >= 0)) {
            System.out.println("Input recognized as [" + token + "]. so Stack cleared.");
            stack.clear();
            return true;
        }
        else if (token.equals("pop") && (stack.size() >= 1)) {
            String p = stack.pop();
            System.out.println("The popped up element is " + p);
            return true;
        }
        else if (token.equals("swap") && (stack.size() >= 2)) {
            System.out.println("Last two Stack elements Before Swaping ");
            String b = stack.pop();
            System.out.println(b + " ");
            String a = stack.pop();
            System.out.println(a + " ");
            int x = getValue(a);
            int y = getValue(b);
            x = x + y;
            y = x - y;
            x = x - y;
            stack.push("" + x);
            stack.push("" + y);
            System.out.println();
            System.out.println("Last two Stack elements After Swaping ");
            while (!(stack.empty())) {
                System.out.println(stack.pop() + " ");
            }
            return true;
        }
        else {
            System.out.println("Input not recognized [" + token + "]");
            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, Stack<String> stack)
     * {
     * Scanner sn = new Scanner(line);
     * while (sn.hasNext()) {
     * String token = sn.next();
     * evaluate(token, stack);
     * }
     * } */
    public static void main(String[] args)
    {
        Stack<String> stack = new Stack<String>();
        RPN r = new RPN();
        Scanner sn = new Scanner(System.in);
        while (sn.hasNext()) {
            String token = sn.next();
            boolean b = r.evaluate(token, stack);
            if (!b) {
                System.exit(0);
            }
        }
    }
}

 

5. Write test cases for the RPN (RPNTest.java) to ensure it works correctly. Remember, part of your score depends on how well you test your own code.
You can test the RPN evaluator by sending tokens one at a time to evaluate() or by calling the evaluateLine() passing a full text line. Make sure that
you test all special cases, for example division by 0, or pop and empty stack, etc.
Transcribed Image Text:5. Write test cases for the RPN (RPNTest.java) to ensure it works correctly. Remember, part of your score depends on how well you test your own code. You can test the RPN evaluator by sending tokens one at a time to evaluate() or by calling the evaluateLine() passing a full text line. Make sure that you test all special cases, for example division by 0, or pop and empty stack, etc.
20
21
22
23
24
25
26
27
28
29
30
!
32
33
34
35
36
37
38
40
41
42
43
44
45
0-
public class RPNMain
{
}
* Main program, manages input from keyboard and passing
* information to evaluation procedures above.
* @param args - command line arguments
*/
public static void main(String args[])
{
RPN interpreter = new RPN();
StackADT<String> stack = new Stack<>();
System.out.println("Welcome to mini RPN calculator\n");
System.out.print("$ ");
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
}
String line = sc.nextLine().trim();
interpreter.evaluateLine(line, stack);
System.out.print("\n$ ");
System.out.println("Bye");
Transcribed Image Text:20 21 22 23 24 25 26 27 28 29 30 ! 32 33 34 35 36 37 38 40 41 42 43 44 45 0- public class RPNMain { } * Main program, manages input from keyboard and passing * information to evaluation procedures above. * @param args - command line arguments */ public static void main(String args[]) { RPN interpreter = new RPN(); StackADT<String> stack = new Stack<>(); System.out.println("Welcome to mini RPN calculator\n"); System.out.print("$ "); Scanner sc = new Scanner(System.in); while (sc.hasNextLine()) { } String line = sc.nextLine().trim(); interpreter.evaluateLine(line, stack); System.out.print("\n$ "); System.out.println("Bye");
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY