Question: Make a copy and fix any issues concerning the A3 - Simple Calculator program; see my comments in the submission area. If I have not
- Make a copy and fix any issues concerning the A3 - Simple Calculator program; see my comments in the submission area. If I have not provided any comments at this point, please e-mail me and ask me to review your A3 submission.
- Modify the A3 - Simple Calculator program to create an efficient program thoroughly following the Code Conventions using a do-while loop and switch construct appropriately to display and process a calculator, event-driven menu based on the following information:
- The following items must be completed or you will automatically get a zero for this assignment!
- Use only the concepts covered through Module 4; see the Module Overviews, videos, and notes; do NOT use any arrays, user-defined methods other than main(), etc.
- Follow good programming practices, e.g., variables are declared at the top of methods, meaningful variable names, initialized variables, code is organized using heading-type comments, e.g., // Constants and Variables, // Menu, // Menu Processing, separate code sections with a single blank line, etc.
- Do NOT use anyone else's code including from the book, Internet, publisher, etc.
- Change the menu to include an option for getting the two numbers
- Change your code, so displaying and processing the menu is inside a do-while loop; see the Sample Run section below
- If a letter other than the ones listed on the menu is entered, an appropriately formatted error message displays
- If any of the other options, excluding the exit option, is selected from the menu before the two numbers are entered by the user, an error message should be displayed indicating the user must select the getting two numbers option first before selecting this option
- If any other option except the exit option is selected, either the user will be asked to enter two numbers, or the appropriate results or an error message will display, and then, the menu will display again; in other words, it loops back around
- If the exit option is selected, a good-bye message is displayed, and the loop and program quit
- Make sure your output is professional looking; see the Output section of A3 - Simple Calculator and the Sample Run section below.
- Once you have completed and thoroughly tested your program, make sure you show your work to the professor before leaving class, or if this is an online course, copy and paste your code from your IDE into a Microsoft Word document and do the following steps:
- Do NOT reformat your code after pasting it into Word!
- Save and close the Word document.
- Attach the document to this assignment.
- Submit it.
- Verify the document you just submitted is the correct one.
Sample Run
(The ellipses, i.e., ..., would be replaced with other menu options. Input is in bold.)
Menu (G)et two numbers. (A)dd. (S)ubtract. (M)ultiply. (D)ivide. ... e(X)it. Choice: a Error: Please select option G from the menu first! Menu (G)et two numbers. (A)dd. (S)ubtract. (M)ultiply. (D)ivide. ... e(X)it. Choice: g Enter two numbers separated with a space: 1 0 Menu (G)et two numbers. (A)dd. (S)ubtract. (M)ultiply. (D)ivide. ... e(X)it. Choice: A The sum is 1.000. Menu (G)et two numbers. (A)dd. (S)ubtract. (M)ultiply. (D)ivide. ... e(X)it. Choice: m The product is 0.000. Menu (G)et two numbers. (A)dd. (S)ubtract. (M)ultiply. (D)ivide. ... e(X)it. Choice: X Thank you for using David's calculator! Good-bye!
HERE IS MY A3 Simple Calculator PROGRAM
import java.util.*;
public class Main { public static void main(String[] args) { int num1,num2,result=0,average=0,large; Scanner sc=new Scanner(System.in); System.out.println("Enter two numbers separated by space:"); num1=sc.nextInt(); num2=sc.nextInt(); System.out.println("Calculator Menu"); System.out.println(); System.out.println("(A)dd"); System.out.println("(S)ubtract"); System.out.println("(M)ultiple"); System.out.println("(D)ivide"); System.out.println("e(X)it"); char option=sc.next().charAt(0); char option1=Character.toLowerCase(option); switch(option1) { case 'a': { if(num1>0 && num2>0) { result=num1+num2; System.out.println("Addition of two numbers is "+result); } break; } case 's': { if(num1>0 && num2>0) { result=num1-num2; System.out.println("Subtraction of two numbers is "+result); } else{ System.out.println("Negative Numbers!"); } break; } case 'm': { if(num1>0 && num2>0) { result=num1*num2; System.out.println("Multiplication of two numbers is "+result); } else { System.out.println("Negative numbers");break; } } case 'd': { if(num1>0 && num2>0) { result=num1/num2; System.out.println("Division of two numbers is "+result); } else if(num1==0 || num2==0) { System.out.println("Cannot divide by 0"); } break; } case 'x': { System.out.println("Exit"); break; } default: System.out.println("Invalid choice!"); } average=(num1+num2)/2; System.out.println("Average of two number is "+average); if(num1>num2) { System.out.println("Number1 is largest"); } else { System.out.println("Number2 is largest"); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
