Java Multi-Op Calculator: Perform Arithmetic & More
In the realm of programming, creating a simple yet functional calculator can serve as an excellent starting point for beginners. Today, we're going to delve into the world of Java and build a calculator that can perform multiple operations. This interactive tool will allow users to add, subtract, multiply, and divide numbers, all while learning the fundamentals of Java programming.
Understanding the Requirements
Before we dive into the coding aspect, let's first understand what we aim to achieve. Our Java calculator should be able to:
- Take two numbers as input from the user.
- Provide a menu of operations to choose from (addition, subtraction, multiplication, division).
- Perform the selected operation on the two input numbers.
- Display the result to the user.
Setting Up the Java Project
To begin, create a new Java project in your preferred Integrated Development Environment (IDE). For this example, we'll use NetBeans.

Creating the Main Class
Right-click on your project in the 'Projects' tab, navigate to 'New' > 'Java Class', and name it 'Calculator'. This will be our main class file where the magic happens.
Implementing the Calculator Functionality
Now, let's start coding our calculator. We'll use the 'Scanner' class to take user input and 'System.out.println' to display output. Here's a basic structure to get us started:
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Take two numbers as input
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter second number: ");
double num2 = scanner.nextDouble();
// Display menu of operations
System.out.println("\nChoose an operation:");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
// Take operation choice as input
int choice = scanner.nextInt();
// Perform the chosen operation and display result
double result = 0;
switch (choice) {
case 1:
result = num1 + num2;
break;
case 2:
result = num1 - num2;
break;
case 3:
result = num1 * num2;
break;
case 4:
if (num2 != 0) {
result = num1 / num2;
} else {
System.out.println("Error! Division by zero is not allowed.");
return;
}
break;
default:
System.out.println("Invalid operation choice. Please try again.");
return;
}
System.out.printf("%.2f %s %.2f = %.2f\n", num1, getOperationSymbol(choice), num2, result);
}
private static String getOperationSymbol(int choice) {
switch (choice) {
case 1:
return "+";
case 2:
return "-";
case 3:
return "*";
case 4:
return "/";
default:
return "";
}
}
}
Testing the Calculator
Now that we've implemented our calculator, it's time to test it. Run the 'Calculator' class, and you should be able to input two numbers and choose an operation to perform on them. The result will be displayed on the console.

Improving the Calculator
Our calculator is now functional, but there's always room for improvement. Here are a few ideas to enhance your calculator:
- Add error handling for non-numeric input.
- Implement a loop to allow users to perform multiple calculations without exiting the program.
- Create a graphical user interface (GUI) using JavaFX or Swing for a more interactive experience.
Building a Java calculator is an excellent way to learn the basics of Java programming, user input, and decision-making structures. As you progress in your coding journey, you'll find that these fundamental concepts form the building blocks for more complex applications.























