Java Write a command line calculator. Implement the (), +, -, /, *, and % functi
ID: 3593430 • Letter: J
Question
Java
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
import java.lang.*;
import java.io.*;
class LookAtMrAlgebraOverHereException extends IllegalArgumentException{
String str1;
LoikAtMrAlgebraOverHereException(String str2)
{ str1=str2;
}
public String toString(){
return("LookAtMrAlgebraOverHereException Occurred: "+str1);
}
}
class UserIsADumbassException extends LookAtMrAlgebraOverHereException{
String str1;
UserIsADumbassException(String str2){
str1=str2;
}
public String toString(){
return("UserIsADumbassException Occurred: "+str1);
}
}
class IllegalOperationException extends UserIsADumbassException{
String str1;
IllegalOperationException(String str2){
str1=str2;
}
public String toString(){
return ("IllegalOperationException Occurred: "+str1);
}
}
public class Practical extends IllegalOperationException{
Public static void main(String args[]){
if args.length==0)
{ System.out.println("NO arguments are passed"); }
else{
try{
int a=Integer.paersInt(args[0]);
throw new LookAtMrAlgebraOverHereException("You enters anythings other than a number");
throw new UserIsADumbassException("you forgot to enter a number");
String p=args[1];
throw new IllegalOperationException("you enters an operationwhich is not supported");
int b=Integer.parseInt(args[2]);
throw new LookAtMrAlgebraOverHereException("you enter anything other than a number);
throw new UserIsDumbassException("You forgot toenter a number");
switch(p)
{
case "+":
System.out.println("Addition of "+a+ "and" +b+":"+(a+b));
break;
// same way "-", "*"and"%"
case "/"":
try{ System.out.println("Division of"+a+ "and"+b+":" +(a/b));}
catch( ArithmaticException ex)
{ System.out.println("Divided by zero");
break;
}
catch(IllegalArgumntException ex){}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.