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 all the basic functions (addition, su

ID: 3716513 • Letter: W

Question

Write a command line calculator. Implement all the basic functions (addition, subtraction...) as well as the correct behavior for parenthesis. 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 AlgebraFailException derived from the IlleglArgumentException. If the user enters an operation which isn't supported, throw a QuitMashingOnYourKeyboardException 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. The user should never know what we think of them. 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?). If you research how to turn the command line arguments into postfix notation

Please give a complete solution if possible. And explain in brief.

Explanation / Answer

Hello as per your requirement i have written code which fulfill all your requirements please follow it step by step.

ProgramCalculator.java

------------------------------------------------------

import java.util.Stack;

public class Calculator {

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

public static void main(String[] args){

try {

String expression = new String();

String[] stringContent = new String[args.length + 2];

stringContent[0] = new String("(");

stringContent[args.length + 1] = new String(")");

for (int tempX = 1; tempX < args.length + 1; tempX++) {

stringContent[tempX] = new String(args[tempX-1]);

}

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

expression += stringContent[i];

}

System.out.println(expression);

makePostfix(stringContent);

System.out.println(postFixStack);

System.out.println(solve(postFixStack));

}catch(Exception e){

System.out.println(e);

}

}

public static void makePostfix(String [] tempA) throws LookAtAlgebraOverHereException,UserisAdumbassException,IllegalOperationException{

Stack<String> operations = new Stack();

Stack<String> temporary = new Stack();

String string = new String();

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

if(tempA[i].equals("+") || tempA[i].equals("-") || tempA[i].equals("*") || tempA[i].equals("/") || tempA[i].equals("%")){

if(operations.isEmpty()) {

operations.add(tempA[i]);

}

else{

if(precedence(operations.peek()) > precedence(tempA[i]) && !operations.peek().equals("(")) {

temporary.add(operations.pop());

operations.add(tempA[i]);

}

else{

operations.add(tempA[i]);

}

}

}

else if(tempA[i].equals("(")){

operations.add(tempA[i]);

}

else if(tempA[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{

temporary.add(operations.pop());

}

}

}

else{

char[] test = tempA[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 " + tempA[i]);

}

}

temporary.add(tempA[i]);

}

}

while(!temporary.isEmpty()){

postFixStack.add(temporary.pop());

}

System.out.println("Finish the changing to postfix.");

}

public static int precedence(String tempA){

if(tempA.equals("+") || tempA.equals("-")){

return 0;

}

else if(tempA.equals("*") || tempA.equals("/")|| tempA.equals("%")){

return 1;

}

else if(tempA.equals("(") || tempA.equals(")")){

return 2;

}

else return -1;

}

public static double add(double tempA, double b){

return tempA + b;

}

public static double subtract(double tempA, double b){

return add(tempA,b * -1);

}

public static double multiply(double tempA, double b){

return tempA * b;

}

public static double divide(double tempA, double b) throws IllegalOperationException{

if(b == 0.0){

throw new IllegalOperationException("There is tempA division by zero in your expression. " +

"Fix this error to get tempA result.");

}

return tempA / b;

}

public static double modulo(double tempA, double b){

return tempA % b;

}

public static double solve(Stack<String> tempA) throws LookAtAlgebraOverHereException, IllegalOperationException{

Stack<String> answer = new Stack();

Double temporary;

double one;

double two;

while (!postFixStack.isEmpty()) {

if (precedence(tempA.peek()) == -1) {

answer.add(tempA.pop());

} else if (tempA.peek().equals("+")) {

tempA.pop();

one = Double.parseDouble(answer.pop());

two = Double.parseDouble(answer.pop());

temporary = add(one, two);

answer.add(temporary.toString());

} else if (tempA.peek().equals("-")) {

tempA.pop();

one = Double.parseDouble(answer.pop());

two = Double.parseDouble(answer.pop());

temporary = subtract(two, one);

answer.add(temporary.toString());

} else if (tempA.peek().equals("*")) {

tempA.pop();

one = Double.parseDouble(answer.pop());

two = Double.parseDouble(answer.pop());

temporary = multiply(one, two);

answer.add(temporary.toString());

} else if (tempA.peek().equals("/")) {

tempA.pop();

one = Double.parseDouble(answer.pop());

two = Double.parseDouble(answer.pop());

temporary = divide(two, one);

answer.add(temporary.toString());

} else if (tempA.peek().equals("%")) {

tempA.pop();

one = Double.parseDouble(answer.pop());

two = Double.parseDouble(answer.pop());

temporary = modulo(two, one);

answer.add(temporary.toString());

}

}

if(answer.size() > 1){

throw new LookAtAlgebraOverHereException("Here this is an incomplete expression. " +

"Please Fix the expression and try again.");

}

return Double.parseDouble(answer.pop());

}

}

-------------------------------------------------------------------------------------------------

IllegalOperationException.java

-----------------------------------------------------------------------

public class IllegalOperationException extends IllegalArgumentException {

private String messageContent;

public IllegalOperationException(){

System.out.println("An Illegal operation was performed.");

}

public IllegalOperationException(String messageContent){

System.out.println("An Illegal operation was performed.");

this.messageContent = messageContent;

}

public String toString(){

String string = new String();

string += messageContent;

return string;

}

}

-------------------------------------------------------------------------------------------------

LookAtAlgebraOverHereException.java

------------------------------------------------------------------------

public class LookAtAlgebraOverHereException extends IllegalArgumentException {

private String messageContent;

public LookAtAlgebraOverHereException(){

System.out.println("LookAtAlgebraOverHereException was thrown.");

}

public LookAtAlgebraOverHereException(String messageContent){

System.out.println("LookAtAlgebraOverHereException was thrown.");

this.messageContent = messageContent;

}

public String toString() {

String temporary = new String();

temporary += messageContent;

return temporary;

}

}

--------------------------------------------------------------------------------------------------

UserisAdumbassException.java

----------------------------------------------------------------------

public class UserisAdumbassException extends IllegalArgumentException {

String messageContent;

public UserisAdumbassException(){

System.out.println("UserisAdumbassException was thrown.");

}

public UserisAdumbassException(String messageContent){

System.out.println("UserisAdumbassException was thrown.");

this.messageContent = messageContent;

}

public String toString() {

String temporary = new String();

temporary += messageContent;

return temporary;

}

}

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