Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please write a Java application program that can perform the following 6 operators for 2 numbers: N1 and N2. You need to prompt the user

Please write a Java application program that can perform the following 6 operators for 2 numbers: N1 and N2. You need to prompt the user to enter a number, an operator, and another number.

To simplify your Java program, please declare N1 and N2 as double-floating numbers, and operator as String. Please see the test case #1 below to design your application program properly.

This program is like a simple calculator for users to enjoy.

You must keep your program running to accept more data for the next operation until the operator received is @. In other words, the operator @ is the only way to stop your program and thank the user.

The 6 valid operators for this simple calculator are as follows:

  1. + for addition of N1 and N2. Therefore, result = (N1 + N2).
  2. for subtraction of N2 from N1. Therefore, result = (N1 N2).
  3. * for multiplication of N1 with N2. Therefore, result = (N1 * N2).
  4. / for floating-point division of N1 by N2. If N2 is zero, please say The second number cannot be zero,

else result = (N1 / N2).

  1. ^ for power N2 with base N1 (i.e., N1N2). Therefore, result = Math.pow ( N1 , N2 ) in Java.
  2. % for division remainder (i.e., modulus) of N1 by N2. If N2 is zero, please

say The second number cannot be zero, else result = (N1 % N2).

  1. @ for stopping this calculator after printing a friendly thank-you message.
  2. Otherwise, please say Sorry, the operator ____ is not valid!. // Please show this invalid operator.

===========================================================================================.

Java Programming Design Considerations and Recommendations:

(1) You may reference Example 5.14: A Simple Calculator (page 256-258) to get some idea of your program structure. This project program does not allow any operator expressed in a word such as ADD, SUBTRACT, MULTIPLY, or DIVIDE.

(2) If switch statement on String is not accepted by your Java compiler, you can do switch on char. In Java, you cannot do scan.nextChar to get next character. You must use scan.next to get the next String operator, and then you may use operator.charAt(0) to get the first character of the String operator.

(3) When you enter data for this program, you must have at least one blank between number and operator, and at least another blank between operator and number. Otherwise, your program will encounter a run-time error on input data.

===========================================================================.

Your test case #1 output must look exactly as follows including the input data.

You must also do test case #2 and test case #3 with different set of data.

Each test case must cover all those 6 valid operators (at least once for each) plus an invalid one.

Welcome to use the Simple Calculator of ! You must use your name!

1================================================.

Please enter your number, operator, and number > 3.3 + 5.5

Result: 3.3 + 5.5 = 8.8

2================================================.

Please enter your number, operator, and number > 3.3 - 5.5

Result: 3.3 5.5 = 2.2

3================================================.

Please enter your number, operator, and number > 3.3 * 5.5

Result: 3.3 * 5.5 = 18.15

4================================================.

Please enter your number, operator, and number > 3.3 / 5.5

Result: 3.3 / 5.5 = 0.6

5================================================.

Please enter your number, operator, and number > 3.3 ^ 5.5

Result: 3.3 ^ 5.5 = 710.93

6================================================.

Please enter your number, operator, and number > 13.0 % 5.0

Result: 13 % 5 = 3

7================================================.

Please enter your number, operator, and number > 13.0 \ 5.0

Sorry, the operator \ is not valid!

8==================================================.

Please enter your number, operator, and number > 3 / 0

The second number cannot be zero!

9==================================================.

Please enter your number, operator, and number > 3 % 0

The second number cannot be zero!

10=================================================.

Please enter your number, operator, and number > 0 @ 0

Thank you for using the Simple Calculator of ! You must use your name!

11=================================================.

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

Datacasting How To Stream Databases Over The Internet

Authors: Jessica Keyes

1st Edition

007034678X, 978-0070346789

Students also viewed these Databases questions

Question

What is a joint cost? How does it relate to by-products?

Answered: 1 week ago