ram reads user input with command line argument, two inte ment, two integers and
ID: 3736580 • Letter: R
Question
ram reads user input with command line argument, two inte ment, two integers and in between tor. The programme will determine whether the input is valid, an rompt info if not. Read the codes, design the isNumber0 method t progran an operator. The program. (5 marks) public class ArithmeticTest ( public static void main(Stringl] args) ( if (isNumber(args[e]) & isNumber(args[2])) ( int value1 Integer.parseInt (args[e]); int value Integer.parseInt (args [2]); char op args[1].charAt (e); calcAndDisplay (value1, value2, op); ) else f system.out.println("Your enter for operator is invalid!"); else f System . out.println("Your enter for integer is invalid!"); //write isNumber method here public static void calcAndDisplay(int vall, int val2, char op) double result e; switch(op) f case '+': result = val1 + val2; break; case ,-': result val1 - val2; break; case 'x': result - val1 va12; break; case ,1': result = val1 / val2; break;Explanation / Answer
public class ArithmeticTest
{
public static void main(String[] args)
{
if(isNumber(args[0]) && isNumber(args[2]))
{
int value1=Integer.parseInt(args[0]);
int value2=Integer.parseInt(args[2]);
char op = args[1].charAt(0);
if(op == ' +' || op == '-' || op == '*' || op == '/')
{
calcAndDisplay(value1, value2, op);
}
else
{
System.out.println("your enter for operator isinvalid ");
}
}
else
{
System.out.println("your enter for integer is invalid ");
}
}
}
public void isNumber()
{
if ( value1 >= 0 && value <=9 ) || (value2 >= 0 && value2 <= 9)
{
System.out.println("Enter value is no.....");
}
else
{
System.out.println("Enter value is not number.....");
}
}
public static void calcAndDisplay( int val1, int val2, char op)
{
double result = 0;
switch(op)
{
case ' + ' : result = val1 + val2;
break;
case ' -' : result = val1 - val2;
break;
case ' + ' : result = val1 + val2;
break;
case ' * ' : result = val1 * val2;
break;
case ' / ' : result = val1 / val2;
break;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.