Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/progys/interview/quiz/model/Point.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.progys.interview.quiz.model;

import java.util.Objects;

/**
* Defines a point.
*
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down
34 changes: 34 additions & 0 deletions src/test/java/com/progys/interview/quiz/model/PointTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading