Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions AgeCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public class AgeCalculator {
public static int calculateAge(int birthYear, int currentYear) {
int birth = birthYear % 100;
int current = currentYear % 100;
int age = current - birth;
return age;
}
Comment on lines +2 to +7
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Critical logic error: Age calculation algorithm is fundamentally flawed.

The current implementation using modulo 100 to extract last two digits will produce incorrect results in most scenarios:

  1. Century boundary issues: Born in 1999, current year 2001 → 01 - 99 = -98 (should be 2)
  2. Negative ages: Any birth year ending in digits higher than current year digits
  3. Incorrect calculations: Born in 1985, current year 2023 → 23 - 85 = -62 (should be 38)

Replace with proper age calculation:

-    public static int calculateAge(int birthYear, int currentYear) {
-        int birth = birthYear % 100;
-        int current = currentYear % 100;
-        int age = current - birth;
-        return age;
+    public static int calculateAge(int birthYear, int currentYear) {
+        if (currentYear < birthYear) {
+            throw new IllegalArgumentException("Current year cannot be before birth year");
+        }
+        return currentYear - birthYear;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public static int calculateAge(int birthYear, int currentYear) {
int birth = birthYear % 100;
int current = currentYear % 100;
int age = current - birth;
return age;
}
public static int calculateAge(int birthYear, int currentYear) {
if (currentYear < birthYear) {
throw new IllegalArgumentException("Current year cannot be before birth year");
}
return currentYear - birthYear;
}
🤖 Prompt for AI Agents
In AgeCalculator.java lines 2 to 7, the age calculation using modulo 100 on
years causes incorrect results across century boundaries and negative ages.
Replace this logic by directly subtracting birthYear from currentYear to get the
correct age. Ensure the method returns currentYear minus birthYear without using
modulo operations.

}
Comment on lines +1 to +8
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add input validation and documentation.

Consider adding comprehensive input validation and JavaDoc documentation to make the class more robust and maintainable.

+/**
+ * Utility class for age-related calculations.
+ */
 public class AgeCalculator {
+    /**
+     * Calculates age based on birth year and current year.
+     * 
+     * @param birthYear the year of birth
+     * @param currentYear the current year
+     * @return the calculated age in years
+     * @throws IllegalArgumentException if currentYear < birthYear or years are invalid
+     */
     public static int calculateAge(int birthYear, int currentYear) {
+        if (birthYear < 1900 || birthYear > currentYear) {
+            throw new IllegalArgumentException("Invalid birth year: " + birthYear);
+        }
+        if (currentYear < 1900) {
+            throw new IllegalArgumentException("Invalid current year: " + currentYear);
+        }
         return currentYear - birthYear;
     }
 }

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In AgeCalculator.java lines 1 to 8, the calculateAge method lacks input
validation and documentation. Add JavaDoc comments to describe the class and
method purpose, parameters, and return value. Implement input validation to
check that birthYear and currentYear are reasonable (e.g., non-negative and
currentYear is not before birthYear) and throw appropriate exceptions if invalid
inputs are detected.