In this tutorial, we will learn how to build a simple calculator in Java. We will use basic arithmetic operations like addition, subtraction, multiplication, and division. Java is a widely used programming language known for its simplicity and versatility, making it a great choice for beginners.
To follow along with this tutorial, you should have a basic understanding of Java programming. If you are new to Java, it is recommended to familiarize yourself with basic syntax and concepts before proceeding.
To get started, open your favorite Java Integrated Development Environment (IDE) or a simple text editor. Create a new Java project and name it “SimpleCalculator”.
First, we need to create a class to hold our main method. Create a new file named SimpleCalculator.java and add the following code:
public class SimpleCalculator {
public static void main(String[] args) {
// Code goes here
}
}
Next, let’s prompt the user to enter two numbers and an operator. We will use the Scanner class to read the input. Add the following code inside the main method:
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
System.out.print("Enter the operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);
// Code for calculations goes here
}
}
Based on the operator entered by the user, we will perform the corresponding arithmetic operation. Here is the code to handle the calculations:
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
System.out.print("Enter the operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);
int result;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
System.out.println("Invalid operator!");
return;
}
System.out.println("Result: " + result);
}
}
Compile and run the program. You will be prompted to enter the two numbers and the operator. After entering the values, the program will display the calculated result.
Congratulations! You have successfully built a simple calculator in Java. This tutorial covered the basic concepts of accepting user input, performing calculations, and using the switch statement for control flow. You can further enhance the calculator by adding more operations or adding error handling for division by zero, etc. Keep exploring and practicing to improve your Java programming skills. Happy coding!