Question

A Simple Calculator Summary: Write a console program (character based) to do simple calculations (addition, subtraction,...


A Simple Calculator Summary: Write a console program (character based) to do simple calculations (addition, subtraction, multiplication and division) of two numbers, using your understanding of Java. Description: You need to write a program that will display a menu when it is run. The menu gives five choices of operation: addition, subtraction, multiplication, division, and a last choice to exit the program. It then prompts the user to make a choice of the calculation they want to do. Once the user selects the operation, it will check for valid menu choices (and give an appropriate message if a wrong choice was selected) and then prompts the user to enter two numbers, separated by a space. If the user enters valid numbers, it will do the operation desired, and then displays the result. If the user enters invalid numbers, it displays an error message and asks for the correct input. After displaying the result, it displays Press enter key to continue. Once enter key is pressed, it displays the menu again. The program repeats until the user selects the choice to exit.
Sample Run of the Program: Sample Run of the program:
Welcome to <John Doe’s> Handy Calculator.

1. Addition.
2. Subtraction.
3. Multiplication
4. Division
5. Exit.

What would you like to do? 3.
Please enter two floats to multiply, separated by a space: 24.0 4.0.
Result of multiplying 24.00 and 4.00 is 96.00.
Press enter key to continue ....   

Welcome to <John Doe’>s Handy Calculator.
1. Addition.
2. Subtraction.
3. Multiplication.
4. Division.
5. Exit.
What would you like to do? 4.
Please enter two floats to divide separated by a space: 2.0 0.
You can’t divide by zero please re-enter both floats: asasfs asdfasfas
You have entered invalid floats please re-enter: 16.0 4.0. Result of dividing 16.0 by 4.00 is 4.00.
Press enter key to continue ....

Welcome to <John Doe’>s Handy Calculator.
1. Addition.
2. Subtraction.
3. Multiplication
4. Division.
5. Exit.
What would you like to do? 5.
Thank you for using <John Doe’>s Handy Calculator.   

Note : 1) Replace the <John Doe’s> with your name. 2) Make sure to properly word the output for the choices and result. If user selects ‘1’ from the menu, the prompt should be for ‘Addition’ not multiplication as shown in the example. 3) 3, 24.0 4.0 are shown in the example to emphasize that it is entered by the user and not part of the program. Underline and italics are not a requirement. 4) Your program should allow input of integer and decimal numbers (floats). The output should always be in decimals, with two decimal digits as precision. 5) Make sure your program will continue displaying the menu after the result is shown and user has pressed the enter key. Your program will exit only when user selects 5. 6) If the user selects an option other than 1-5, show a message that they must select a number between 1 and 5. Give them a chance to re-enter. Continue until a valid number is entered.
7) If they enter invalid values instead of numbers (e.g. strings), provide an error message and give them chances to re-enter. Continue until valid numbers are entered. You don’t need to worry about range of floats. 8) Make sure to catch the divide by zero issue. In the case of a division choice, the second number should not be a zero. The users should be given a second chance to enter other than a zero. 9) You should decompose your program in many static methods. So, you can call these static methods from main method to do the functionality required. Your class need not to be instantiated with a ‘new’. All methods are directly called from main of same class becausethey are static methods
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java Program for SImple Handy Calculator

