Question: JAVA PROGRAM Purpose The purpose of this assessment is to use nested if-else and switch constructs appropriately and efficiently to create a simple menu-driven calculator.

JAVA PROGRAM Purpose

The purpose of this assessment is to use nested if-else and switch constructs appropriately and efficiently to create a simple menu-driven calculator. You will also show your ability to apply information from the Code Conventions and Template to organize and properly format your program's comments, code, and output.

Specifications

Professionally ask the user for two numbers, and then, display a professionally formatted menu to the user with an appropriate title and list of options; see the output examples below. The options should include the following:

  • Add the two numbers and display the result
  • Subtract the two numbers and display the result
  • Multiply the two numbers and display the result
  • Divide the two numbers and either display the result or an appropriate error message concerning the cannot divide by zero issue
  • Calculate the average of the two numbers and display the result; the above error may also apply for this option
  • Find and display the largest of the two numbers
  • Display if each number is negative, positive, or zero; because we haven't covered loops yet, you will need a set of nested if-elses for each number, so these two stacked sets of nested if-elses will be the only stacked if/if-else constructs you will need
  • Quits without doing anything other than to display a good-bye message

Other aspects of the program are as follows:

  • The user should be able to enter either an uppercase or lowercase letter for the menu options, NOT numbers
  • The program should use a switch to process the menu choices, and nested if-else constructs as appropriate, NOT stacked ones, except as noted above
  • If the choice is not on the menu, an appropriate error message should be displayed
  • Each time the program is run, it will either display an answer, error message, or nothing, and a good-bye message, and quit; in other words, do NOT use loops, other use-defined methods, or arrays for this program, i.e., stick to the concepts covered through Module 3

Output

All output should be written professionally, e.g., sentences begin with a capital letter and end with punctuation, single blank lines are used to separate the different parts, etc. Error messages should be offset and surrounded by white space using the following format: Error: Replace this with an appropriate error message! Below are incomplete examples of what your output might look like, but feel free to make it your own as appropriate. The ellipses, i.e., ..., would be replaced with other menu options. Input from the user is bolded.

Enter two numbers separated by a space: 1 5.5 Calculator Menu (A)dd. (S)ubtract. (M)ultiply. (D)ivide. ... e(X)it. Choice: a The sum is 6.500. Thank you for using David's simple calculator! Good-bye!
Enter two numbers separated by a space: 1 0 Calculator Menu (A)dd. (S)ubtract. (M)ultiply. (D)ivide. ... e(X)it. Choice: D Error: Cannot divide by zero! Thank you for using David's simple calculator! Good-bye!
Enter two numbers separated by a space: -1 5.5 Calculator Menu (A)dd. (S)ubtract. (M)ultiply. (D)ivide. ... e(X)it. Choice: b Error: Invalid choice! Thank you for using David's simple calculator! Good-bye!
Enter two numbers separated by a space: 1 -5.5 Calculator Menu (A)dd. (S)ubtract. (M)ultiply. (D)ivide. ... e(X)it. Choice: x Thank you for using David's simple calculator! Good-bye!

Testing

Make sure you thoroughly test your program, e.g., did you test using negative and positive numbers, and zeros, did you test all options, did you test all error-causing possibilities, etc.

Code Conventions

Your comments and code must thoroughly follow the Code Conventions and demonstrate good programming practices. Here are some reminders:

  • Make sure you start with the Template changing and replacing the comments and code as necessary
  • Make sure you organize your code using heading-type comments and single blank lines, for example:
    // Constants and Variables ... // Input ... // Menu ... // Menu Processing ...
  • Use meaningful names for constants and variables, and make sure they are formatted appropriately
  • Define constants for numbers other than -1, 0, or 1
  • Declare all variables at the top of the method
  • Initialize all variables, e.g., to zeros, appropriately formatted for their data types
  • Use tabs, NOT multiple spaces, to line up your comments and code
  • Use single spaces appropriately, e.g., after commas, before and after binary operators, after keywords like if and switch, etc.

