Simple Calculator
1. Introduction
This document describes a basic calculator program implemented in Java. The
program allows users to perform basic arithmetic operations (addition,
subtraction, multiplication, and division) on two user-provided numbers.
2. System Requirements
Software: Java Runtime Environment (JRE) 1.8 or later
Hardware: Any computer with a Java compatible operating system
3. Functionality
The calculator performs the following functions:
Prompts the user to enter two numbers.
Prompts the user to select an arithmetic operation (+, -, *, /).
Performs the chosen operation on the entered numbers.
Handles division by zero errors.
Displays the final result of the calculation.
4. User Interface
This is a command-line calculator. The user interacts with the program by
entering input through the console.
5. Detailed Design
The program consists of a single Java class named SimpleCalculator. Here's a
breakdown of the code:
Scanner Object: A Scanner object is created to read user input from the
console.
Input Numbers: The user is prompted to enter two numbers, which are stored
in double variables num1 and num2.
Input Operation: The user is prompted to select an arithmetic operation (+, -,
*, /). The program reads the input character and stores it in a char variable
operator.
Calculation: A switch statement evaluates the operator and performs the
corresponding calculation using basic arithmetic operators.
Error Handling: The division case checks for division by zero and displays an
error message if encountered.
Output: The final result is displayed on the console using System.out.println.
6. Testing
The program has been tested with various inputs to ensure it performs the
following correctly:
Addition, subtraction, multiplication, and division of positive and negative
numbers.
Handling of division by zero.
7. Limitations
This is a basic calculator and does not support complex mathematical functions
(e.g., trigonometry, logarithms).
It is a command-line program and does not have a graphical user interface
(GUI).
8. Future Enhancements
Implement functionalities for more mathematical operations.
Develop a GUI for a more user-friendly experience.
Error handling for invalid user input (e.g., non-numeric characters).
9. Conclusion
This document provides a detailed description of a simple Java calculator
program. The program offers basic functionalities for performing arithmetic
operations and serves as a foundational example for understanding Java
programming concepts.
10. Code
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter second number: ");
double num2 = scanner.nextDouble();
System.out.print("Enter operation (+, -, *, /): ");
char operator = scanner.next().charAt(0);
double result;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 == 0) {
System.out.println("Error: Division by zero");
return;
}
result = num1 / num2;
break;
default:
System.out.println("Invalid operator");
return;
}
System.out.println("Result: " + result);
scanner.close();
}
}