import java.util.*;
import java.lang.*;
import java.text.DecimalFormat;//importing for decimal formats
class calculator
{
   static float input1,input2;
   public static void main(String[] args)
   {
       calculate(); //for calculations
   }
   public static void calculate()
   {
       Scanner input=new Scanner(System.in);
       int option=menu();//user choice input
       if(option>=1 && option<=5)
       {
           switch(option)
           {
               case 1:
                   add();//addition
                   break;
               case 2:
                   sub();//subtraction
                   break;
               case 3:
                   mul();//multiplication
                   break;
               case 4:
                   div();//division
                   break;
               case 5:
                   System.exit(0);//exit for the program
               default:
                   calculate();//if user prompts for wrong options

           }
       }
       else
       {
           System.out.println("You are supposed to Select Correct Options.");
           //used to show the Message to the User
           calculate();//to calculate again
       }
   }
   public static void add()
   {
       Scanner ai=new Scanner(System.in);//defining for taking inputs from console
       DecimalFormat df = new DecimalFormat(); //defining for decimal formats for 2 places
       df.setMaximumFractionDigits(2);
       System.out.println("Enter two values for Addition");
       try{
           input1=ai.nextFloat(); //first input from the user
           input2=ai.nextFloat(); //second input fro the user
           float result=input1+input2;
           System.out.print("Result of Adding "+input1+" and "+input2+" is :\t");
           System.out.println(df.format(result)); //printing result upto 2 deciaml places
           System.out.println("Press any key and press Enter");
           char temp=ai.next().charAt(0); //used for pressing enter
           if (temp!='~') {
               calculate();
           }
       }
       catch(Exception e)
       {
           System.out.println("You are Supposed to give input Correctly\n\n");
           //Messaging users to to give correct input
           calculate();
       }
      
   }
   public static void sub()
   {
       Scanner ai=new Scanner(System.in);//defining for taking inputs from console
       DecimalFormat df = new DecimalFormat();
       df.setMaximumFractionDigits(2);
       System.out.println("Enter two values for Subtraction");
       try{
           input1=ai.nextFloat();
           input2=ai.nextFloat();
           float result=input1-input2;
           System.out.print("Result of Subtracting "+input1+" and"+input2+" is :\t");
           System.out.println(df.format(result));
           System.out.println("Press any key and press Enter");
           char temp=ai.next().charAt(0);
           if (temp!='~') {
               calculate();
           }
       }
       catch(Exception e)
       {
           System.out.println("You are Supposed to give input Correctly\n\n");
           calculate();
       }
      
   }
   public static void mul()
   {
       Scanner ai=new Scanner(System.in);//defining for taking inputs from console
       DecimalFormat df = new DecimalFormat();
       df.setMaximumFractionDigits(2);
       System.out.println("Enter two values for Multiplication");
       try{
           input1=ai.nextFloat();
           input2=ai.nextFloat();
           float result=input1*input2;
           System.out.print("Result of Multiplying "+input1+" and "+input2+" is :\t");
           System.out.println(df.format(result));
           System.out.println("Press any key and press Enter");
           char temp=ai.next().charAt(0);
           if (temp!='~') {
               calculate();
           }
       }
       catch(Exception e)
       {
           System.out.println("You are Supposed to give input Correctly\n\n");
           calculate();
       }
      
   }
   public static void div()
   {
       Scanner ai=new Scanner(System.in);//defining for taking inputs from console
       DecimalFormat df = new DecimalFormat();
       df.setMaximumFractionDigits(2);
       System.out.println("Enter two values for Division");
       try{
           input1=ai.nextFloat();
           input2=ai.nextFloat();
           int temp1=(int)input2;
           while(temp1==0)
           {
               System.out.println("You cannot divide by zero.please re-enter");
               //to re take inputs from the user when Entered 0
               input2=ai.nextFloat();
               temp1=(int)input2;
           }
           float result=input1/input2;
           System.out.print("Result of Dividing "+input1+" by "+input2+" is :\t");
           System.out.println(df.format(result));
           System.out.println("Press any key and press Enter");
           char temp=ai.next().charAt(0);
           if (temp!='~') {
               calculate();
           }
       }
       catch(Exception e)
       {
           System.out.println("You are Supposed to give input Correctly\n\n");
           calculate();
       }
      
   }
   public static int menu()
   {
       Scanner mi=new Scanner(System.in);
       //here mi as menu_input
       System.out.println("Welcome to Adi's Handy Calculator\n");
       System.out.println("1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Exit");
       System.out.print("What would you like to do?\t");
       int choice; //taking choice from user
       choice=mi.nextInt();
       return choice; //return user choice to calculate
   }
}

Note: I took Entered key as an Any character input to exit.

