This Java program implements a simple text-based calculator in the...
May 29, 2025 at 03:43 PM
This Java program implements a simple text-based calculator in the Calc
class. Here's an explanation of what the code does:
Purpose:
The program provides an interactive command-line calculator capable of:
- Calculating basic mathematical operations between two numbers (
+
,-
,*
,/
,%
). - Performing some single operand operations (
sqrt
,log
,sin
,cos
,tan
). - Handling invalid input gracefully with proper error messages.
- Repeating calculations in a loop until the user decides to exit by typing
exit
.
Detailed Explanation:
-
Program Setup:
- It imports the
Scanner
class to read user input. - Prompts the user with instructions:
🔢 Java Calculator - Type 'exit' to quit
- It imports the
-
Main Calculation Loop (
while (true)
):- Continuously reads input from the user until they type
exit
.
- Continuously reads input from the user until they type
-
Input Parsing and Processing:
- Case 1: If the user inputs a single valid number, it echoes the number:
➡️ Result: <number>
- Case 2: If the user inputs a unary operation (e.g.,
sqrt 9
orsin 45
):- The program recognizes supported operations (
sqrt
,log
,sin
,cos
,tan
). - Parses the number and performs the operation using the
Math
library:sqrt
: Computes the square root.log
: Computes the natural logarithm.sin
,cos
,tan
: Trigonometric calculations using degrees (converted to radians).
- Outputs the result.
- The program recognizes supported operations (
- Case 3: If the user inputs a binary operation (e.g.,
5 + 3
):- Splits the input into three parts:
number1 operator number2
. - Parses the numbers and checks if the operator is one of
+, -, *, /, %
. - Performs the operation:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/): Checks for division by zero and outputs appropriate error messages.
- Modulus (%)
- Outputs the result, formatted to 4 decimal places.
- Splits the input into three parts:
- Case 1: If the user inputs a single valid number, it echoes the number:
-
Error Handling:
- Misformatted or invalid inputs (e.g.,
abc
,5 ^ 2
) are caught by:- A check on token lengths or valid functions/operators.
- Catching exceptions when parsing or processing invalid inputs.
- Outputs error messages:
❗ Invalid input. Try again.
- Misformatted or invalid inputs (e.g.,
-
Exit Condition:
- If the user types
exit
(case insensitive), the loop breaks, and the program outputs:👋 Goodbye!
- The
Scanner
resource is closed.
- If the user types
-
Helper Methods:
isNum(String s)
: Checks if the input is a valid numeric value by attempting to parse it as adouble
.isOp(String s)
: Checks if the input is a valid mathematical operator (one of+
,-
,*
,/
,%
).
Example Usage:
-
Regular operations:
Enter expression (e.g., 5 + 3 or sqrt 9): 5 + 3 ➡️ 5.0 + 3.0 = 8.0000
-
Unary operations:
Enter expression (e.g., 5 + 3 or sqrt 9): sqrt 16 ➡️ √16.0 = 4.0
-
Trigonometric operation:
Enter expression (e.g., 5 + 3 or sqrt 9): sin 30 ➡️ sin(30.0) = 0.49999999999999994
-
Division by zero:
Enter expression (e.g., 5 + 3 or sqrt 9): 5 / 0 ⚠️ Error: Division by zero
-
Invalid inputs:
Enter expression (e.g., 5 + 3 or sqrt 9): 5 ^ 2 ❗ Invalid input. Try again.
-
Exit:
Enter expression (e.g., 5 + 3 or sqrt 9): exit 👋 Goodbye!
Key Features:
- Supports various mathematical calculations with input validation.
- Handles common errors (e.g., invalid format, division by zero).
- Provides user-friendly interaction with error and result messages.
- Allows extensibility for new operations.
Generate your own explanations
Download our vscode extension
Read other generated explanations
Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node