Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a simple menu driven four-function integer calculator that takes the commands add, sub, mul and div and executes the corresponding calculation. Your program should

Write a simple menu driven four-function integer calculator that takes the commands add, sub, mul and div and executes the corresponding calculation. Your program should be case insensitive and also be able to handle leading and trailing blanks. (Consult the Java API to find the appropriate String methods). Be sure your program can pass those tests in addition to giving the correct results. The program continues to run until the user tells the program to stop. Valid answers to the Do you want to continue (y)? question are only the characters y and n (upper or lower case), or alternatively, if you prefer you can substitute case-insensitive strings (yeso) in place of the single character responses. If any other input is provided, issue a message to System.err stating that this was an invalid input along with the incorrect choice. If the input is incorrect, you should terminate the loop. You MUST use the functions (names and parameters) below as specified, so read these instructions carefully.

At the start of the program, and at the end display your program banner via the banner() function.

banner: displays your banner, calls printLine twice

printLine: prints a line of a specified length and character. Takes a character count, and two characters. The first character will be used for the majority of the line. The 2nd character is used to start and end the line. E.g., printLine(10, -, +) would print 8 - surrounded by +: +--------+

In your main loop which must be controlled by a Boolean variable - you must have these functions:

showMenu: This function receives a variable of type Scanner so that it can ask the user for some input. It displays the menu of valid commands (add/sub/mul/div) and returns a lower case version of the string the user entered. If the command is not valid, return the string containing invalid. You can either use a switch statement or if statement(s) to determine the validity of the input. Switch will be easier/cleaner, IMHO :)

doMath: Only called if the user has specified a valid command. Send the valid command and two integers to the function for execution. You must use a switch statement for this code. The function returns the result of the math operation, i.e., an integer.

printResult: Only called if the user has specified a valid command. Send the valid command, the two numbers and the result to this function. The function displays the result, it doesnt return anything.

This means you will have a total of at least five methods in addition to main. Reminder: the main method comes first in your source code. Project and Class name SuperCalc (yes, both are the same :)

Basic outline of Program (you determine what parts are part of the loop):

1. Display banner (once)

2. Ask the user for two integers (not doubles)

3. Display the menu of valid commands

4. If the command was invalid a. Issue an informative error message to System.err

5. Else (the command was valid) a. Compute the result (an integer) b. Display the result

6. Ask the user if they want to continue doing math.

7. At the end of program run display banner (once)

Tip: Before you do your string comparison, convert your string to lower case and get rid of any leading and trailing blanks, itll make your code shorter and easier.

image text in transcribed

Add Sub Mul Div choice? ack Invalid command. Do you want to continue (y)? y Performing one more calculation. Enter first number: 33 Enter second number: 99 MENU Add Sub Mul Div choice? add 33 add 99 132 Do you want to continue (y)? n my banner.. Example of what happens if an invalid choice is entered. Error message to System.err (shows up in red, and out of sequence) my banner. Enter first number: 1234 Enter second number: 5678 MENU Add Sub Mul Div choice? add 1234 add 5678 6912 Do you want to continue (y)?q ERROR: Invalid choice my banner Terminating program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 23rd European Conference Adbis 2019 Bled Slovenia September 8 11 2019 Proceedings Lncs 11695

Authors: Tatjana Welzer ,Johann Eder ,Vili Podgorelec ,Aida Kamisalic Latific

1st Edition

3030287297, 978-3030287290

More Books

Students also viewed these Databases questions

Question

Conduct a needs assessment. page 269

Answered: 1 week ago