Add a comment
Know the answer?
Add Answer to:
A Simple Calculator Summary: Write a console program (character based) to do simple calculations (addition, subtraction,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Python Program that displays repeatedly a menu as shown in the sample run below....

    Write a Python Program that displays repeatedly a menu as shown in the sample run below. The user will enter 1, 2, 3, 4 or 5 for choosing addition, substation, multiplication, division or exit respectively. Then the user will be asked to enter the two numbers and the result for the operation selected will be displayed. After an operation is finished and output is displayed the menu is redisplayed, you may choose another operation or enter 5 to exit the...

  • Creating a Calculator Program

    Design a calculator program that will add, subtract, multiply, or divide two numbers input by a user.Your program should contain the following:-The main menu of your program is to continue to prompt the user for an arithmetic choice until the user enters a sentinel value to quit the calculatorprogram.-When the user chooses an arithmetic operation (i.e. addition) the operation is to continue to be performed (i.e. prompting the user for each number, displayingthe result, prompting the user to add two...

  • This is a quick little assignment I have to do, but I missed alot of class...

    This is a quick little assignment I have to do, but I missed alot of class due to the Hurricane and no one here knows what theyre doing. please help! this is Java with intelli-J Overview In this project students will build a four-function one-run calculator on the command line. The program will first prompt the user for two numbers, then display a menu with five operations. It will allow the user to select an option by reading input using...

  • Task 2.5: A simple calculator (25 marks) In this task you will develop a MARIE program...

    Task 2.5: A simple calculator (25 marks) In this task you will develop a MARIE program that implements four basic functions of a calculator: addition, subtraction, multiplication, division, and exponent. The program should be performing the user selected tasks (one of the mentioned mathematical operations) after the user enters one of the selection inputs, ‘a’, ‘s’, ‘m’, ‘d’ or ‘p’. Here, input ‘a’ selects addition process, ‘s’ selects subtraction process, ‘m’ selects multiplication process, ‘d’ selects division process, and ‘p’...

  • (For Python program)   Write a program that fulfills the functionalities of a mathematical quiz with the...

    (For Python program)   Write a program that fulfills the functionalities of a mathematical quiz with the four basic arithmetic operations, i.e., addition, subtraction, multiplication and integer division. A sample partial output of the math quiz program is shown below. The user can select the type of math operations that he/she would like to proceed with. Once a choice (i.e., menu option index) is entered, the program generates a question and asks the user for an answer. A sample partial output...

  • Write a C++ Program that simulates a basic calculator using functions which performs the operations of...

    Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero) Display a menu for list of operations that can be calculated and get the input from user about his choice of calculation. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double. Calculations must be...

  • Write a C++ Program that simulates a basic calculator using functions which performs the operations of...

    Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero) Display a menu for the list of operations that can be calculated and get the input from the user about his choice of calculation. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double. Calculations...

  • Problem 3 (35) Design a calculator that performs four operations: addition, multiplication, division and subtraction with...

    Problem 3 (35) Design a calculator that performs four operations: addition, multiplication, division and subtraction with 2 integer type numbers a) Ask user what type of operation to perform (+, , * or/) a. If the user inputs 'none' then the program terminates. Otherwise it will keep continuing the calculation. (hint: use while) b) Ask user to provide 2 integer number inputs c) Perform operation (whatever operation they mentioned in a) d) Print result e) In division operation, perform a...

  • In this lab you will code a simple calculator. It need not be anything overly fancy,...

    In this lab you will code a simple calculator. It need not be anything overly fancy, but it is up to you to take it as far as you want. You will be creating this program in three small sections. You have the menu, the performance of the basic arithmetic operations (Addition, Subtraction Multiplication, and Division), and the looping. You may want to get the switch menu working first, and then fill in the code for these four arithmetic operations...

  • Read description carefully. Needs to catch exceptions and still be able to keep the program going...

    Read description carefully. Needs to catch exceptions and still be able to keep the program going on past the error. Program should allow user to keep going until he or she quits Math tutor) Write a program that displays a menu as shown in the sample run. You can enter 1, 2. 3, or 4 for choosing an addition. subtraction, multiplication, or division test. After a test is finished, the menu is redisplayed. You may choose another test or enter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT