Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a command line calculator program that can evaluate simple mathematical ex

ID: 3682660 • Letter: W

Question

Write a command line calculator program that can evaluate simple mathematical expressions on doubles typed in postfix notation (also called reverse polish notation, or RPN), as well as store variables for later use in other expressions.  

In a typical mathematical expression that you are probably used to, such as 5 2, the operator+) goes in-between the two operands (5 and 2). This is called "infix" notation. Postfix notation places the two operands first, and the + sign last, giving 5 2 +. One of the main benefits of postfix notation is that it is easy to evaluate using a "stack". Parentheses are not needed, nor are precedence rules, since there is only one reasonable way to evaluate the expression. The algorithm to do this uses a stack of numbers, and can be stated in a few steps as follows Given a Postfix expression such as "2 3 +x*", break it into tokens, and initialize an empty stack of numbers. Then do the following steps: 1. Process the tokens from left to right. For each token, If the token is an operand (a number or variable), push the value of the operand on the stack (of doubles in our case). For an operator +, etc.), pop the needed number of operands off the stack, compute the result, and push it back on the stack. 2. The expression's value wil be on the top of the stack at the end. Error checking is also easy. The following are error 1. If at any point there are not enough operands for an operation on the stack, the expression is not valid. 2. If more than one value is on the stack at the end, the expression is not valid. Memory: Your calculator must have a memory that allows it to store or modify named variables. These can easily be stored in a HashMap, or parallel ArrayLists.

Explanation / Answer

Example-Java code for a command line argument program

public class Test{

    int Trails;

    int Days

    public Test(int numT, int numD){

        Trails = numT;

        Days = numD;

    }

    public static void main (String[] args){

        int numT;

        int numD;

        if(args.length!=0){

            numT = Integer.parseInt(args[0]);

            numD = Integer.parseInt(args[1]);

            //Create object here

            Test t = new Test(numT, numD);

        }

    }

}

Example: The program Arithmetic reads three parameters form the command-line, two integers and an arithmetic operator.

public class Arithmetic {

   public static void main (String[] args) {

      int operand1, operand2;

      char theOperator;

      operand1 = Integer.parseInt(args[0]); // Convert String to int

      operand2 = Integer.parseInt(args[1]);

      theOperator = args[2].charAt(0);       // Consider only 1st character

      System.out.print(args[0] + args[2] + args[1] + "=");

      switch(theOperator) {

         case ('+'):

          System.out.println(operand1 + operand2); break;

         case ('-'):

            System.out.println(operand1 - operand2); break;

         case ('*'):

            System.out.println(operand1 * operand2); break;

         case ('/'):

            System.out.println(operand1 / operand2); break;

         default:

            System.out.printf("%nError: Invalid operator!");

      }

   }

}

Example-Java code for a postfix expression evaluation

import java.io.*;

class Stack

{

   private int[] a;

   private int top,m;

   public Stack(int max)

   {

     m=max;

     a=new int[m];

     top=-1;

   }

   public void push(int key)

   {

     a[++top]=key;

   }

   public int pop()

   {

     return(a[top--]);

   }

}

class Evaluation{

   public int calculate(String s)

   {

     int n,r=0;

     n=s.length();

     Stack a=new Stack(n);

     for(int i=0;i<n;i++)

     {

       char ch=s.charAt(i);

       if(ch>='0'&&ch<='9')

         a.push((int)(ch-'0'));

       else

       {

         int x=a.pop();

         int y=a.pop();

         switch(ch)

         {

           case '+':r=x+y;

              break;

           case '-':r=y-x;

              break;

           case '*':r=x*y;

              break;

           case '/':r=y/x;

              break;

           default:r=0;

         }

         a.push(r);

       }

     }

     r=a.pop();

     return(r);

   }

}

class PostfixEvaluation

{

   public static void main(String[] args)throws IOException

   {

     String input;

     while(true)

     {

       System.out.println("Enter the postfix expresion");

       input=getString();

       if(input.equals(""))

         break;

       Evaluation e=new Evaluation();

       System.out.println("Result:- "+e.calculate(input));

     }

   }

   public static String getString()throws IOException

   {

     DataInputStream inp=new DataInputStream(System.in);

     String s=inp.readLine();

     return s;

   }

}

note-by using the above coding examples the given question can be answered.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote