question archive Design a program with the following specifications: Required Program Name: RPN

Design a program with the following specifications: Required Program Name: RPN

Subject:Computer SciencePrice:6.86 Bought12

Design a program with the following specifications:

  • Required Program Name: RPN.java
  • The program can use either command line arguments or standard input (the CLI) to enter in a file name. For standard input, the program should use standard output to prompt the user for the name of a file. This file contains a set of expressions in RPN format.
  • If the input file does not exist or an I/O error occurs while processing it, an error message is displayed and execution halts.
  • The input data file is a plain ASCII text file containing RPN expressions of varying length and complexity.
  • Each expression is evaluated and any malformed expressions generate an error message.
  • Program output should be neat and adequately spaced for readability.

Program Implementation

  • Using a Stack ADT, implement the program as a Java application within a single compilation unit.
  • Using only integer values, implement the following operators: addition (+), subtraction (-), multiplication (*), integer division (/) and positive number exponentiation (^).
  • For internal documentation, use plenty of comments throughout the program.
  • Be sure to include your name and course/section in the application documentation block.
  • For grading, submit a copy of the source code and a screen snapshot of the program execution.

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Code:

import java.io.File;
import static java.lang.Character.isDigit;
import java.util.*;
import java.util.regex.*;
public class RPN {


  /*
  This method is for taking the input from the user, Here we will
  take the two type of input from the user, based on the choice.
  One will be through CLI and another will be from the file. 
  */
    public static String inputexpr(String expr)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter 1 for input the expression");
        System.out.println("Enter 2 to enter expression from the file");
        int choice = sc.nextInt();sc.nextLine();
        if(choice == 1)     // If the user selects 1 
        {
            System.out.println("Enter the expression: ");
            expr = sc.nextLine();     //expression is stored here.
        } 
        else if(choice == 2)
        {
            try{
            File file = new File("C:\\Users\\asus\\Desktop\\expr.txt");       //location of the file
            Scanner files = new Scanner(file);
            expr = files.nextLine();    //expression is stored here.
            System.out.println(expr);
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }
        return expr;
    }
    
    /*
    This function will take the expression from the above and then will note the counter according to the syntax.
    For example: if the syntax contains any symbol like +,-,*,/,^ then decrease the counter, otherwise if there is a character then 
    increase the counter. 
    */
    public static boolean verifyexpr(String expr)
    {
        boolean b = true;
        int count=0;
        int len=expr.length();
        int i=0;
        while(i<len)
        {
            if(isDigit(expr.charAt(i)))
            {
                count++;
            }
            else if(expr.charAt(i)=='+' ||expr.charAt(i)=='-'||expr.charAt(i)=='*'||expr.charAt(i)=='/'||expr.charAt(i)=='^')
            {
                count--;
            }
            else if(expr.charAt(i)==' '){}
            else
            {
                b = false;
                break;
            }
            i++;
        }
        if(count!=1)
            b = false;
      
        return b;
    }


    public static int evaluateexpr(String expr)
    {
        Stack<Integer> stack = new Stack<Integer>();   //define the stack "stack"
        int i=0;
        int src, src1, src2;
        int len = expr.length();
        while(i<len)
        {
          
            if(isDigit(expr.charAt(i)))
            {
                //if the character is a digit then convert into integer and push onto stack
                src = Integer.parseInt(expr.charAt(i)+"");
                stack.push(src);
            }
            else
            {
                //if token is an operator then pop two operands from stack and perform operations.
                src2 = stack.pop();
                src1 = stack.pop();
                char ch = expr.charAt(i);
                if(ch == '+')
                {
                    stack.push(src1 + src2);
                }
                else if(ch == '-')
                {
                    stack.push(src1 - src2);
                }
                else if(ch == '*')
                {
                    stack.push(src1 * src2);
                }
                else if(ch == '/')
                {
                    stack.push(src1/src2);
                }
                else if(ch=='^')
                {
                    stack.push((int)Math.pow(src1*1.0, src2*1.0));
                }
            }
            i++;
        }
        return stack.pop();
    }


    //Main function
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String expr = null;
        //Go to the inputexpr() method. 
        expr = inputexpr(expr);
        //verify input expression
        boolean check = verifyexpr(expr);
        if(check)
        {
            int eval = evaluateexpr(expr);
            System.out.println("eval = "+eval);
        }
        else
        {
            System.out.println("Invalid Expression!!");
        }
    }
  
}

This program is done in VSCode.

Output(1):

Output(2):

Output(3):

Please see the attached file for the complete solution