Mini Project: Basic Calculator

In this mini project, you'll create a simple calculator program in Java that can perform addition, subtraction, multiplication, and division operations.

Project Overview

You'll write a Java program that:

  1. Takes input from the user for two numbers and the operation they want to perform.
  2. Performs the selected operation on the numbers.
  3. Displays the result to the user.

Steps to Create the Calculator

Step 1: Set Up Your Project

Open your preferred Java IDE (e.g., Eclipse, IntelliJ IDEA, or NetBeans) and create a new Java project named "BasicCalculator."

Step 2: Write the Main Code

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);
    }
}

Step 3: Run the Program

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.

Congratulations!

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