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

Write a command line calculator. Implement the (), +, -, /, *, and % functions.

ID: 3593429 • Letter: W

Question

Write a command line calculator. Implement the (), +, -, /, *, and % functions. Make sure your calculator works with both integers and doubles.
Here's an example run: javac Calculator 2 + 3 5

Separate all the arguments with a space to make your life easy. If the user enters anything other than a number where a number belongs, throw a LookAtMrAlgebraOverHereException derived from the IlleglArgumentException. If the user enters an operation which isn't supported, throw a IllegalOperationException derived from the IllegalArgumentException. If the user forgets to enter a number, throw a UserIsADumbassException also derived from IllegalArgumentException. Make sure you deal with these exceptions gracefully. Don't pass them on to the JVM. You should also catch and deal with ArithmeticException in case the user tries to divide by zero.
Make sure your calculator correctly deals with the order of operations. (Remember PEMDAS?). I think if you research how to turn the command line arguments into postfix notation, it'll help. But I'm not 100% sure.

Explanation / Answer

Calculator.java
------------------------------------------------------
import java.util.Stack;

public class Calculator {

    public static Stack<String> postFixStk = new Stack();

    public static void main(String[] args){
        try {
            String expression = new String();

            String[] s = new String[args.length + 2];
            s[0] = new String("(");
            s[args.length + 1] = new String(")");
            for (int x = 1; x < args.length + 1; x++) {
                s[x] = new String(args[x-1]);
            }

            for (int i = 0; i < s.length; i++) {
                expression += s[i];
            }

            System.out.println(expression);

            makePostfix(s);

            System.out.println(postFixStk);

            System.out.println(solve(postFixStk));
        }catch(Exception e){
            System.out.println(e);
        }


    }

    public static void makePostfix(String [] a) throws LookAtMrAlgebraOverHereException,UserIsADumbassException,IllegalOperationException{
        Stack<String> operations = new Stack();
        Stack<String> temp = new Stack();
        String string = new String();

        for(int i = 0; i < a.length ; i++){

            if(a[i].equals("+") || a[i].equals("-") || a[i].equals("*") || a[i].equals("/") || a[i].equals("%")){
                if(operations.isEmpty()) {
                    operations.add(a[i]);
                }
                else{
                    if(precedence(operations.peek()) > precedence(a[i]) && !operations.peek().equals("(")) {
                        temp.add(operations.pop());
                        operations.add(a[i]);
                    }
                    else{
                        operations.add(a[i]);
                    }
                }
            }
            else if(a[i].equals("(")){
                operations.add(a[i]);
            }
            else if(a[i].equals(")")){
                boolean isPar = false;
                while(!isPar){
                    if(operations.isEmpty()){
                        throw new UserIsADumbassException("There is an extra Parenthesis in your expression.");
                    }
                    if(operations.peek().equals("(")){
                        operations.pop();
                        isPar = true;
                    }
                    else{
                        temp.add(operations.pop());

                    }
                }
            }
            else{
                char[] test = a[i].toCharArray();
                for(int z = 0;z<test.length;z++){
                    if(test[z] == '0' || test[z] == '1' ||test[z] == '2' ||test[z] == '3' ||test[z] == '4' ||test[z] == '5' ||test[z] == '6' ||test[z] == '7' ||test[z] == '8' ||test[z] == '9' ){ }
                    else{
                        throw new UserIsADumbassException("There is an illegal term in your expression. " +
                                "The illegal term is " + a[i]);
                    }
                }
                temp.add(a[i]);
            }
        }
        while(!temp.isEmpty()){
            postFixStk.add(temp.pop());
        }
        System.out.println("Finished changing to postfix.");

    }

    public static int precedence(String a){
        if(a.equals("+") || a.equals("-")){
            return 0;
        }
        else if(a.equals("*") || a.equals("/")|| a.equals("%")){
            return 1;
        }
        else if(a.equals("(") || a.equals(")")){
            return 2;
        }
        else return -1;
    }

    public static double add(double a, double b){
        return a + b;
    }

    public static double subtract(double a, double b){
        return add(a,b * -1);
    }

    public static double multiply(double a, double b){
        return a * b;
    }

    public static double divide(double a, double b) throws IllegalOperationException{
        if(b == 0.0){
            throw new IllegalOperationException("There is a division by zero in your expression. " +
                    "Fix this error to get a result.");
        }
        return a / b;

    }

    public static double modulo(double a, double b){
        return a % b;
    }

    public static double solve(Stack<String> a) throws LookAtMrAlgebraOverHereException, IllegalOperationException{
        Stack<String> ans = new Stack();
        Double temp;
        double one;
        double two;
        while (!postFixStk.isEmpty()) {
            if (precedence(a.peek()) == -1) {
                ans.add(a.pop());
            } else if (a.peek().equals("+")) {
                a.pop();
               >                 two = Double.parseDouble(ans.pop());
                temp = add(one, two);
                ans.add(temp.toString());
            } else if (a.peek().equals("-")) {
                a.pop();
               >                 two = Double.parseDouble(ans.pop());
                temp = subtract(two, one);
                ans.add(temp.toString());
            } else if (a.peek().equals("*")) {
                a.pop();
               >                 two = Double.parseDouble(ans.pop());
                temp = multiply(one, two);
                ans.add(temp.toString());
            } else if (a.peek().equals("/")) {
                a.pop();
               >                 two = Double.parseDouble(ans.pop());
                temp = divide(two, one);
                ans.add(temp.toString());
            } else if (a.peek().equals("%")) {
                a.pop();
               >                 two = Double.parseDouble(ans.pop());
                temp = modulo(two, one);
                ans.add(temp.toString());
            }
        }
        if(ans.size() > 1){
            throw new LookAtMrAlgebraOverHereException("This is an incomplete expression. " +
                    "Fix the expression and try again.");
        }
        return Double.parseDouble(ans.pop());
    }
}
-------------------------------------------------------------------------------------------------
IllegalOperationException.java
-----------------------------------------------------------------------
public class IllegalOperationException extends IllegalArgumentException {
    private String message;
    public IllegalOperationException(){
        System.out.println("An Illegal operation was performed.");
    }

    public IllegalOperationException(String message){
        System.out.println("An Illegal operation was performed.");
        this.message = message;
    }
    public String toString(){
        String string = new String();
        string += message;
        return string;
    }
}
-------------------------------------------------------------------------------------------------
LookAtMrAlgebraOverHereException.java
------------------------------------------------------------------------
public class LookAtMrAlgebraOverHereException extends IllegalArgumentException {
    private String message;

    public LookAtMrAlgebraOverHereException(){
        System.out.println("LookAtMrAlgebraOverHereException was thrown.");
    }
    public LookAtMrAlgebraOverHereException(String message){
        System.out.println("LookAtMrAlgebraOverHereException was thrown.");
        this.message = message;
    }

    public String toString() {
        String temp = new String();
        temp += message;
        return temp;
    }
}
--------------------------------------------------------------------------------------------------
UserIsADumbassException.java
----------------------------------------------------------------------
public class UserIsADumbassException extends IllegalArgumentException {
    String message;
    public UserIsADumbassException(){
        System.out.println("UserIsADumbassException was thrown.");
    }
    public UserIsADumbassException(String message){
        System.out.println("UserIsADumbassException was thrown.");
        this.message = message;

    }
    public String toString() {
        String temp = new String();
        temp += message;
        return temp;
    }
}

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