A Java-based command-line calculator application with a clean, modular architecture.
- Basic Arithmetic Operations: Addition, subtraction, multiplication, division
- Advanced Operations: Power (exponentiation), square root
- Multiple Input Formats:
- Mathematical expressions:
5 + 3
,10 * 2
- Command format:
add 5 3
,sqrt 25
- Mathematical expressions:
- Error Handling: Division by zero, invalid input, negative square roots
- Interactive CLI: Help system, clear screen, graceful exit
src/
├── main/
│ └── Main.java # Entry point and main program loop
├── calculator/
│ └── Calculator.java # Core calculation logic
├── input/
│ └── InputHandler.java # Input parsing and validation
├── operations/
│ └── Operation.java # Enum defining supported operations
└── utils/
├── NumberParser.java # Number parsing utilities
└── StringUtils.java # String manipulation utilities
- Java 8 or higher
- Command line terminal
# Navigate to the project directory
cd "/home/ashish/Documents/code/Java/First Project"
# Compile all Java files
javac -d bin src/**/*.java
# Alternative: Compile with classpath
javac -cp src -d bin src/main/Main.java src/calculator/Calculator.java src/input/InputHandler.java src/operations/Operation.java src/utils/NumberParser.java src/utils/StringUtils.java
# Run the application
java -cp bin main.Main
> 5 + 3
Result: 8
> 10.5 * 2
Result: 21
> 15 / 3
Result: 5
> 2 ^ 8
Result: 256
> add 5 3
Result: 8
> mul 4.5 2
Result: 9
> sqrt 16
Result: 4
> pow 2 10
Result: 1024
> help # Show help information
> clear # Clear the screen
> exit # Exit the calculator
Operation | Symbols | Command | Example |
---|---|---|---|
Addition | + |
add |
5 + 3 or add 5 3 |
Subtraction | - |
sub , subtract |
5 - 3 or sub 5 3 |
Multiplication | * |
mul , multiply |
5 * 3 or mul 5 3 |
Division | / |
div , divide |
15 / 3 or div 15 3 |
Power | ^ |
pow , power |
2 ^ 3 or pow 2 3 |
Square Root | sqrt |
sqrt , squareroot |
sqrt 25 |
The calculator handles various error conditions:
- Division by zero: Returns appropriate error message
- Negative square root: Not supported for real numbers
- Invalid input: Clear error messages with suggestions
- Missing operands: Validates correct number of arguments
- Modular Design: Each class has a single responsibility
- Extensible: Easy to add new operations via the Operation enum
- Robust Input Handling: Supports multiple input formats
- Clean Separation: Business logic separated from UI concerns
- Utility Classes: Reusable components for common operations
Potential features that could be added:
- Memory operations (store/recall)
- History of calculations
- More advanced mathematical functions (trigonometry, logarithms)
- Expression parsing with parentheses support
- Configuration file for custom settings