Skip to content

Commit

Permalink
Chapter 20 new exercise solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryDulaney committed Sep 8, 2021
1 parent 7a4b918 commit 8ec62fb
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
32 changes: 32 additions & 0 deletions ch_20/exercise20_04/CompareY.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ch_20.exercise20_04;

import java.util.Comparator;

/**
* Define a class named CompareY that implements Comparator<Point>.
* Implement the compare method to compare two points on their y-coordinates.
* If two points have the same y-coordinates, compare their x-coordinates.
*/
public class CompareY implements Comparator<Point> {
@Override
public int compare(Point p1, Point p2) {
int resultY;
if (p1.y < p2.y) {
resultY = -1;
} else if (p1.y > p2.y) {
resultY = 1;
} else {
resultY = 0;
}
if (resultY == 0) {
if (p1.x < p2.x) {
return -1;
}
if (p1.x > p2.x) {
return 1;
}
return 0;
}
return resultY;
}
}
46 changes: 46 additions & 0 deletions ch_20/exercise20_04/Exercise20_04.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ch_20.exercise20_04;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
* *20.4 (Sort points in a plane) Write a program that meets the following requirements:
* ■ Define a class named Point with two data fields x and y to represent a
* point’s x- and y-coordinates. Implement the Comparable interface for comparing the points on x-coordinates.
* If two points have the same x-coordinates, compare their y-coordinates.
* ■ Define a class named CompareY that implements Comparator<Point>.
* Implement the compare method to compare two points on their y-coordinates.
* If two points have the same y-coordinates, compare their x-coordinates.
* ■ Randomly create 100 points and apply the Arrays.sort method to display
* the points in increasing order of their x-coordinates and in increasing order
* of their y-coordinates, respectively.
*/
public class Exercise20_04 {
private static Point[] points = new Point[100];
private static CompareY comparator = new CompareY();

public static void main(String[] args) {
Random randomPoint = new Random();
for (int i = 0; i < points.length; i++) {
points[i] = new Point(randomPoint.nextDouble() * randomPoint.nextInt(999), randomPoint.nextDouble() * randomPoint.nextInt(999));
}
System.out.println("Created 100 Random Points: ");
for (Point point : points) {
System.out.print(point.toString());
}

System.out.println("Increasing order by the x-coordinates: ");
Arrays.sort(points);
for (Point point : points) {
System.out.print(point.toString());
}

System.out.println("Increasing order by the y-coordinates: ");
Arrays.sort(points, comparator);
for (Point point : points) {
System.out.print(point.toString());
}
}
}
69 changes: 69 additions & 0 deletions ch_20/exercise20_04/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ch_20.exercise20_04;

/**
* ■ Define a class named Point with two data fields x and y to represent a
* * point’s x- and y-coordinates.
* Implement the Comparable interface for comparing the points on x-coordinates.
* If two points have the same x-coordinates, compare their y-coordinates.
*/
public class Point implements Comparable<Point> {
double x;
double y;

public Point(double x, double y) {
this.x = x;
this.y = y;
}

public double getX() {
return x;
}

public Point setX(double x) {
this.x = x;
return this;
}

public double getY() {
return y;
}

public Point setY(double y) {
this.y = y;
return this;
}


@Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
"}\n";
}

@Override
public int compareTo(Point that) {
if (this.x == that.x) {
if (this.y < that.y) {
return -1;
}
if (this.y > that.y) {
return 1;
}
return 0; // this.y == that.y

} else {
if (this.x < that.x) {
return -1;
}
if (this.x > that.x) {
return 1;
}
return 0;//this.x == that.x

}

}

}

0 comments on commit 8ec62fb

Please sign in to comment.