From e367286ea80a243c6e5c9823caf754f4a434e965 Mon Sep 17 00:00:00 2001 From: progys Date: Sun, 16 Aug 2026 22:19:44 +0300 Subject: [PATCH] Fix README example output to match actual, add Point value semantics, remove dead branch - README: fix Example session output (capital Shape, => prefix on point-query lines, triangle/donut toString formats, corrected area precision) to match actual program output. - Point: add equals/hashCode/value semantics for correct behavior in collections and mocks. - ShapeEntity: remove unreachable default branch in the constructor switch (sealed Shape makes it dead code); keep the toShape default as a DB safety check. --- README.md | 14 ++++---- .../progys/interview/quiz/model/Point.java | 14 ++++++++ .../quiz/persistence/ShapeEntity.java | 2 -- .../interview/quiz/model/PointTest.java | 34 +++++++++++++++++++ 4 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 src/test/java/com/progys/interview/quiz/model/PointTest.java diff --git a/README.md b/README.md index 7d0a1ea..adb9448 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ Once running, type `help` to print the list of available commands with usage exa Every created shape is assigned a unique identifier and printed back in a standardized form, for example: ``` -=> shape 1: circle with centre at (1.7, -5.05) and radius 6.9 +=> Shape 1: circle with centre at (1.7, -5.05) and radius 6.9 ``` ### Other commands @@ -112,18 +112,18 @@ Every created shape is assigned a unique identifier and printed back in a standa ``` circle 1.7 -5.05 6.9 -=> shape 1: circle with centre at (1.7, -5.05) and radius 6.9 +=> Shape 1: circle with centre at (1.7, -5.05) and radius 6.9 triangle 4.5 1 -2.5 -33 23 0.3 -=> shape 2: triangle with vertices at (4.5, 1) (-2.5, -33) (23, 0.3) +=> Shape 2: triangle at v0=(4.5, 1.0), v1=(-2.5, -33.0), v2=(23.0, 0.3) donut 4.5 7.8 1.5 1.8 -=> shape 3: donut with centre at (4.5, 7.8) inner radius 1.5 and outer radius 1.8 +=> Shape 3: donut with centre at (4.5, 7.8) with inner radius 1.5 and outer radius 1.8 5.1 6.2 Shape list containing point (5.1, 6.2): -shape 3: donut with centre at (4.5, 7.8) inner radius 1.5 and outer radius 1.8; Shape area: 3.11 -Found 1 shapes containing point (5.1, 6.2). Surface area combined: 3.11 +=> Shape 3: donut with centre at (4.5, 7.8) with inner radius 1.5 and outer radius 1.8 ; Shape area: 3.11 +Found 1 shapes containing point (5.1, 6.2). Surface area combined: 3.1102 ``` ### File input @@ -178,7 +178,7 @@ It is a full programming exercise whose outcome should be code that can be compi For the circle, the numbers are the x and y coordinates of the centre followed by the radius. For the triangle it is the x and y coordinates of the three vertices (six numbers in total). For the donut it is the x and y of the centre followed by the two radii. In addition, every time such a line is entered, the application should give it a unique identifier and print it out in a standardized form, for example: ``` - => shape 1: circle with centre at (1.7, -5.05) and radius 6.9 + => Shape 1: circle with centre at (1.7, -5.05) and radius 6.9 ``` 2. When the user enters a pair of numbers, the application should print out all the shapes that include that point in the (x, y) space, i.e. it should print out shape S if the given point is inside S. (A point is inside a donut shape if it is inside the outer circle but not inside the inner one.) It should also print out the surface area of each shape found, and the total area of all the shapes returned for a given point. diff --git a/src/main/java/com/progys/interview/quiz/model/Point.java b/src/main/java/com/progys/interview/quiz/model/Point.java index 3cb547a..331cad5 100644 --- a/src/main/java/com/progys/interview/quiz/model/Point.java +++ b/src/main/java/com/progys/interview/quiz/model/Point.java @@ -1,5 +1,7 @@ package com.progys.interview.quiz.model; +import java.util.Objects; + /** * Defines a point. * @@ -22,6 +24,18 @@ public double getY() { return y; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Point other)) return false; + return Double.compare(x, other.x) == 0 && Double.compare(y, other.y) == 0; + } + + @Override + public int hashCode() { + return Objects.hash(x, y); + } + @Override public String toString() { return String.format("(%s, %s)", x, y); diff --git a/src/main/java/com/progys/interview/quiz/persistence/ShapeEntity.java b/src/main/java/com/progys/interview/quiz/persistence/ShapeEntity.java index a2be2ad..f0fbd41 100644 --- a/src/main/java/com/progys/interview/quiz/persistence/ShapeEntity.java +++ b/src/main/java/com/progys/interview/quiz/persistence/ShapeEntity.java @@ -62,8 +62,6 @@ public ShapeEntity(Shape shape) { outerRadius = donut.getOuterCircle().getRadius(); } case null -> throw new IllegalArgumentException("Cannot store a null shape"); - default -> throw new IllegalArgumentException( - "Unsupported shape type: " + shape.getClass().getName()); } } diff --git a/src/test/java/com/progys/interview/quiz/model/PointTest.java b/src/test/java/com/progys/interview/quiz/model/PointTest.java new file mode 100644 index 0000000..8c087aa --- /dev/null +++ b/src/test/java/com/progys/interview/quiz/model/PointTest.java @@ -0,0 +1,34 @@ +package com.progys.interview.quiz.model; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class PointTest { + + @Test + void equalPointsAreEqual() { + assertThat(new Point(1.5, 2.5)).isEqualTo(new Point(1.5, 2.5)); + } + + @Test + void differentPointsAreNotEqual() { + assertThat(new Point(1.5, 2.5)).isNotEqualTo(new Point(1.5, 3.0)); + } + + @Test + void equalPointsHaveSameHashCode() { + assertThat(new Point(1.5, 2.5).hashCode()).isEqualTo(new Point(1.5, 2.5).hashCode()); + } + + @Test + void equalsIsReflexive() { + Point p = new Point(0, 0); + assertThat(p).isEqualTo(p); + } + + @Test + void equalsTreatsNullAsNotEqual() { + assertThat(new Point(1, 2)).isNotEqualTo(null); + } +}