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
12 changes: 12 additions & 0 deletions AgeCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class AgeCalculator {Add commentMore actions
public static void calculateAge(int birthYear, int currentYear) {
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider improving method design for better reusability.

The method signature is clear, but consider returning the calculated age instead of printing it directly. This would improve reusability and follow the single responsibility principle.

-    public static void calculateAge(int birthYear, int currentYear) {
+    public static int calculateAge(int birthYear, int currentYear) {

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

🤖 Prompt for AI Agents
In AgeCalculator.java at line 2, the calculateAge method currently prints the
age directly, limiting reusability. Modify the method to return the calculated
age as an integer instead of printing it. This change will separate calculation
from output, adhering to the single responsibility principle and making the
method more versatile for different uses.

int birth = birthYear % 100; 1Code has comments. Press enter to view.
int current = currentYear % 100;
int age = current - birth;
Comment on lines +3 to +5
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix critical logic error in age calculation.

The modulo 100 approach is fundamentally flawed and produces incorrect results for most real-world scenarios. This method ignores century information entirely.

Example failures:

  • birthYear=1990, currentYear=2024 → calculated age=-66 (should be 34)
  • birthYear=1999, currentYear=2001 → calculated age=-98 (should be 2)

Apply this diff to fix the calculation:

-        int birth = birthYear % 100;
-        int current = currentYear % 100;
-        int age = current - birth;
+        int age = 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
int birth = birthYear % 100; 1Code has comments. Press enter to view.
int current = currentYear % 100;
int age = current - birth;
- int birth = birthYear % 100;
- int current = currentYear % 100;
- int age = current - birth;
+ int age = currentYear - birthYear;
🤖 Prompt for AI Agents
In AgeCalculator.java around lines 3 to 5, the age calculation uses modulo 100
on years, which ignores century information and causes incorrect negative ages.
Replace the logic by directly subtracting birthYear from currentYear to get the
correct age, ensuring the calculation accounts for the full year values without
modulo operations.

if (age < 0) {
System.out.println("Calculated age: " + age + " (Possible data error)");
} else {
System.out.println("Calculated age: " + age);
}
Comment on lines +6 to +10
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve input validation and error handling.

The negative age check is masking the real calculation logic error. With proper age calculation, negative results should only occur with invalid inputs (future birth year).

Consider adding proper input validation and removing the workaround for calculation errors:

+        if (birthYear > currentYear) {
+            throw new IllegalArgumentException("Birth year cannot be greater than current year");
+        }
+        
         int age = currentYear - birthYear;
-        if (age < 0) {
-            System.out.println("Calculated age: " + age + " (Possible data error)");
-        } else {
-            System.out.println("Calculated age: " + age);
-        }
+        return age;
📝 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
if (age < 0) {
System.out.println("Calculated age: " + age + " (Possible data error)");
} else {
System.out.println("Calculated age: " + age);
}
if (birthYear > currentYear) {
throw new IllegalArgumentException("Birth year cannot be greater than current year");
}
int age = currentYear - birthYear;
return age;
🤖 Prompt for AI Agents
In AgeCalculator.java around lines 6 to 10, the current code prints a warning
for negative age values but does not prevent or handle invalid input properly.
Improve this by adding input validation before the age calculation to check if
the birth year is in the future or otherwise invalid. Remove the conditional
print workaround for negative ages and instead ensure the calculation logic only
runs with valid inputs, throwing an error or returning a clear message if the
input is invalid.

}Add commentMore actions
}