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
Calculatorclass 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
dividemethod includes an exception handling mechanism to prevent division by zero, throwing anIllegalArgumentExceptionif the divisor is zero. - Main Method: The
mainmethod demonstrates how to use the calculator methods by performing addition, subtraction, multiplication, and division on two double numbers (num1andnum2), and printing the results to the console.
Usage:
To use this calculator program:
- Edit the
mainmethod to input different values fornum1andnum2. - Compile and run the Java program to observe the results of addition, subtraction, multiplication, and division operations.
Explanation of the Code:
- Class Definition:
Calculatorclass contains static methods (add,subtract,multiply,divide) to perform basic arithmetic operations.
- Method Documentation:
- Each method (
add,subtract,multiply,divide) is documented using JavaDoc comments (/** ... */). This provides descriptions of parameters, return values, and any exceptions thrown.
- Each method (
- Exception Handling:
- The
dividemethod includes exception handling to ensure no division by zero occurs, throwing anIllegalArgumentExceptionif attempted.
- The
- Main Method:
- The
mainmethod demonstrates example usage by initializing two double numbers (num1andnum2), performing each operation, and printing the results to the console.
- The
