Calculators are a fundamental tool in our everyday lives, whether it’s for math homework, budgeting, or even just making simple calculations. In this blog post, we will be creating a simple calculator using the switch…case statement in Java.
To start, we will need to create a new Java project and create a new class called “Calculator”. Inside this class, we will be creating a main method where our program will run.
First, we will need to declare our variables. We will be using two double variables, “num1” and “num2”, to hold the numbers that we will be performing calculations on. We will also need a char variable, “operator”, to hold the operator that the user inputs.
Next, we will create a switch statement that will take the operator as its input. Inside the switch statement, we will use the case statement to check the operator and perform the corresponding calculation.
Here is the code for our switch statement:
switch(operator) {
case '+':
System.out.println(num1 + num2);
break;
case '-':
System.out.println(num1 - num2);
break;
case '*':
System.out.println(num1 * num2);
break;
case '/':
System.out.println(num1 / num2);
break;
default:
System.out.println("Invalid operator");
}
In this code, we are using the “+”, “-“, “*”, and “/” operators to perform addition, subtraction, multiplication, and division, respectively. If the operator input is none of these, the program will output “Invalid operator”.
Finally, we will need to prompt the user for their input. We can use the Scanner class to read in the user’s input for the two numbers and the operator. Here is the code for that:
Scanner input = new Scanner(System.in);
System.out.print("Enter first number: ");
num1 = input.nextDouble();
System.out.print("Enter operator: ");
operator = input.next().charAt(0);
System.out.print("Enter second number: ");
num2 = input.nextDouble();
With this code, the program will prompt the user to enter their first number, operator, and second number. The program will then store these values in the corresponding variables.
Java Program
Putting it all together, here is the full code for our simple calculator:
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
double num1, num2;
char operator;
Scanner input = new Scanner(System.in);
System.out.print("Enter first number: ");
num1 = input.nextDouble();
System.out.print("Enter operator: ");
operator = input.next().charAt(0);
System.out.print("Enter second number: ");
num2 = input.nextDouble();
switch(operator) {
case '+':
System.out.println(num1 + num2);
break;
case '-':
System.out.println(num1 - num2);
break;
case '*':
System.out.println(num1*num2);
break;
case '/':
System.out.println(num1 / num2);
break;
default:
System.out.println("Invalid operator");
}
}
}
Explanation
- The program imports the Scanner class from java.util package.
- The program defines a class called Calculator with a main method.
- Within the main method, the program declares two variables, num1, and num2, to hold the two numbers entered by the user. It also declares a variable called an operator to hold the arithmetic operator entered by the user.
- The program creates a Scanner object called input to read input from the user.
- The program prompts the user to enter the first number, which is stored in the num1 variable.
- The program prompts the user to enter the arithmetic operator, which is stored in the operator variable.
- The program prompts the user to enter the second number, which is stored in the num2 variable.
- The program uses a switch statement to evaluate the operator variable and perform the corresponding arithmetic operation using the num1 and num2 variables.
- If the operator entered by the user is “+”, the program performs num1 + num2 and prints the result.
- If the operator entered by the user is “-“, the program performs num1 – num2 and prints the result.
- If the operator entered by the user is “*”, the program performs num1 * num2 and prints the result.
- If the operator entered by the user is “/”, the program performs num1 / num2 and prints the result.
- If the operator entered by the user is not one of the accepted operators (+, -, *, /), the program prints “Invalid operator”.
Latest Posts:
- Merging Two Sorted Arrays in JavaTitle: Merging Two Sorted Arrays in Java Merging two sorted arrays into a single array is a common operation in… Read more: Merging Two Sorted Arrays in Java
- Frequency of Elements in an ArrayTitle: Frequency of Elements in an Array Using Java Counting the frequency of elements in an array is a fundamental… Read more: Frequency of Elements in an Array
- Calculate the sum of diagonals in a matrixTitle: Calculate the sum of diagonals in a matrix In this blog post, we’ll delve into the world of matrices… Read more: Calculate the sum of diagonals in a matrix
- Binary Search in JavaBinary search is a powerful algorithm used to efficiently find a target value in a sorted array. In this blog… Read more: Binary Search in Java
- Removing Duplicate Elements in Array using JavaRemoving Duplicate Elements in Array using Java Arrays are fundamental in Java, but duplicate elements can clutter your data. In… Read more: Removing Duplicate Elements in Array using Java
- Transpose a 2D Array in JavaTitle: Transpose a 2D Array in Java In Java, transposing a 2D array involves converting its rows into columns and… Read more: Transpose a 2D Array in Java