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...
Using Java Prime numbers. Write a program that prompts the user for an integer and then...
Using Java Prime numbers. Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer. For example, when the user enters 20, the program should print 2 3 5 7 11 13 17 19 Recall that a number is a prime number if it is not divisible by any number except 1 and itself. Use a class PrimeGenerator with methods nextPrime and isPrime. Supply a class PrimePrinter whose main method...
python programming. Write a program that prompts the user to enter an integer from 0 to...
python programming. Write a program that prompts the user to enter an integer from 0 to 9. The program will check if the input is a positive integer. It displays the correct answer and shows the guess is correct or not. The demos are shown as following.(Hint:1. Use a random math function 2. Use str.isdigit() to check the input)
Java Write a program that reads 4 integer numbers from the user combine it into a...
Java Write a program that reads 4 integer numbers from the user combine it into a single number. You need to ask the user to first specify how to combine the numbers (forward or backward). Note that for the backward option you need to check that the last digits should be 0, also for the forward option you need to check that the fist digit is not 0. Use for loops and switch for the menu.                       4. Write an application...
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...
Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers...
Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers using a sentinel while loop. The program should accept integer inputs until the user enters the value -1 (negative one is the sentinel value that stops the while loop). After the user enters -1, the program should display the entered numbers followed by their sum as shown below. Notice that -1 is not part of the output. The program should ignore any other negative...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT