forked from HarryDulaney/intro-to-java-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a4b918
commit 8ec62fb
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} | ||
|
||
} | ||
|
||
} |