A Java Calculator Project is one of the best beginner-friendly projects for learning Java programming. It helps you understand fundamental concepts such as variables, operators, user input, conditional statements, methods, loops, and exception handling. By building a calculator, you gain practical experience in applying Java concepts to solve real-world problems.
In this project, we will create a simple console-based calculator that performs basic arithmetic operations such as addition, subtraction, multiplication, division, and modulus. Later, we’ll also explore ways to improve the calculator by adding advanced features.
Project Objective
The objective of this project is to build a calculator that:
- Accepts two numbers from the user.
- Allows the user to choose an arithmetic operation.
- Performs the selected operation.
- Displays the result.
- Handles invalid input gracefully.
- Prevents division by zero.
Features
This calculator supports the following operations:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
- Exit option
Technologies Used
- Java
- Scanner Class
- Switch Statement
- Loops
- Methods
- Exception Handling (Optional)
Project Workflow
The calculator works as follows:
- Display the calculator menu.
- Ask the user to select an operation.
- Read two numbers from the keyboard.
- Perform the selected calculation.
- Display the result.
- Ask the user whether to continue.
- Repeat until the user exits.
Java Calculator Source Code
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("\n===== JAVA CALCULATOR =====");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("5. Modulus");
System.out.println("6. Exit");
System.out.print("Choose an option: ");
choice = scanner.nextInt();
if (choice == 6) {
System.out.println("Thank you for using the calculator.");
break;
}
if (choice 6) {
System.out.println("Invalid Choice!");
continue;
}
System.out.print("Enter First Number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter Second Number: ");
double num2 = scanner.nextDouble();
switch (choice) {
case 1:
System.out.println("Result = " + (num1 + num2));
break;
case 2:
System.out.println("Result = " + (num1 - num2));
break;
case 3:
System.out.println("Result = " + (num1 * num2));
break;
case 4:
if (num2 == 0) {
System.out.println("Cannot divide by zero.");
} else {
System.out.println("Result = " + (num1 / num2));
}
break;
case 5:
if (num2 == 0) {
System.out.println("Cannot perform modulus by zero.");
} else {
System.out.println("Result = " + (num1 % num2));
}
break;
}
} while (true);
scanner.close();
}
}
Sample Output
===== JAVA CALCULATOR =====
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
6. Exit
Choose an option: 1
Enter First Number: 25
Enter Second Number: 15
Result = 40.0
Division Example
Choose an option: 4
Enter First Number: 50
Enter Second Number: 10
Result =5.0
Invalid Choice Example
Choose an option: 8
Invalid Choice!
Division by Zero Example
Choose an option: 4
Enter First Number: 20
Enter Second Number: 0
Cannot divide by zero.
Code Explanation
1. Import Scanner Class
import java.util.Scanner;
The Scanner class is used to read input from the keyboard.
2. Create Scanner Object
import java.util.Scanner;
The Scanner class is used to read input from the keyboard.
3. Display Menu
System.out.println("1. Addition");
System.out.println("2. Subtraction");
The program displays all available calculator operations.
4. Read User Choice
choice = scanner.nextInt();
The user selects the desired operation.
5. Read Numbers
double num1 = scanner.nextDouble();
double num2 = scanner.nextDouble();
The calculator accepts two numbers from the user.
6. Perform Calculation
switch(choice)
The switch statement executes the selected arithmetic operation.
7. Handle Division by Zero
if(num2 == 0)
The program checks whether the denominator is zero before performing division or modulus.
8. Repeat the Program
do{
...
}while(true);
The calculator continues running until the user selects the Exit option.
Supported Operations
| Operation | Symbol | Example | Result |
|---|---|---|---|
| Addition | + | 10 + 5 | 15 |
| Subtraction | – | 20 – 8 | 12 |
| Multiplication | * | 6 × 7 | 42 |
| Division | / | 30 ÷ 5 | 6 |
| Modulus | % | 10 % 3 | 1 |
Advantages of This Project
- Beginner-friendly Java project.
- Demonstrates the use of the Scanner class.
- Uses loops for continuous execution.
- Introduces the switch statement.
- Shows conditional programming using if-else.
- Handles common runtime errors like division by zero.
- Easy to expand with additional features.
Possible Enhancements
Once you complete the basic calculator, you can improve it by adding:
- Square root calculation.
- Power (x^y) operation.
- Percentage calculation.
- Average of multiple numbers.
- Scientific calculator functions.
- Calculation history.
- Memory functions (M+, M-, MR, MC).
- Graphical User Interface (GUI) using Java Swing or JavaFX.
- Keyboard shortcuts.
- Dark mode interface for GUI applications.
Common Mistakes
1. Forgetting to Close Scanner
scanner.close();
Always close the Scanner object after use to release system resources.
2. Division by Zero
Never perform:
num1 / 0
Always check whether the denominator is zero before dividing.
3. Invalid Menu Choice
Validate the user’s menu selection before performing any operation.
4. Using int Instead of double
Using double allows the calculator to work with decimal numbers such as:
10.5 + 4.3 = 14.8
5. Forgetting break Statements
Each switch case should include a break statement to avoid unintended fall-through.
Learning Outcomes
By completing this project, you will gain hands-on experience with:
- Java syntax
- Variables and data types
- Arithmetic operators
- User input using Scanner
- Conditional statements
- switch statement
- Loops (do-while)
- Error handling
- Program structure
- Console-based application development
Project Extensions
After building this calculator, you can create more advanced Java projects, such as:
- Student Management System
- Banking Management System
- Library Management System
- Employee Payroll System
- Hospital Management System
- Number Guessing Game
- Currency Converter
- BMI Calculator
- Temperature Converter
- Scientific Calculator
- Expense Tracker
- To-Do List Application
These projects build upon the concepts learned in the calculator project and help strengthen your Java programming skills.
Conclusion
The Java Calculator Project is an excellent beginner project that combines many core Java concepts into a practical application. It demonstrates how to accept user input, perform arithmetic operations, use loops, apply conditional logic, and organize code effectively. By implementing features such as division-by-zero checks and input validation, you also learn how to write more reliable and user-friendly programs.
As your Java skills improve, you can enhance this calculator by adding scientific functions, graphical user interfaces using Swing or JavaFX, calculation history, and advanced mathematical operations. Completing this project provides a strong foundation for building more complex Java applications and prepares you for real-world software development challenges.