In: Computer Science
Java Program that prompts the user and reads in an integer between 0 to 50, inclusively. If the entered value is outside the range, program’s output will display the following message: “Error: the entered number is out of range”, and continually re-prompts the user to enter the integer again until the user enters a valid integer. • Once a valid integer has been entered, program prompts the user and reads in a second integer between -5 to 20, inclusively. If the entered value is outside the range, program’s output will display the following message: “Error: the entered number is out of range”, and continually re-prompts the user to enter the integer again until the user enters a valid integer. • Once both integers have been entered, program prompts for and read in the arithmetic operation that the user wish to perform: addition (+), subtraction (+), multiplication (*), division (/), or modulus (%). If the entered operation is other than those listed, program’s output will display the following message: “Error: Invalid operation”, and exit the program. • The requested operation will be performed, and program’s output will show details of the operation. • Division by zero can occur with the user input. In that scenario, program’s output simply display “Error: Division by zero” and exit the program. • You also need to find out whether the result of the operation is negative, positive or zero and program’s output will display accordingly. • Comments your code (variable declaration, blocks of code, calculation)
import java.util.Scanner;
public class Operations
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.println("Enter first integer between 0 to
50 inclusively");
int n1=input.nextInt();
while(true)
{
if(n1>=0 &&
n1<=50)
break;
System.out.println("Error: the
entered number is out of range");
System.out.println("Enter first
integer between 0 to 50 inclusively");
n1=input.nextInt();
}
System.out.println("Enter second integer between -5 to
20 inclusively");
int n2=input.nextInt();
while(true)
{
if(n2>=-5 &&
n2<=20)
break;
System.out.println("Error: the
entered number is out of range");
System.out.println("Enter second
integer between -5 to 20 inclusively");
n2=input.nextInt();
}
System.out.println("Enter operation you want to
perform: addition (+), subtraction (-), multiplication (*),
division (/), or modulus (%)");
char op=input.next().charAt(0);
int result=0;
if(op=='+')
{
result=n1+n2;
System.out.printf(String.format("Addition: %d %c %d =
%d\n",n1,op,n2,result));
}
else if(op=='-')
{
result=n1-n2;
System.out.printf(String.format("Subtraction: %d %c %d
= %d\n",n1,op,n2,result));
}
else if(op=='*')
{
result=n1*n2;
System.out.printf(String.format("Multiplication: %d %c
%d = %d\n",n1,op,n2,result));
}
else if(op=='/')
{
if(n2==0)
{
System.out.println("Error:
Division by zero");
System.exit(0);
}
result=n1/n2;
System.out.printf(String.format("Division: %d %c %d =
%d\n",n1,op,n2,result));
}
else if(op=='%')
{
result=n1%n2;
System.out.println("Modulus: "+n1+" "+op+" "+n2+"=
"+result);
}
else
{
System.out.println("Error: Invalid
operation");
System.exit(0);
}
if(result==0)
System.out.print("The result is zero");
else if(result>0)
System.out.print("The result is positive");
else
System.out.println("The result is negative");
}
}
Output