A calculator is a fundamental project for anyone learning a new programming language. It allows you to practice basic programming concepts such as variables, control structures, loops and functions. In this article, we will create a simple calculator in Java that can perform basic arithmetic operations such as addition, subtraction, multiplication and division.
Prerequisites
- Java Development Kit (JDK) installed on the computer.
- An Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse or a simple text editor and command line for compiling Java code.
Step-by-Step Guide
Step 1: Setting Up Your Project
- Create a New Java Project: Open your IDE and create a new Java project. Name it
Calculator
. - Create a New Java Class: Inside your project create a new Java class. Name it
Calculator
.
Step 2: Writing the Calculator Code
Let's start by writing the code for our calculator.
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter first number: ");
double num1 = scanner.nextDouble();
System.out.println("Enter second number: ");
double num2 = scanner.nextDouble();
System.out.println("Enter an operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);
double result;
switch (operator) {
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.");
return;
}
break;
default:
System.out.println("Invalid operator!");
return;
}
System.out.println("The result is: " + result);
}
}
Output :
Enter first number:
10
Enter second number:
20
Enter an operator (+, -, *, /):
+
The result is: 30.0
Explanation of the Code
1. Importing the Scanner Class:
import java.util.Scanner;
The Scanner
class is used to take input from the user.
2. Main Method:
public static void main(String[] args) {
This is the entry point of our program.
3. Creating Scanner Object:
Scanner scanner = new Scanner(System.in);
We create an instance of Scanner
to read user inputs.
4. Taking User Inputs:
System.out.println("Enter first number: ");
double num1 = scanner.nextDouble();
We prompt the user to enter the first number and store it in num1
.
System.out.println("Enter second number: ");
double num2 = scanner.nextDouble();
Similarly, we prompt the user to enter the second number and store it in num2
.
System.out.println("Enter an operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);
We prompt the user to enter an operator and store it in operator
.
5. Switch Case for Operations:
switch (operator) {
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.");
return;
}
break;
default:
System.out.println("Invalid operator!");
return;
}
We use a switch case to perform the operation based on the operator entered by the user.
6. Displaying the Result:
System.out.println("The result is: " + result);
We print the result of the operation.
Running the Calculator
1. Compile the Program:
If using a command line, navigate to the directory containing your Calculator.java
file and compile it using:
javac Calculator.java
2. Run the Program: After compiling, run the program using:
3. Enter Inputs: Follow the prompts to enter two numbers and an operator. The program will display the result of the operation.
Conclusion
Creating a simple calculator in Java is a great way to practice basic programming concepts and get familiar with taking user inputs and performing operations. This project foundation for more complex applications and helps build a strong understanding of Java syntax and control structures.
Post a Comment