In this mini project, you'll create a simple calculator program in Java that can perform addition, subtraction, multiplication, and division operations.
You'll write a Java program that:
Open your preferred Java IDE (e.g., Eclipse, IntelliJ IDEA, or NetBeans) and create a new Java project named "BasicCalculator."
import java.util.Scanner;
public class BasicCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();
System.out.println("Select operation: ");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.print("Enter your choice (1/2/3/4): ");
int choice = scanner.nextInt();
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("Cannot divide by zero.");
return;
}
break;
default:
System.out.println("Invalid choice.");
return;
}
System.out.println("Result: " + result);
}
}
Run the program in your IDE. Follow the prompts to enter two numbers and select an operation. The program will display the result of the chosen operation.
You've successfully created a basic calculator program in Java! This mini project helps you practice user input, conditional statements, and basic arithmetic operations. Feel free to enhance the program by adding error handling or additional features like more operations or looping until the user decides to exit