-
Notifications
You must be signed in to change notification settings - Fork 0
Create Utility.java #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,67 @@ | ||||||||||||||||||||
| import java.util.regex.Pattern; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| public class Utility { | ||||||||||||||||||||
|
|
||||||||||||||||||||
| private static final String EMAIL_REGEX = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"; | ||||||||||||||||||||
| private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEX); | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| public static boolean isValidEmail(String email) { | ||||||||||||||||||||
| return EMAIL_PATTERN.matcher(email).matches(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| public static int add(int a, int b) { | ||||||||||||||||||||
| return a + b; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| public static int divide(int a, int b) { | ||||||||||||||||||||
| if (b == 0) { | ||||||||||||||||||||
| System.out.println("Warning: Division by zero. Returning 0 instead."); | ||||||||||||||||||||
| return 0; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| return a / b; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
| public static double calculateDiscountedPrice(double price, double discount) { | ||||||||||||||||||||
| double discountedPrice = price - (price * discount / 100); | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Validate the discount parameter range before calculating the discounted price. [possible bug]
Suggested change
|
||||||||||||||||||||
| return discountedPrice; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| public static String reverseString(String input) { | ||||||||||||||||||||
| if (input == null) { | ||||||||||||||||||||
| System.out.println("Warning: Input is null. Returning null."); | ||||||||||||||||||||
| return null; | ||||||||||||||||||||
|
Comment on lines
+34
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Change the null check in reverseString to throw an exception instead of printing a warning and returning null, ensuring errors are handled explicitly. [business logic]
Suggested change
|
||||||||||||||||||||
| } | ||||||||||||||||||||
| return new StringBuilder(input).reverse().toString(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| public static long factorial(int n) { | ||||||||||||||||||||
| if (n < 0) { | ||||||||||||||||||||
| System.out.println("Warning: Factorial of negative number. Returning -1."); | ||||||||||||||||||||
| return -1; | ||||||||||||||||||||
|
Comment on lines
+42
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Throw an exception for negative factorial inputs instead of printing a warning and returning a sentinel value. [business logic]
Suggested change
|
||||||||||||||||||||
| } | ||||||||||||||||||||
| long result = 1; | ||||||||||||||||||||
| for (int i = 1; i <= n; i++) { | ||||||||||||||||||||
| result *= i; | ||||||||||||||||||||
|
Comment on lines
+47
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Insert an overflow check in the factorial loop to prevent incorrect results for large inputs. [possible bug]
Suggested change
|
||||||||||||||||||||
| } | ||||||||||||||||||||
| return result; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| public static int findMax(int[] numbers) { | ||||||||||||||||||||
| if (numbers == null || numbers.length == 0) { | ||||||||||||||||||||
| System.out.println("Warning: Input array is null or empty. Returning Integer.MIN_VALUE."); | ||||||||||||||||||||
| return Integer.MIN_VALUE; | ||||||||||||||||||||
|
Comment on lines
+54
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Replace the System.out.println warning in findMax with an exception for null or empty arrays to avoid returning misleading sentinel values. [business logic]
Suggested change
|
||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| int max = numbers[0]; | ||||||||||||||||||||
| for (int number : numbers) { | ||||||||||||||||||||
| if (number > max) { | ||||||||||||||||||||
| max = number; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| return max; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Add a null check in isValidEmail to safely handle null inputs, avoiding potential NullPointerExceptions. [possible bug]