Skip to content

Commit 1da6078

Browse files
committed
feat: Implement utility methods for max, min, and string trimming with uppercase conversion
WHAT the code does: - Defines class MaxMinMethod with three static utility methods: - max(int a, int b): returns the larger of two integers. - min(int a, int b): returns the smaller of two integers. - trimAndUpperCase(String str): - If str is not null, trims leading/trailing spaces and converts it to uppercase. - If str is null, returns an empty string. - In main(): - Demonstrates max() with inputs 4 and 7. - Demonstrates min() with inputs 3 and 9. - Demonstrates trimAndUpperCase() with input `" hhhhhhh akakjskd "`. WHY this matters: - Shows how to create **static utility methods** for common operations, a common pattern in Java (similar to java.lang.Math or Apache Commons Lang). - Reinforces conditional logic: - If/else for determining maximum/minimum. - Null checks for safer string manipulation. - Demonstrates string processing using built-in methods trim() and toUpperCase(). HOW it works: 1. max(a, b): - Compares a and b, returns whichever is larger. 2. min(a, b): - Compares a and b, returns whichever is smaller. 3. trimAndUpperCase(str): - If str is null → returns "". - Else trims whitespace and converts to uppercase. 4. main(): - Calls each utility method and prints results. Tips and gotchas: - Java’s built-in `Math.max(a, b)` and `Math.min(a, b)` already provide these functionalities, but custom methods can be useful for demonstration. - Always perform null checks on strings before invoking methods like trim() or toUpperCase() to avoid NullPointerException. - Consider using Apache Commons StringUtils or Java 11’s `isBlank()` for more advanced string handling. - Utility classes like this are often declared `final` with a private constructor to prevent instantiation. Use-cases / analogies: - max/min: comparing scores, prices, or measurements to pick the higher or lower. - trimAndUpperCase: preparing user input (like names or IDs) for consistent storage and comparison in databases. Short key: java-utility-methods max min string-trim uppercase null-check. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent e68e1b7 commit 1da6078

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed
Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,32 @@
1-
public class MaxMinMethod
2-
{
3-
public static int max(int a, int b)
4-
{
5-
if (a > b)
6-
{
1+
public class MaxMinMethod {
2+
public static int max(int a, int b) {
3+
if (a > b) {
74
return a;
85
} else {
96
return b;
107
}
118
}
129

13-
public static int min(int a, int b)
14-
{
15-
if (a < b)
16-
{
10+
public static int min(int a, int b) {
11+
if (a < b) {
1712
return a;
1813
} else {
1914
return b;
2015
}
2116
}
2217

23-
public static String trimAndUpperCase(String str)
24-
{
25-
if(str != null){
18+
public static String trimAndUpperCase(String str) {
19+
if(str != null) {
2620
return str.trim().toUpperCase();
2721
}
28-
else{
22+
else {
2923
return "";
3024
}
3125
}
3226

3327
public static void main(String[] args){
34-
System.out.println("Max: " + MaxMinMethod.max(4,7)); // Output: Max: 7
35-
System.out.println("Min: " + MaxMinMethod.min(3,9)); // Output: Min: 3
36-
System.out.println("Trimmed and Uppercase: " + MaxMinMethod.trimAndUpperCase(" hhhhhhh akakjskd ")); // Output: HHHHHHH AKAKJSKD
28+
System.out.println("Max: " + MaxMinMethod.max(4,7));
29+
System.out.println("Min: " + MaxMinMethod.min(3,9));
30+
System.out.println("Trimmed and Uppercase: " + MaxMinMethod.trimAndUpperCase(" hhhhhhh akakjskd "));
3731
}
38-
}
32+
}

0 commit comments

Comments
 (0)