Template Instructions 1. Set up your IDE as specified in Module 1. 2. Create an empty Java project and source file. 3. Copy the comments and code below the Code heading, just like it is, into the blank source file keeping the following in mind: Everything should line up once you paste it into your IDE if you have set it up correctly, i.e., a tab should be the equivalent of four spaces, etc. Do NOT use multiple spaces in your code, unless it is intention within a set of quotes The following lines of code are incomplete and may need to be deleted or updated; if you delete those lines, make sure you leave a single blank line between the different sets of comments: package // Packages import Eclipse for Java should be set up correctly by default, so you can use Source Format (Ctrl+Shift+F) to format your code, which should match the Code Conventions for the Java Programming Language; you can find a link in the Resources Overview in the Modules in Canvas 4. Change the code appropriately to create yourself a template based on the following information: All your source code should have a ".java" extension without the quotes The File Name should be about the program, NOT the assignment, e.g., calculator.java, NOT A1.java; format the names consistently For an overall example of how to organize your comments and code, see the Code Conventions under the heading: File Organization. Let me know if the Code Conventions need updating based on the information, comments and code in this file. Notice how there is a star on every line of a multiline comment, and the stars line up and so do the comments after the colons The Description should be a small paragraph describing the entire program, NOT the assignment, from the users point of view in plain English keeping the following in mind: o Sentences start with a capital letter and end with punctuation, proofread your comments, etc. o Do NOT refer to any specific code or programming concepts within the description o See the examples in the Code Conventions and elsewhere in Canvas The Date should be formatted like this: m/d/yy, e.g., 1/15/21, and updated every time the program is changed, except when copying your code into a quiz unless otherwise specified; it is also a good idea to keep an ongoing record of the changes, for example: 5/19/20 - Added getting additional information from the user, i.e., ... 5/18/20 - Changed how the output gets displayed, i.e., ... Remove any extra blank lines, i.e., there should only be one blank line between the different parts of your code; see the examples below, in the Code Conventions, and throughout Canvas Concerning each class: o Each class should be in its own file with the class name the same as the file name excluding the file extension o The class Javadoc comments should go immediately above the first line of the class definition: Write a paragraph describing the class like the Description above, but as shown below Include an @author line for each person involved in writing the class Include an @version line followed by a version number followed by the date formatted like above Concerning the Javadoc comments above each method, i.e., Parameters, Processes, and Return Value (PPR) comments, which may be referred to as Input, Process, and Output (IPO) comments, you will need to create a similarly related set of comments for every method you create using the following information: o If you were to give your compiled code, e.g., in a library, along with the PPR comments, the only thing the other programmer would see is your PPR comments and the method header, everything else is your secret, so do not refer to any specific code within the braces of the method o Think of each method as a self-contained and self-sufficient island, so the comments should only include the information the other programmer would need to know to use the method without telling them exactly how the interior of the method works o The Processes or method description is where you would specify what the method does in plain English like writing the Description for the entire program but limited to the current method keeping the following in mind: Do NOT put any references to code within the braces Do NOT refer to other methods If the method does or does not do something another programmer would need to know, make sure you include that information, e.g., does not handle the divide by zero issue o The Parameters is where you would list and explain the items in the method's parameter list, i.e., the items between the parentheses of the method, NOT the input from the user, keeping the following in mind: Each parameter should be on its own line beginning with @param followed by a description, etc. Include any ranges o The Return Value is where you would specify information about what the method returns, i.e., concerning the return-type and what data or information is returned to the method call by the return statement at the end of the method, NOT the output to the user, keeping the following in mind: Do NOT include a return value comment for void methods The return value begins with @return followed by a description of what is returned o See the example in the Code Conventions under the heading: Method Declarations Put ALL your Constants and Variable declarations and definitions at the top of your methods, NOT later within your code keeping the following in mind: o This is a good habit, so you know where to find and easily change any constant/variable definitions o It is always a good idea to appropriately initialize your variables, e.g., to zeros Put the rest of your code after all the variable declarations and definitions using the following guides: o Organize it well, for example: First, get user input, second, do calculations, and third, display output to the user Do not forget to use heading type comments, e.g., // Input, // Calculations, // Output, etc. Separate each section with a single blank line o Use white space appropriately, e.g., do not put a single blank line after a comment when the comment is about the next line of code, put a single space before and after operators, etc. o See the Code Conventions for more information Code /* * File Name: Replace this text with your file name. * Names: Replace this text with your first and last name, and anyone else involved. * Course: COP 2800C at Valencia College * Professor: David Stendel * Description: Replace this text; see the instructions in the Template. * Date: 1/15/21 */ package // Packages import /** * Replace this with a description of your Java class. You can include HTML code as * these comments can be used to generate web pages. * * @author Your Name * @version 1.0, 1/15/21 */ public class Application { /** * Replace this with a professional paragraph about the Processes, which describes in * plain English what this method does, i.e., you should not refer to any specific code * beyond the parameters in the method's signature. You can include HTML code as these * comments can be used to generate web pages. * * @param args A String array containing any command line options. * @return Replace this with a description of what is returned by the method, e.g., A * String representing a person's first name. Delete these return lines for void * methods and constructors. */ public static void main(String[] args) { // Constants and Variables } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!