Skip to content

Commit 4ef0c53

Browse files
committed
feat: Add SimpleCalculator class demonstrating method overloading
WHAT the code does: - Defines a `SimpleCalculator` class with three overloaded `calculate()` methods: 1. **No parameters** → adds two constants (`7 + 13 = 20`) and prints the result. 2. **Two parameters** → multiplies two integers and returns the product. 3. **Three parameters** → subtracts two numbers from the first and prints the result. - `main()` demonstrates all three variations: - Calls no-arg method → prints addition. - Calls two-arg method → prints multiplication. - Calls three-arg method → prints subtraction. WHY this matters: - Demonstrates **method overloading** (same name, different signatures). - Shows multiple operations (addition, multiplication, subtraction) under a unified method name. - Reinforces compile-time polymorphism in Java. HOW it works: 1. `calc.calculate()` → prints `"Addition (no parameters): 20"`. 2. `calc.calculate(4, 5)` → returns `20`, printed as multiplication. 3. `calc.calculate(50, 20, 10)` → computes `20`, prints subtraction result. Method Signature Notes: - Java resolves overloading by the **number and type of parameters** at compile time. - Return type does not affect method selection. Tips & gotchas: - Having a method name that does different operations (`add`, `multiply`, `subtract`) under `calculate` can confuse readability. - For clarity, separate methods with descriptive names may be better in production. - Method overloading is useful when operations are **similar in purpose**, not unrelated. Use-cases: - Educational example for compile-time polymorphism. - Foundation for building simple calculators or utility classes. - Interview prep for method overloading and signatures. Short key: class-simple calculator-method-overloading. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 99f8e86 commit 4ef0c53

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

Section10Methods/PracticeProblems/src/SimpleCalculator.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
public class SimpleCalculator {
2-
3-
// Method 1: No parameters - adds two constants
4-
void calculate() //No parameters
5-
{
2+
// Method 1: No parameters - adds two constants.
3+
void calculate() {
64
int x = 7, y = 13;
75
int sum = x + y;
86
System.out.println("Addition (no parameters): " + sum);
97
}
108

11-
// Method 2: Two parameters - multiplies them and returns result
9+
// Method 2: Two parameters - multiplies them and returns result.
1210
int calculate(int a, int b) {
1311
return a * b;
1412
}
1513

16-
// Method 3: Three parameters - subtracts and prints result
14+
// Method 3: Three parameters - subtracts and prints result.
1715
void calculate(int a, int b, int c) {
1816
int result = a - b - c;
1917
System.out.println("Subtraction (three parameters): " + result);
2018
}
2119

2220
public static void main(String[] args) {
2321
SimpleCalculator calc = new SimpleCalculator();
24-
2522
calc.calculate(); // Output: Addition (no parameters): 20
2623

2724
int product = calc.calculate(4, 5); // 4 * 5 = 20

0 commit comments

Comments
 (0)