feat(triangle): add Triangle class and tests#138
feat(triangle): add Triangle class and tests#138MudasirStanikzay wants to merge 1 commit intomainfrom
Conversation
WalkthroughIntroduces a new Triangle class with validation, perimeter, and area calculations using Heron’s formula, plus getters. Adds corresponding JUnit 5 tests covering valid creation, invalid inputs, perimeter, and area. No other files changed. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor Client
participant Triangle
rect rgb(238,245,255)
note over Client,Triangle: Construction and validation
Client->>Triangle: new Triangle(a,b,c)
alt any side <= 0
Triangle-->>Client: throw IllegalArgumentException("Sidornas längd måste vara större än 0.")
else violates triangle inequality
Triangle-->>Client: throw IllegalArgumentException("Ogiltig triangel enligt triangelolikheten.")
else valid
Triangle-->>Client: instance created
end
end
rect rgb(241,252,241)
note over Client,Triangle: Computations
Client->>Triangle: getPerimeter()
Triangle-->>Client: a + b + c
Client->>Triangle: getArea()
Triangle-->>Client: sqrt(s*(s-a)*(s-b)*(s-c))
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
✨ Finishing Touches
🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (6)
src/main/java/org/fungover/breeze/geometry/Triangle.java (5)
4-6: Make fields immutable (final)These fields are set only in the constructor; mark them
finalto enforce immutability and improve thread-safety/readability.- private double a; - private double b; - private double c; + private final double a; + private final double b; + private final double c;
8-19: Validate finiteness and standardize exception messages
- Guard against NaN/Infinity explicitly for clearer failure modes.
- If the codebase uses English, consider switching messages to English for consistency.
public Triangle(double a, double b, double c) { - if (a <= 0 || b <= 0 || c <= 0) { - throw new IllegalArgumentException("Sidornas längd måste vara större än 0."); - } - if (!isValidTriangle(a, b, c)) { - throw new IllegalArgumentException("Ogiltig triangel enligt triangelolikheten."); - } + if (!Double.isFinite(a) || !Double.isFinite(b) || !Double.isFinite(c)) { + throw new IllegalArgumentException("Side lengths must be finite numbers."); + } + if (a <= 0 || b <= 0 || c <= 0) { + throw new IllegalArgumentException("Side lengths must be greater than 0."); + } + if (!isValidTriangle(a, b, c)) { + throw new IllegalArgumentException("Invalid triangle: violates triangle inequality."); + } this.a = a; this.b = b; this.c = c; }
21-23: Triangle inequality check looks good; considerstaticMethod is pure and does not rely on instance state. Making it
staticcommunicates intent.- private boolean isValidTriangle(double a, double b, double c) { + private static boolean isValidTriangle(double a, double b, double c) { return a + b > c && a + c > b && b + c > a; }
30-34: Clamp radicand to avoid NaN from FP roundoff in near-degenerate trianglesFor very skinny valid triangles, rounding can make the radicand slightly negative. Clamping prevents spurious NaNs.
public double getArea() { - double s = getPerimeter() / 2; - return Math.sqrt(s * (s - a) * (s - b) * (s - c)); + double s = getPerimeter() / 2.0; + double radicand = s * (s - a) * (s - b) * (s - c); + return Math.sqrt(Math.max(0.0, radicand)); }
36-39: Optional naming tweakIf you want a more descriptive API, consider
getSideA/B/C. Not required.src/test/java/org/fungover/breeze/geometry/TriangleTest.java (1)
27-32: Use tolerance for floating-point comparison and add a couple more negative-path tests
- Prefer a delta for doubles to avoid brittleness.
- Add tests for zero/negative sides and degenerate equality (a + b == c).
@Test void testArea() { Triangle t = new Triangle(3, 4, 5); - assertEquals(6, t.getArea()); + assertEquals(6.0, t.getArea(), 1e-9); } + + @Test + void testZeroOrNegativeSidesThrow() { + assertAll( + () -> assertThrows(IllegalArgumentException.class, () -> new Triangle(0, 1, 1)), + () -> assertThrows(IllegalArgumentException.class, () -> new Triangle(-1, 1, 1)) + ); + } + + @Test + void testDegenerateTriangleThrows() { + assertThrows(IllegalArgumentException.class, () -> new Triangle(1, 2, 3)); + }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (2)
src/main/java/org/fungover/breeze/geometry/Triangle.java(1 hunks)src/test/java/org/fungover/breeze/geometry/TriangleTest.java(1 hunks)
🔇 Additional comments (4)
src/main/java/org/fungover/breeze/geometry/Triangle.java (1)
25-28: LGTM: perimeterStraightforward and correct.
src/test/java/org/fungover/breeze/geometry/TriangleTest.java (3)
8-14: LGTM: valid creation and gettersCovers the happy path clearly.
16-19: Good: triangle inequality failureConsistently exercises invalid input.
21-25: LGTM: perimeter testSimple and correct.
There was a problem hiding this comment.
Good validation for positive side lengths and triangle inequality.
Correct use of Heron’s formula for area calculation.
Clean and readable implementation with clear method separation.
There was a problem hiding this comment.
Tests are simple, clear, and easy to understand.
Covers both valid and invalid triangle creation.
Checks correctness of both perimeter and area.
Added Triangle class with basic methods and TriangleTest with all tests passing.
Summary by CodeRabbit
New Features
Tests