Simple Calculator Program in Java

This Java program implements a basic calculator with operations for addition, subtraction, multiplication, and division.

Java Code:


/**
 * A simple calculator program in Java.
 */
public class Calculator {

    /**
     * Method to add two numbers.
     * @param num1 The first number.
     * @param num2 The second number.
     * @return The sum of num1 and num2.
     */
    public static double add(double num1, double num2) {
        return num1 + num2;
    }

    /**
     * Method to subtract two numbers.
     * @param num1 The first number.
     * @param num2 The second number.
     * @return The difference of num1 and num2.
     */
    public static double subtract(double num1, double num2) {
        return num1 - num2;
    }

    /**
     * Method to multiply two numbers.
     * @param num1 The first number.
     * @param num2 The second number.
     * @return The product of num1 and num2.
     */
    public static double multiply(double num1, double num2) {
        return num1 * num2;
    }

    /**
     * Method to divide two numbers.
     * @param num1 The dividend.
     * @param num2 The divisor (must not be zero).
     * @return The result of dividing num1 by num2.
     * @throws IllegalArgumentException if num2 is zero.
     */
    public static double divide(double num1, double num2) {
        if (num2 == 0) {
            throw new IllegalArgumentException("Cannot divide by zero");
        }
        return num1 / num2;
    }

    public static void main(String[] args) {
        // Example usage of the calculator methods
        double num1 = 10.5;
        double num2 = 5.2;
        
        // Perform operations
        double sum = add(num1, num2);
        double difference = subtract(num1, num2);
        double product = multiply(num1, num2);
        double quotient = divide(num1, num2);
        
        // Output results
        System.out.println("Sum: " + sum);
        System.out.println("Difference: " + difference);
        System.out.println("Product: " + product);
        System.out.println("Quotient: " + quotient);
    }
}
  

Explanation:

The Java code above implements a simple calculator program with the following components:

  • Class Definition: The Calculator class contains static methods (add, subtract, multiply, divide) to perform basic arithmetic operations.
  • Method Documentation: Each method is documented using JavaDoc comments to describe their purpose, parameters, return values, and any exceptions thrown.
  • Exception Handling: The divide method includes an exception handling mechanism to prevent division by zero, throwing an IllegalArgumentException if the divisor is zero.
  • Main Method: The main method demonstrates how to use the calculator methods by performing addition, subtraction, multiplication, and division on two double numbers (num1 and num2), and printing the results to the console.

Usage:

To use this calculator program:

  1. Edit the main method to input different values for num1 and num2.
  2. Compile and run the Java program to observe the results of addition, subtraction, multiplication, and division operations.

Explanation of the Code:

  1. Class Definition:
    • Calculator class contains static methods (add, subtract, multiply, divide) to perform basic arithmetic operations.
  2. Method Documentation:
    • Each method (add, subtract, multiply, divide) is documented using JavaDoc comments (/** ... */). This provides descriptions of parameters, return values, and any exceptions thrown.
  3. Exception Handling:
    • The divide method includes exception handling to ensure no division by zero occurs, throwing an IllegalArgumentException if attempted.
  4. Main Method:
    • The main method demonstrates example usage by initializing two double numbers (num1 and num2), performing each operation, and printing the results to the console.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)