Question

In: Computer Science

Java Program that prompts the user and reads in an integer between 0 to 50, inclusively....

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)

Solutions

Expert Solution

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


Related Solutions

JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1 and 100 inclusive. The user will be repeatedly prompted until they input a valid integer.
Write a program which: Prompts the user for a positive integer >= 0 Validates the user...
Write a program which: Prompts the user for a positive integer >= 0 Validates the user input to ensure it is a positive integer >= 0 Allocate (dynamically) an array big enough for the data. Load the array with random numbers ranging in value from1 to 100 Display the elements of the array (unsorted) Display the elements of the array (sorted) Display the average Display the median Display the mode, if none, display appropriate message RESTRICTIONS No global variables No...
Java Programming Create a program that prompts the user for an integer number and searches for...
Java Programming Create a program that prompts the user for an integer number and searches for it within an array of 10 elements. What is the average number of comparisons required to find an element in the array? Your program should print the number of comparisons required to find the number or determine that the number does not exist. Try finding the first and last numbers stored in the array. Run your program several times before computing the average.
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
1. Write a program that prompts the user for a filename, then reads that file in...
1. Write a program that prompts the user for a filename, then reads that file in and displays the contents backwards, line by line, and character-by character on each line. You can do this with scalars, but an array is much easier to work with. If the original file is: abcdef ghijkl the output will be: lkjihg fedcba Need Help with this be done in only PERL. Please use "reverse"
JAVA Program Write a program that prompts the user for data until the user wishes to...
JAVA Program Write a program that prompts the user for data until the user wishes to stop (you must have a while loop) (You must read in at least 8 to 10 sets of voter data using dialog boxes) The data to read in is: The registration of the voter (Democrat, Republican or other) The gender of the voter The Presidential candidate the voter is choosing (Trump or Biden) Which candidate has done better to manage the economy? (Trump or...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Problem 4 : Write a program that prompts the user to enter in an integer and...
Problem 4 : Write a program that prompts the user to enter in an integer and then prints as shown in the example below Enter an integer 5 // User enters 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Bye
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT