1. Write a Java program called ArithmeticT1 that takes three command-line argume
ID: 3874579 • Letter: 1
Question
1. Write a Java program called ArithmeticT1 that takes three command-line arguments: two integers and in between an arithmetic operator (+, -, *, program shall perform the corresponding operation on the two integers and display the result. Note that you need to deal with the nonnumeric input as operands (one of the two integers) using exception handler (try-catch statements) - the program terminates if any operand is nonnumeric, and it should display a message with the wrong operand input before exiting. Besides, the program should also handle the wrong input for operator. C: Tutorials>java ArithmeticT1 12 34 12 34 46.0 C:Tutorials>java ArithmeticT1 12 34 12 34 -22. C: Tutorials jaua ArithmeticT1 12 34 12 × 34 408 .0 C: Tutorials>java ArithmeticT1 12 /34 12 34 0.35294117647058826 Example run of wrong input: Tutorialava ArithmeticT2 7/8 34 rong Input: 7/8 :Tutorial> java ArithmeticT2 78 3:4 rong Input: 3:4 2. (Using the FlowLayout manager) Write a program that meets the following requirements (see Figure 12.14): Create a frame and set its layout to FlowLayout Create two panels and add them to the frame. . Each panel contains three buttons. The panel uses FlowLayout.Explanation / Answer
Q1. Code of first question:
Note : I am using "x" for multiplication operator.
public class ArithmeticT1 {
//to check if a string is of numeric type or not
public static boolean isNumeric(String s){
try{
Integer.parseInt(s); //it will throw an exception if at least
//one character of the string is not
//numeric.So that string can not be
// converted into integer.
return true;
}
catch(Exception e){
System.out.println("Wrong Input: "+s);
return false;
}
}
//to check if the given operator is one of the four types "+", "-", "*" or "/".
public static boolean isCorrectOperator(String s){
try{
if (s.equals("+") || s.equals("-") || s.equals("x") || s.equals("/"))
return true;
else
throw new Exception(); //throwing exception
}
catch(Exception exp){
System.out.println("Wrong Input: "+s);
return false;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
if (args.length<3){ //if there is some missing argument
System.out.println("Some arguments are missing");
return;
}
if (args.length>3){ // if extra arguments are given
System.out.println("Extra arguments.Only 3 arguments are required");
return;
}
String operand1=args[0];
String operator=args[1];
String operand2=args[2];
if (isNumeric(operand1) && isNumeric(operand2) && isCorrectOperator(operator)){ //if everything is fine
int num1=Integer.valueOf(operand1);
int num2=Integer.valueOf(operand2);
if (operator.equals("+")){
System.out.println(num1+" + "+num2+" = "+(num1+num2));
}
else if (operator.equals("-")){
System.out.println(num1+" - "+num2+" = "+(num1-num2));
}
else if (operator.equals("x")){
System.out.println(num1+" x "+num2+" = "+(num1*num2));
}
else if (operator.equals("/")){
System.out.println(num1+" / "+num2+" = "+((num1*1.0)/num2));
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.