Skip to content

Commit 81cdf6e

Browse files
authored
BAEL-9425 Storing X and Y Coordinates in Java (#18766)
* Create XYCoordinatesUnitTest.java * Update and rename XYCoordinatesUnitTest.java to XYCoordinatesUnitTest.java * Create Point.java * Update XYCoordinatesUnitTest.java * Create PointRecord.java * Update XYCoordinatesUnitTest.java * Update XYCoordinatesUnitTest.java * Update XYCoordinatesUnitTest.java * Rename XYCoordinatesUnitTest.java to XYCoordinatesUnitTest.java * Rename Point.java to Point.java * Rename PointRecord.java to PointRecord.java * Update XYCoordinatesUnitTest.java * Update Point.java * Update PointRecord.java * Update XYCoordinatesUnitTest.java * Update XYCoordinatesUnitTest.java
1 parent 6a7a1a6 commit 81cdf6e

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.storingXYcoordinates;
2+
3+
import java.util.Objects;
4+
5+
public class Point {
6+
private final double x;
7+
private final double y;
8+
9+
public Point(double x, double y) {
10+
this.x = x;
11+
this.y = y;
12+
}
13+
14+
public double getX() {
15+
return x;
16+
}
17+
18+
public double getY() {
19+
return y;
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return "Point{" +
25+
"x=" + x +
26+
", y=" + y +
27+
'}';
28+
}
29+
30+
@Override
31+
public boolean equals(Object o) {
32+
if (this == o) return true;
33+
if (o == null || getClass() != o.getClass()) return false;
34+
Point point = (Point) o;
35+
return Double.compare(point.x, x) == 0 &&
36+
Double.compare(point.y, y) == 0;
37+
}
38+
39+
@Override
40+
public int hashCode() {
41+
return Objects.hash(x, y);
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.storingXYcoordinates;
2+
3+
public record PointRecord(double x, double y) {
4+
}
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.baeldung.storingXYcoordinates;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import java.awt.geom.Point2D;
6+
7+
class XYCoordinatesUnitTest {
8+
9+
@Test
10+
void givenAPoint_whenUsingGetter_thenPointReturnsCoordinatesCorrectly() {
11+
// Given a point with coordinates (10.0, 20.5)
12+
Point point = new Point(10.0, 20.5);
13+
14+
// Then its getters should return the correct values
15+
assertEquals(10.0, point.getX(), "X coordinate should be 10.0");
16+
assertEquals(20.5, point.getY(), "Y coordinate should be 20.5");
17+
}
18+
19+
@Test
20+
void givenTwoPointsWithSameCoordinates_whenComparedForEquality_thenShouldBeEqual() {
21+
Point point1 = new Point(5.1, -3.5);
22+
Point point2 = new Point(5.1, -3.5);
23+
24+
assertEquals(point1, point2, "Points with same coordinates should be equal");
25+
}
26+
27+
@Test
28+
void givenAPointRecord_whenUsingAccessorMethods_thenRecordReturnsCoordinatesCorrectly() {
29+
// Given a record with coordinates (30.5, 40.0)
30+
PointRecord point = new PointRecord(30.5, 40.0);
31+
32+
// Then its accessor methods should return the correct values
33+
assertEquals(30.5, point.x(), "X coordinate should be 30.5");
34+
assertEquals(40.0, point.y(), "Y coordinate should be 40.0");
35+
}
36+
37+
@Test
38+
void givenTwoRecordsWithSameCoordinates_whenComparedForEquality_thenShouldBeEqual() {
39+
PointRecord point1 = new PointRecord(7.0, 8.5);
40+
PointRecord point2 = new PointRecord(7.0, 8.5);
41+
42+
assertEquals(point1, point2, "Records with same coordinates should be equal");
43+
}
44+
45+
@Test
46+
void givenAnAWTPoint_whenAccessingItsFieldsAndGetters_thenReturnsCoordinatesCorrectly() {
47+
// Given an AWT Point
48+
Point2D.Double point = new Point2D.Double(10.5, 20.5);
49+
50+
// Then its public fields should hold the correct values
51+
assertEquals(10.5, point.x);
52+
assertEquals(20.5, point.y);
53+
54+
// And its getters should also work
55+
assertEquals(10.5, point.getX());
56+
assertEquals(20.5, point.getY());
57+
}
58+
59+
@Test
60+
void givenAnAWTPointForIntegerCoordinates_whenAccessingItsFieldsAndGetters_thenReturnsCoordinatesCorrectly() {
61+
// Given an AWT Point
62+
java.awt.Point point = new java.awt.Point(50, 60);
63+
64+
// Then its public fields should hold the correct values
65+
assertEquals(50, point.x);
66+
assertEquals(60, point.y);
67+
68+
// And its getters should also work
69+
assertEquals(50, point.getX());
70+
assertEquals(60, point.getY());
71+
}
72+
73+
@Test
74+
void givenArrayOfCoordinates_whenAccessingArrayIndices_thenReturnsCoordinatesAtCorrectIndices() {
75+
// Given an array representing coordinates (15.0, 25.0)
76+
double[] coordinates = new double[2];
77+
coordinates[0] = 15.0; // X
78+
coordinates[1] = 25.0; // Y
79+
80+
// Then index 0 should be X and index 1 should be Y
81+
assertEquals(15.0, coordinates[0], "Index 0 should be the X coordinate");
82+
assertEquals(25.0, coordinates[1], "Index 1 should be the Y coordinate");
83+
}
84+
}

0 commit comments

Comments
 (0)