Exersice 121 Number format Exercise 129 Listing 79 Calcula
Exersice 12.1
(Number format )
Exercise 12.9
| * Listing 7.9, Calculator.java, is a simple commandline calculator. | |
| * Note that the program terminates if any operand is nonnumeric. | |
| * Write a program with an exception handler that deals with nonnumeric | |
| * operands; then write another program without using an exception handler | |
| * to achieve the same objective. Your program should display a message that | |
| * informs the user of the wrong operand type before exiting (see Figure 12.12). |
Solution
Answer Calculator.java
First part with try and catch block :
Program :
import java.util.*;
/*Class Calulator*/
public class Calculator{
/*Main method*/
public static void main(String args[]){
/*if(args.length !=3){
System.out.println(\"Please input properly : operand 1 operator operand 2\");
System.exit(0);
}*/
int no1 = 0;
int no2 = 0;
int answer = 0;
/*checking numer*/
try{
no1 = Integer.parseInt(args[0]);
}catch(Exception e){
System.out.println(\"Invalid Input : \" + args[0]);
}
try{
no2 = Integer.parseInt(args[2]);
}catch(Exception e){
System.out.println(\"Invalid Input : \" + args[2]);
}
/*if exception occur than catch will be executed*/
switch(args[1]){
case \"+\" :
System.out.println(\"Answer is : \" + (no1+no2));
break;
case \"-\" :
System.out.println(\"Answer is : \" + (no1-no2));
break;
case \"*\" :
System.out.println(\"Answer is : \" + (no1*no2));
break;
case \"/\" :
System.out.println(\"Answer is : \" + (no1/no2));
break;
default :
System.out.println(\"Invalid Input\");
break;
}
}
}
Sample Output :
c:\\example>java calculator \"4+7\"
Answer is : 11
c:\\example>java calculator \"9*6\"
Answer is : 54
c:\\example>java calculator \"8-3\"
Answer is : 5
c:\\example>java calculator \"e-4\"
Invalid operand : e
First part without try and catch block :
Program :
import java.util.*;
import java.util.regex.Pattern.*;
/*Class Calulator*/
public class Calculator{
/*boolean checking method*/
public static boolean isNumeric(String s){
return java.util.regex.Pattern.matches(\"\\\\d+\",s);
}
/*Main method*/
public static void main(String args[]){
/*if(args.length !=3){
System.out.println(\"Please input properly : operand 1 operator operand 2\");
System.exit(0);
}*/
int no1 = 0;
int no2 = 0;
int answer = 0;
boolean check = true;
/*checking numer*/
check = isNumeric(args[0]);
if(check ==false){
System.out.println(\"Invalid Input :\" + args[0]);
}
else{
no1 = Integer.parseInt(args[0]);
}
check = isNumeric(args[2]);
if(check ==false){
System.out.println(\"Invalid Input :\" + args[2]);
}
else{
no1 = Integer.parseInt(args[0]);
}
switch(args[1]){
case \"+\" :
System.out.println(\"Answer is : \" + (no1+no2));
break;
case \"-\" :
System.out.println(\"Answer is : \" + (no1-no2));
break;
case \"*\" :
System.out.println(\"Answer is : \" + (no1*no2));
break;
case \"/\" :
System.out.println(\"Answer is : \" + (no1/no2));
break;
default :
System.out.println(\"Invalid Input\");
break;
}
}
}
Sample Output :
c:\\example>java calculator \"4+7\"
Answer is : 11
c:\\example>java calculator \"9*6\"
Answer is : 54
c:\\example>java calculator \"8-3\"
Answer is : 5
c:\\example>java calculator \"e-4\"
Invalid operand : e


