Calculators are one of the most fundamental projects for beginners in programming. They help you learn about input handling, conditional statements, loops, and basic arithmetic operations. In this blog post, we will walk through the creation of a simple calculator in Java.
Prerequisites
To follow along with this tutorial, you should have a basic understanding of:
- Java syntax and structure
- How to set up and run a Java project
- Basic knowledge of arithmetic operations
Make sure you have a Java Development Kit (JDK) installed and a code editor or IDE (such as IntelliJ IDEA, Eclipse, or VS Code) ready to use.
Step 1: Setting Up the Project
Create a new Java project in your preferred IDE or editor. Then, create a Java class named BasicCalculator
. This will serve as the entry point for your program.
Step 2: Writing the Code
Below is the full implementation of a basic calculator in Java:
import java.util.Scanner;
public class BasicCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(“Welcome to the Basic Calculator!”);
System.out.println(“Choose an operation: +, -, *, /, or %”);
String operation = scanner.nextLine();
System.out.println(“Enter the first number:”);
double num1 = scanner.nextDouble();
System.out.println(“Enter the second number:”);
double num2 = scanner.nextDouble();
double result = 0;
boolean validOperation = true;
switch (operation) {
case “+”:
result = num1 + num2;
break;
case “-“:
result = num1 – num2;
break;
case “*”:
result = num1 * num2;
break;
case “/”:
if (num2 != 0) {
result = num1 / num2;
} else {
System.out.println(“Error: Division by zero is not allowed.”);
validOperation = false;
}
break;
case “%”:
result = num1 % num2;
break;
default:
System.out.println(“Error: Invalid operation.”);
validOperation = false;
}
if (validOperation) {
System.out.println(“The result is: ” + result);
}
scanner.close();
}
}
Step 3: Understanding the Code
- Importing Scanner: The
Scanner
class is imported to handle user input. - Prompting the User: The program prompts the user to enter the operation and two numbers.
- Using a Switch Statement: A
switch
statement is used to perform different operations based on the user’s choice. - Handling Division by Zero: A special check is included to handle division by zero, preventing runtime errors.
- Displaying the Result: The result of the operation is displayed to the user.
Step 4: Running the Program
To run the program:
- Compile the Java file using
javac BasicCalculator.java
. - Run the compiled file using
java BasicCalculator
. - Follow the prompts to perform calculations.
Additional Features
Here are some ideas to enhance your calculator:
- Error Handling: Handle invalid inputs gracefully by using exception handling (
try-catch
blocks). - Continuous Operations: Allow the user to perform multiple calculations without restarting the program by using a loop.
- Advanced Functions: Add support for mathematical functions like square root, exponentiation, or trigonometric operations.
- Graphical User Interface (GUI): Create a GUI version using JavaFX or Swing.
Creating a basic calculator is an excellent project to strengthen your understanding of Java. It introduces fundamental programming concepts in a practical and interactive way. Once you’ve mastered the basics, try extending the program to make it more versatile and user-friendly.