A console-based calculator application built in Java.
This project implements a feature-rich console-based calculator using Java. It demonstrates:
- Object-Oriented Programming (OOP) principles
- Clean code structure with separation of concerns
- Meaningful Git commit history and version control practices
The calculator supports arithmetic operations, keeps a session history, and provides a clean, color-coded terminal interface.
Note: All the results are displayed to 4 decimal places for consistency (e.g.
120displays as120.0000). This makes sure that there is uniform formatting across both whole numbers and decimal results.
| Feature | Description |
|---|---|
| ➕ Addition | Add two numbers |
| ➖ Subtraction | Subtract one number from another |
| ✖️ Multiplication | Multiply two numbers |
| ➗ Division | Divide with zero-division error handling |
| % Modulus | Find the remainder of division |
| ^ Power | Raise a number to an exponent |
| √ Square Root | Compute the square root (with negative guard) |
| History | View all calculations made in the current session |
| Clear | Reset history and last result |
java-calculator/
├── src/
│ └── calculator/
│ ├── Main.java # Entry point & CLI interface
│ └── Calculator.java # Core arithmetic logic
├── .gitignore
└── README.md
| Requirement | Version |
|---|---|
| Java JDK | 17+ |
git clone https://github.com/ThatoMabilo/java-calculator.git
cd java-calculatorjavac -d out src/calculator/Main.java src/calculator/Calculator.javajava -cp out calculator.MainOnce launched, you'll see a menu like this:
── Operations ─────────────────────────
1 Addition (+)
2 Subtraction (-)
3 Multiplication (*)
4 Division (/)
5 Modulus (%)
6 Power (^)
7 Square Root (√)
── Utilities ────────────────────────────
8 Show Last Result
9 Show History
0 Clear History
q Quit
Enter the number of the operation you want, then enter your numbers when prompted.
Screenshots of the application running on my local machine are available in the
/screenshotsfolder of this repository.
Enter choice: 1
Enter first number : 15.5
Enter second number: 4.5
15.5000 + 4.5000 = 20.0000
Enter choice: 6
Enter first number : 2
Enter second number: 10
2.0000 ^ 10.0000 = 1024.0000
Enter choice: 9
── Calculation History ──
15.0000 + 4.5000 = 20.0000
2.0000 ^ 10.0000 = 1024.0000
This project follows a structured commit strategy:
| Commit | Description |
|---|---|
init |
Initial project scaffold and README |
feat |
Add Calculator core logic (all operations) |
feat |
Add Main CLI with color-coded menu |
fix |
Handle edge cases (div by zero, negative sqrt) |
docs |
Update README with usage instructions |
- Student: Thato Anikie Mabilo
- Student Number: 222148349
- Lecturer: Dr. Boniface Kabaso
- Program: Console-based Calculator