Commit 1da6078
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
1 file changed
+12
-18
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
7 | 4 | | |
8 | 5 | | |
9 | 6 | | |
10 | 7 | | |
11 | 8 | | |
12 | 9 | | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
| 10 | + | |
| 11 | + | |
17 | 12 | | |
18 | 13 | | |
19 | 14 | | |
20 | 15 | | |
21 | 16 | | |
22 | 17 | | |
23 | | - | |
24 | | - | |
25 | | - | |
| 18 | + | |
| 19 | + | |
26 | 20 | | |
27 | 21 | | |
28 | | - | |
| 22 | + | |
29 | 23 | | |
30 | 24 | | |
31 | 25 | | |
32 | 26 | | |
33 | 27 | | |
34 | | - | |
35 | | - | |
36 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
37 | 31 | | |
38 | | - | |
| 32 | + | |
0 commit comments