Type the following code into a file called Expressions.java. After the program i
ID: 3872071 • Letter: T
Question
Type the following code into a file called Expressions.java. After the program is entered, compile and run the application to make sure it works. Each invocation of println outputs an arithmetic expression. The first println command is followed by comment that shows the output and describe the operation that occur in the first expression. Complete the program by adding a comment after each println statement that describes all the arithmetic operations that occur when evaluating the expression that is printed.
Explanation / Answer
1.This question doesnt have the program that has to be completed, So I have written my own to guide
2.Write a notepad file names Expressions.java and copy the following code in it.
3.Compile and run
Expressions.java:
import java.util.Scanner;
class Expressions
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("YOU HAVE FOLLOWING CHOICES : ");
System.out.println("1. ADDITION");
System.out.println("2. SUBTRACTION ");
System.out.println("3. MULTIPLICATION ");
System.out.println("4. DIVISION");
System.out.println("ENTER YOUR CHOICE : ");
int i=s.nextInt();
System.out.println("ENTER FIRST NUMBER ");
int a=s.nextInt();
System.out.println("ENTER SECOND NUMBER ");
int b=s.nextInt();
double result=0;//'result' will store the result of operation
switch(i)
{
case 1:
result=a+b;
System.out.println("You have chosen Addition and the numbers chosen to be added are "+a+" "+b);
System.out.println("The result is "+ a +" "+"+"+ b +" = "+result);
break;
case 2:
result=a-b;
System.out.println("You have chosen Subtraction and the numbers chosen to be subtracted are "+a+" "+b);
System.out.println("The result is "+ a +" "+"-"+ b +" = "+result);
break;
case 3:
result=a*b;
System.out.println("You have chosen Multiplication and the numbers chosen to be multiplied are "+a+" "+b);
System.out.println("The result is "+ a +" "+"*"+ b +" = "+result);
break;
case 4:
try {
result=a/b;
System.out.println("You have chosen Division and the numbers chosen to be divided are "+a+" "+b);
System.out.println("The result is "+ a +" "+"/"+ b +" = "+result);
} catch (Exception e) {
System.out.println("division by zero exception");
}
default:
System.out.println("YOU HAVE ENTERED A WRONG CHOICE");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.