Skip to content

Commit 355e991

Browse files
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

File tree

1 file changed

+19
-49
lines changed

1 file changed

+19
-49
lines changed
Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,41 @@
1-
/*
2-
Syntax
3-
4-
accessModifier returnType methodName(parameter)
5-
{
6-
//Method Body
7-
}
8-
*/
9-
10-
public class Methods
11-
{
1+
public class Methods {
122
public static void main(String[] args) {
133
int[] a = {3, 3, 3, 4, 45, 35};
144
sumOfArray(a);
155

16-
String res = upperFun("abcdefgh");
17-
System.out.println(res);
18-
19-
20-
System.out.println(sum(7, 3));
21-
System.out.println(sum(4,3,7));
22-
23-
int x =10;
24-
System.out.println(multiplyBy10(10));
25-
System.out.println(x);
26-
6+
String str = upperLetter("abcdefgh");
7+
System.out.println("Upper case letters: " + str);
278

9+
System.out.println(sum(7, 3)+" Sum of Two Numbers. ");
10+
System.out.println(sum(4, 3, 7)+" Sum of Three Numbers. ");
2811

12+
int x = 10;
13+
System.out.println("Multiply By 10 is: "+multiplyBy10(10));
2914
}
30-
/*
31-
int a =2;
32-
int b =2;
3315

34-
System.out.println("Sum: "+(a+b));
35-
36-
int arr[] = {1, 2, 3, 4, 5};
37-
int res =0;
38-
39-
for(int i : arr)
40-
{
41-
res += i;
42-
}
43-
System.out.println(res);
44-
*/
45-
public static void sumOfArray(int arr[])
16+
public static void sumOfArray(int arr[]) //printing sum of the array.
4617
{
4718
int res = 0;
4819
for (int i : arr) {
4920
res += i;
5021
}
51-
System.out.println(res);
22+
System.out.println("Sum of array: " + res);
5223
}
5324

54-
private static String upperFun(String str)
55-
{
56-
return str.trim().toUpperCase();
25+
//creating a method name upperLetter and call that method above.and print after that.
26+
private static String upperLetter(String str) {
27+
return str.trim().toUpperCase();
5728
}
5829

59-
public static int sum(int a, int b){
60-
return a+b;
30+
public static int sum(int a, int b) {
31+
return a + b;
6132
}
6233

63-
public static int sum(int a, int b,int c ){
64-
return a+b+c;
34+
public static int sum(int a, int b, int c) {
35+
return a + b + c;
6536
}
6637

67-
public static int multiplyBy10(int x){
68-
return x *10;
38+
public static int multiplyBy10(int x) {
39+
return x * 10;
6940
}
70-
71-
}
41+
}

0 commit comments

Comments
 (0)