Skip to content

Commit 8be898c

Browse files
committed
Easy Calculator added
1 parent 03a725b commit 8be898c

File tree

3 files changed

+170
-1
lines changed

3 files changed

+170
-1
lines changed

EasyCalculator/Calculator.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
public class Calculator {
2+
3+
/**
4+
* A simple error class for division by zero exceptions.
5+
*/
6+
public static class CalculatorError extends ArithmeticException {
7+
public CalculatorError(String message) {
8+
super(message);
9+
}
10+
}
11+
12+
/**
13+
* This constructor initializes a Calculator object.
14+
*/
15+
public Calculator() {
16+
// Intentionally left empty, as the constructor doesn't require any initialization.
17+
}
18+
19+
/**
20+
* This method adds two numbers and returns the sum.
21+
*
22+
* @param a The first number to be added.
23+
* @param b The second number to be added.
24+
* @return The sum of a and b.
25+
*/
26+
public double add(double a, double b) {
27+
return a + b;
28+
}
29+
30+
/**
31+
* This method subtracts two numbers and returns the difference.
32+
*
33+
* @param a The first number from which the second number will be subtracted.
34+
* @param b The second number to be subtracted from the first number.
35+
* @return The difference of a and b.
36+
*/
37+
public double subtract(double a, double b) {
38+
return a - b;
39+
}
40+
41+
/**
42+
* This method multiplies two numbers and returns the product.
43+
*
44+
* @param a The first number to be multiplied.
45+
* @param b The second number to be multiplied.
46+
* @return The product of a and b.
47+
*/
48+
public double multiply(double a, double b) {
49+
return a * b;
50+
}
51+
52+
/**
53+
* This method divides two numbers and returns the quotient.
54+
*
55+
* @param a The dividend (number to be divided).
56+
* @param b The divisor (number by which to divide).
57+
* @return The quotient of a and b.
58+
* @throws CalculatorError If the divisor (b) is zero.
59+
*/
60+
public double divide(double a, double b) throws CalculatorError {
61+
if (b == 0) {
62+
throw new CalculatorError("Division by zero is not allowed!");
63+
}
64+
return a / b;
65+
}
66+
}

EasyCalculator/EasyCalculator.java

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import java.text.DecimalFormat;
2+
import java.util.InputMismatchException;
3+
import java.util.Scanner;
4+
5+
6+
public class EasyCalculator {
7+
8+
public static void main(String[] args) {
9+
10+
// Create a Scanner object for user input.
11+
Scanner sc = new Scanner(System.in, "Windows-1250");
12+
13+
// Welcome message and initialize the calculator object
14+
System.out.println("Welcome to the Easy Calculator!");
15+
Calculator calculator = new Calculator();
16+
17+
// Main loop to handle user interaction.
18+
while (true) {
19+
// Display the menu options.
20+
System.out.println("\nMenu:");
21+
System.out.println("1. Add (+) ");
22+
System.out.println("2. Subtract (-)");
23+
System.out.println("3. Multiply (*)");
24+
System.out.println("4. Divide (/)");
25+
System.out.println("5. Exit (q)");
26+
27+
// Get user input for the desired operation.
28+
int operationChoice;
29+
do {
30+
System.out.print("Enter your choice (1-5): ");
31+
try {
32+
operationChoice = sc.nextInt();
33+
break; // Exit the loop if input is valid (integer)
34+
} catch (InputMismatchException e) {
35+
System.err.println("Invalid input. Please enter a number (1-5).");
36+
sc.next(); // Consume the invalid input
37+
}
38+
} while (true); // Loop until valid input is entered
39+
40+
// Perform the selected operation using the Calculator object.
41+
if (operationChoice == 5) {
42+
System.out.println("Exiting the calculator.");
43+
break;
44+
}
45+
46+
// Get operands from the user.
47+
double firstOperand, secondOperand;
48+
do {
49+
System.out.print("Enter the first number: ");
50+
try {
51+
firstOperand = sc.nextDouble();
52+
break;
53+
} catch (InputMismatchException e) {
54+
System.err.println("Invalid input. Please enter a number.");
55+
sc.next(); // Consume the invalid input
56+
}
57+
} while (true);
58+
59+
do {
60+
System.out.print("Enter the second number: ");
61+
try {
62+
secondOperand = sc.nextDouble();
63+
break;
64+
} catch (InputMismatchException e) {
65+
System.err.println("Invalid input. Please enter a number.");
66+
sc.next(); // Consume the invalid input
67+
}
68+
} while (true);
69+
70+
// Use the Calculator object to perform the chosen operation
71+
// and display the result with clean formatting.
72+
DecimalFormat df = new DecimalFormat("#.###");
73+
double result;
74+
try {
75+
switch (operationChoice) {
76+
case 1:
77+
result = calculator.add(firstOperand, secondOperand);
78+
System.out.println(firstOperand + " + " + secondOperand + " = " + df.format(result));
79+
break;
80+
case 2:
81+
result = calculator.subtract(firstOperand, secondOperand);
82+
System.out.println(firstOperand + " - " + secondOperand + " = " + df.format(result));
83+
break;
84+
case 3:
85+
result = calculator.multiply(firstOperand, secondOperand);
86+
System.out.println(firstOperand + " * " + secondOperand + " = " + df.format(result));
87+
break;
88+
case 4:
89+
result = calculator.divide(firstOperand, secondOperand);
90+
System.out.println(firstOperand + " / " + secondOperand + " = " + df.format(result));
91+
break;
92+
default:
93+
System.err.println("Invalid choice.");
94+
}
95+
} catch (Calculator.CalculatorError e) {
96+
System.err.println(e.getMessage());
97+
}
98+
}
99+
100+
// Close the Scanner object.
101+
sc.close();
102+
}
103+
}

StarterCalculator/Calculator.java renamed to StarterCalculator/StarterCalculator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import java.util.InputMismatchException;
33
import java.util.Scanner;
44

5-
public class Calculator {
5+
public class StarterCalculator {
66

77
/**
88
* Main entry point for the calculator application.

0 commit comments

Comments
 (0)