Note: Make sure you have java installed on your machine.
Create a simple calculator simulator program that only does addition and subtraction.
You need to create three classes named Calculator, Addition & Subtraction
-
The Calculator class will have the main method of Java from where the whole flow of program will be initiated.
-
First, the Calculator class should take input from the user for operation.
-
If the user inputs “Add” then perform Addition
-
If the user inputs “Subtract” then perform Subtraction
-
If the user inputs other than these, you need to display a warning message and ask for input again.
For e.g., You can display text like “What would you like to perform?” and the user provide inputs.
-
-
After that, the program has to ask the user to input two different numbers.
-
Once the numbers are inputted from the user, it should perform Addition and Subtraction based on the previous choice of the user.
-
The Addition class must have a method to do the sum whereas the Subtraction class must have a method to do the subtract.
-
The Calculator class needs to call those methods and retrieve the result from them and finally display them.
Note: You will need to compile all the three classes and finally run only the Calculator class since it contains the main method.
Clone the repository and follow the compilation and execution steps.
To compile the project, you need to run the following command in the terminal:
javac Calculator.java InputOutput.java Result.java Addition.java Subtraction.java
To run the project, you need to run the following command in the terminal:
java Calculator
After you run the execution step you will be prompted as following:
Initializing 'Calculator' elements....
- InputOutput
- Result
- Addition
- Result
- Subtraction
- Calculator
Calculator
@Author: Dibesh Raj Subedi
Menu
Type the option you desire and hit enter.
1. Add
2. Subtract
3. Exit
Enter your choice:
Now enter add
or subtract
accordingly if you want to exit, enter exit
. When you enter CTRL+C
the terminal exits forcefully. When add or subtract is entered you will be prompted to enter numbers to perform the operations.
When program exits, you will be observing the following output:
Initializing 'Calculator' elements....
- InputOutput
- Result
- Addition
- Result
- Subtraction
- Calculator
Calculator
@Author: Dibesh Raj Subedi
Menu
Type the option you desire and hit enter.
1. Add
2. Subtract
3. Exit
Enter your choice: exit
- Exiting gracefully