Skip to content

ashishkaushik05/Java_Calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Simple CLI Calculator

A Java-based command-line calculator application with a clean, modular architecture.

Features

  • 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
  • Error Handling: Division by zero, invalid input, negative square roots
  • Interactive CLI: Help system, clear screen, graceful exit

Project Structure

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

How to Build and Run

Prerequisites

  • Java 8 or higher
  • Command line terminal

Compilation

# 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

Running

# Run the application
java -cp bin main.Main

Usage Examples

Mathematical Expressions

> 5 + 3
Result: 8

> 10.5 * 2
Result: 21

> 15 / 3
Result: 5

> 2 ^ 8
Result: 256

Command Format

> add 5 3
Result: 8

> mul 4.5 2
Result: 9

> sqrt 16
Result: 4

> pow 2 10
Result: 1024

Special Commands

> help          # Show help information
> clear         # Clear the screen
> exit          # Exit the calculator

Supported Operations

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

Error Handling

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

Architecture Highlights

  • 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

Future Enhancements

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published