Question

In: Computer Science

Introduction to Java Programing Using Loop Create a simple calculator program using loop Ask user to...

Introduction to Java Programing

Using Loop

  • Create a simple calculator program using loop
  • Ask user to input two numbers using scanner class
  • Print the instruction of the menu for the calculator program
  • Ask user to press 0 to Quit
  • Ask user to press 1 to Add
  • Ask user to press 2 to Substract
  • Ask user to press 3 to Multiply
  • Ask user to press 4 to Divide
  • Perform correct calcuation based on user inputs and print the result
  • Print error message if user enter wrong input
  • Repeate the process until user quits the application.

Solutions

Expert Solution

Please find the below java code:

//import statements

import java.util.*;
import java.lang.*;

//main class

public class ArithmeticOperation {
   public static void main(String[] args){
   System.out.println("Arithmetic Operations");
   Scanner sc=new Scanner(System.in);
   System.out.println("please enter first number");
   int num1=sc.nextInt();// reading first number
   System.out.println("please enter second number");
   int num2=sc.nextInt();// reading second number
   int select=1;
       while(select!=0)// condition for quit
       {
       int choice=selectoperation();//calling function for options and returning the selection
       select=choice;
       performoperation(choice,num1,num2);//calling function for performing arithmetic calculation and priting the result
      
       }
      
   }
   static int selectoperation(){
       System.out.println("Please select one of the options below ");
       System.out.println("0.Quit \n1.Add \n2.Substract \n3.Mutiply \n4.Divide");
       Scanner sc=new Scanner(System.in);
       int c=sc.nextInt();
      
       return c;
   }
   static void performoperation(int d,int num1,int num2){
       switch(d){
           case 1 :
           System.out.println("Selected option is Addition");
           System.out.println("Addition:"+(num1+num2));
           break;
           case 2 :
           System.out.println("Selected option is Substraction");
           System.out.println("Substraction:"+(num1-num2));
           break;
           case 3 :
           System.out.println("Selected option is Multiplication");
           System.out.println("Multiplication:"+(num1*num2));
           break;
           case 4 :
           System.out.println("Selected option is Division");
           float div=(float)num1/(float)num2;
           System.out.println("Division:"+div);
           break;
           case 0 :
           System.out.println("Selected option is Quit");
           System.out.println("Quitting");
           break;
           default :
           System.out.println("Invalid selection");
          
       }
      
   }

}


Related Solutions

Create a program that will ask the user to choose their order from a simple menu....
Create a program that will ask the user to choose their order from a simple menu. Customers first choose from three types of dishes: Sandwiches/wraps, Rice Meals, or Noodles. They can type in 1, 2, or 3 to select the type then, they choose a specific dish as shown below. Confirm their order by displaying it on the screen after they make their selection. 1. Sandwiches/wraps Hamburger Chicken shawarma 2. Rice meals Arroz con pollo Chana masala 3. Noodles Chow...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will first enter a password, then enters the same password again; If the second input is the same as first one, the user successfully creates the password. Print “Well done.”; Otherwise, the user will be directed to repeat the whole process (go to step 1.)
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
Using Java! Write a program that ask prompt the user to enter a dollar amount as...
Using Java! Write a program that ask prompt the user to enter a dollar amount as double. Then, calculate how many quarters, dimes, nickels and pennies are in the dollar amount. For example: $2.56 = 10 quarters, 1 dime, 1 nickel and 1 cent. Print all of the values. Hint: Use Modulus operator and integer division when necessary.
Create a program using python that provides a simple calculator: Requires a login with a prompt...
Create a program using python that provides a simple calculator: Requires a login with a prompt for a username and a password prior to using the calculator If username and password are incorrect, the program should re-prompt to re-enter correct username and password Once logged in, the user should have access to the calculator and should have the ability for the following mathematical operators: Addition Subtraction Multiplication Division Square Root PI Exponents
using C language Create a bitwise calculator that ask user for input two input, first int,...
using C language Create a bitwise calculator that ask user for input two input, first int, bitwise operation, second int (i.e 5 & 9) only those bitwise operation are allowed: & ~ ^ | << >>. If user uses wrong operators stop program and ask again. Convert the first int and second int into 8 BITS binary (00000000) and use bitwise operator given by user to either AND, OR, XOR, etc (ie 1001 & 1111)
create a program using IDLE where you will gather input from the user using a loop....
create a program using IDLE where you will gather input from the user using a loop. The user needs to provide you with a list of test scores for two categories (two different classrooms). Test scores must be integer values between 0 and 10. You need to process each score so that you can output the following: Number of test scores entered for classroom A. Number of test scores entered for classroom B. Average of test scores entered for classroom...
create a program using IDLE where you will gather input from the user using a loop....
create a program using IDLE where you will gather input from the user using a loop. The user needs to provide you with a list of test scores for two categories (two different classrooms). Test scores must be integer values between 0 and 10. You need to process each score so that you can output the following: Number of test scores entered for classroom A. Number of test scores entered for classroom B. Average of test scores entered for classroom...
Write a program that uses a while loop with a priming read to ask the user...
Write a program that uses a while loop with a priming read to ask the user to input a set positive integers. As long as the user enters a number greater than -1, the program should accumulate the total, keep track of the number of numbers being entered and then calculate the average of the set of numbers after the user enters a -1. This is a sentinel controlled-loop. Here is what a sample run should look like: Enter the...
Write a Python program that has a loop to continuously ask the user for a number,...
Write a Python program that has a loop to continuously ask the user for a number, terminating the loop when the number entered is -1. Inside the loop, 1.) display one asterisk(*) if the number is 1, 2.) two asterisk(**) if the number is 2 and 3.) "OTHER" if the number is any other number.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT