Skip to content

Commit fe97b75

Browse files
committed
feat: Add Sum class with overloaded methods for different summations
WHAT the code does: - Defines a `Sum` class demonstrating different ways of summing integers. - `sum2()`: - Reads two integers from user input via `Scanner`. - Returns their sum. - `sum3(int, int)`: - Returns sum of two integers passed as arguments. - `sum4(int, int, int)`: - Returns sum of three integers. - `main()` demonstrates: - Calling `sum2()` interactively. - Calling `sum3(20, 30)` → outputs 50. - Calling `sum4(100, 200, 300)` → outputs 600. WHY this matters: - Demonstrates **method overloading** with different parameter counts. - Shows difference between interactive input-based and parameter-based methods. - Reinforces **return values vs printing** in methods. HOW it works: 1. `sum2()` asks user for two inputs and returns their sum. 2. `sum3()` is a straightforward addition of two integers. 3. `sum4()` adds three integers. 4. All results are stored in variables (`ans`, `ans2`, `ans3`) and printed. Tips & gotchas: - `Scanner` in `sum2()` should be closed after use → currently left open. - Avoid using `System.out.println` after a `return` (unreachable code). - Method naming (`sum2`, `sum3`, `sum4`) could be improved for clarity. - Could add `varargs` method (`sum(int... nums)`) to generalize addition. Use-cases: - Teaching example for **methods and overloading** in Java. - Practice with interactive input handling using `Scanner`. - Useful as a simple calculator demonstration. - Basis for extending into math utility classes. Short key: class-sum-method-overloading. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 491b333 commit fe97b75

File tree

1 file changed

+15
-25
lines changed
  • Section10Methods/Methods 2.O/src

1 file changed

+15
-25
lines changed

Section10Methods/Methods 2.O/src/Sum.java

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,36 @@
22

33
public class Sum {
44
public static void main(String[] args) {
5-
// int ans = sum2();
6-
// System.out.println(ans);
7-
8-
int ans = sum3(20, 30);
5+
int ans = sum2();
96
System.out.println(ans);
7+
8+
int ans2 = sum3(20, 30);
9+
System.out.println(ans2);
10+
11+
int ans3 = sum4(100, 200, 300);
12+
System.out.println(ans3);
1013
}
1114

12-
// pass the value of numbers when you are calling the method in main()
15+
// pass the value of numbers when you are calling the method in main().
1316
static int sum3(int a, int b) {
14-
int sum = a + b;
15-
return sum;
17+
return a + b;
1618
}
1719

18-
// return the value
1920
static int sum2() {
2021
Scanner in = new Scanner(System.in);
22+
2123
System.out.print("Enter number 1: ");
2224
int num1 = in.nextInt();
25+
2326
System.out.print("Enter number 2: ");
2427
int num2 = in.nextInt();
28+
2529
int sum = num1 + num2;
2630
return sum;
27-
// System.out.println("This will never execute");
31+
// System.out.println("This will never execute");
2832
}
2933

30-
static void sum() {
31-
Scanner in = new Scanner(System.in);
32-
System.out.print("Enter number 1: ");
33-
int num1 = in.nextInt();
34-
System.out.print("Enter number 2: ");
35-
int num2 = in.nextInt();
36-
int sum = num1 + num2;
37-
System.out.println("The sum = " + sum);
34+
static int sum4(int i, int i1, int i2) {
35+
return i + i1 + i2;
3836
}
39-
40-
/*
41-
return_type name (arguments) {
42-
// body
43-
return statement;
44-
}
45-
46-
*/
4737
}

0 commit comments

Comments
 (0)