Skip to content

Create AgeCalculator.java#1

Open
vivek-kumar-2024 wants to merge 1 commit intomainfrom
vivek-kumar-2024-patch-1
Open

Create AgeCalculator.java#1
vivek-kumar-2024 wants to merge 1 commit intomainfrom
vivek-kumar-2024-patch-1

Conversation

@vivek-kumar-2024
Copy link
Owner

@vivek-kumar-2024 vivek-kumar-2024 commented Jun 10, 2025

Summary by CodeRabbit

  • New Features
    • Added a new tool to calculate age based on birth year and current year.

@coderabbitai
Copy link

coderabbitai bot commented Jun 10, 2025

Walkthrough

A new class called AgeCalculator has been added, featuring a public static method calculateAge. This method computes age by subtracting the last two digits of the birth year from those of the current year, both provided as integer arguments.

Changes

File(s) Change Summary
AgeCalculator.java Added new AgeCalculator class with static calculateAge method.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant AgeCalculator

    Caller->>AgeCalculator: calculateAge(birthYear, currentYear)
    AgeCalculator-->>Caller: Returns age (last two digits difference)
Loading

Poem

In a field of numbers, rabbits hop with glee,
A brand new class appears—how clever can it be?
It snips the years with modulo, quick as a hare,
Calculating ages with mathematical flair!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 70933b0 and b62cff0.

📒 Files selected for processing (1)
  • AgeCalculator.java (1 hunks)

Comment on lines +2 to +7
public static int calculateAge(int birthYear, int currentYear) {
int birth = birthYear % 100;
int current = currentYear % 100;
int age = current - birth;
return age;
}
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
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;
}
}
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant