-
Notifications
You must be signed in to change notification settings - Fork 0
Create AgeCalculator.java #8
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: main
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,12 @@ | ||||||||||||||||||||||||
| public class AgeCalculator {Add commentMore actions | ||||||||||||||||||||||||
| public static void calculateAge(int birthYear, int currentYear) { | ||||||||||||||||||||||||
| 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
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. 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:
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
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||
| 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
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. 🛠️ 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
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||
| }Add commentMore actions | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
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.
🛠️ 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.
🤖 Prompt for AI Agents