Commit 355e991
committed
feat: Add Methods class with array sum, string uppercase, overloading, and multiplier
WHAT the code does:
- Defines a `Methods` class demonstrating multiple static utility methods.
- `sumOfArray(int[])`: Iterates through array elements and prints their sum.
- `upperLetter(String)`: Converts a given string to uppercase after trimming spaces.
- Overloaded `sum()`:
- `sum(int, int)` → returns sum of two numbers.
- `sum(int, int, int)` → returns sum of three numbers.
- `multiplyBy10(int)`: Multiplies a number by 10.
- `main()` tests all methods with sample inputs and prints results.
WHY this matters:
- Demonstrates **method creation, reusability, and overloading** in Java.
- Highlights different method scopes (public vs private).
- Useful for learning **array iteration, string manipulation, arithmetic operations**.
- Shows clean separation of logic into individual methods.
HOW it works:
1. Array `{3, 3, 3, 4, 45, 35}` is passed → sum computed as `93`.
2. `"abcdefgh"` is converted to `"ABCDEFGH"`.
3. `sum(7, 3)` returns `10`; `sum(4, 3, 7)` returns `14`.
4. `multiplyBy10(10)` returns `100`.
Tips & gotchas:
- `sumOfArray` currently prints the sum; better design would return it for flexibility.
- `upperLetter` is private → accessible only within the class.
- For large arrays, `Arrays.stream(arr).sum()` could simplify logic.
- Be cautious with `trim().toUpperCase()` if null strings are passed (would cause `NullPointerException`).
Use-cases:
- Educational example for **static methods** in Java.
- Template for utility/helper classes.
- Basis for practicing method overloading and array handling.
- Can be extended to include more arithmetic or string utilities.
Short key: class-methods-utilities-overloading.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 654e99b commit 355e991
1 file changed
+19
-49
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
| 1 | + | |
12 | 2 | | |
13 | 3 | | |
14 | 4 | | |
15 | 5 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
| 6 | + | |
| 7 | + | |
27 | 8 | | |
| 9 | + | |
| 10 | + | |
28 | 11 | | |
| 12 | + | |
| 13 | + | |
29 | 14 | | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | 15 | | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
| 16 | + | |
46 | 17 | | |
47 | 18 | | |
48 | 19 | | |
49 | 20 | | |
50 | 21 | | |
51 | | - | |
| 22 | + | |
52 | 23 | | |
53 | 24 | | |
54 | | - | |
55 | | - | |
56 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
57 | 28 | | |
58 | 29 | | |
59 | | - | |
60 | | - | |
| 30 | + | |
| 31 | + | |
61 | 32 | | |
62 | 33 | | |
63 | | - | |
64 | | - | |
| 34 | + | |
| 35 | + | |
65 | 36 | | |
66 | 37 | | |
67 | | - | |
68 | | - | |
| 38 | + | |
| 39 | + | |
69 | 40 | | |
70 | | - | |
71 | | - | |
| 41 | + | |
0 commit comments