In: Computer Science
For these exercises you are required to edit in the Comparable interface implementation, a compareTo() method, starting with the class definition you obtained from the link, and submit that with a client that uses this ordering. If you are so inclined, you may submit a single client to drive your ordering enhancements for both classes, rather than one for each class.
The exercises, for those without the text:
19. Modify the Point class from Chapter 8 so that it defines a natural ordering by implementing the Comparable interface. Compare the Points by y-major order; that is, points with smaller y-coordinate values should come before those with higher y-coordinate values. Break ties by comparing x-coordinate values.
Please include comments, show the output, and put the text code( not just the screenshot of the code)
here are the class to use(Please use them):
1. Point.java
/* * Your file header comment block goes above this line *
* A Point object represents a pair of (x, y) coordinates.
* Class invariant: x >= 0 && y >= 0. (Quadrant I only)
*/
public class Point {
private int x; private int y;
// Constructs a new point at the given (x, y) location. // pre: x >= 0 && y >= 0
public Point(int x, int y) {
if (x < 0 || y < 0) {
throw new IllegalArgumentException();
}
this.x = x; this.y = y;
}
// Constructs a new point at the origin, (0, 0).
public Point() {
this(0, 0);
// calls Point(int, int) constructor
}
// Returns the distance between this Point and (0, 0).
public double distanceFromOrigin() {
return Math.sqrt(x * x + y * y);
}
// Returns whether o refers to a point with the same (x, y)
// coordinates as this point. Robust version.
public boolean equals(Object o) {
if (o instanceof Point) {
Point other = (Point) o;
return this.x == other.getX() && this.y == other.getY();
}
else { // not a Point object return false;
}
}
// Returns the x-coordinate of this point.
public int getX() { return this.x;
}
// Returns the y-coordinate of this point.
public int getY() { return this.y;
}
// Returns a String representation of this point.
public String toString() {
return "(" + this.x + ", " + this.y + ")";
}
// Returns a new point, shifted from this one by dx and dy.
// pre: x + dx >= 0 && y + dy >= 0
public Point translate(int dx, int dy) {
return new Point(this.x + dx, this.y + dy);
}
}
2. PointMainMin.java
/*
* Your file header comment block goes above this line
*
* A client program that deals with simple points.
* Minimal version, to accompany immutable Point class.
*/
public class PointMainMin {
public static void main(String[] args) {
// create two Point objects
Point p1 = new Point(7, 2);
Point p2 = new Point(4, 3);
// print each point and its distance from the origin
System.out.println("p1 is " + p1);
System.out.printf("distance from origin = %3.2f\n", p1.distanceFromOrigin());
System.out.println("p2 is " + p2);
System.out.printf("distance from origin = %3.2f\n", p2.distanceFromOrigin());
}
}
The interface is present in java.lang package and contains only one method called compareTo(object). It allows only a single sorting order. That is we can only sort the elements of a class on the basis of a single class member only.
(In this case its the value of y)
CompareTo method returns 1 if the current object is greater than the specified object.
Returns 0 if current object is equal to specified object.
Returns -1 if current object is less than the specified object.
On implementing the comparable interface, the point class definition looks like:
import java.util.*;
import java.lang.*;
public class point implements Comparable<point>{
private int x;
private int y;
public point(int x, int y){
if(x<0||y<0){
throw new IllegalArgumentException();
}
this.x = x;
this.y = y;
}
public point(){
this(0,0);
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
// Main change made to the class definition //
@Override
public int compareTo(point p){
/* returns 1 if this.getY() > p.getY()
returns 0 if this.getY == p.getY()
returns -1 if this.getY < p.getY() */
if(this.getY() == p.getY()){
return 0;
}
else if(this.getY() > p.getY()){
return 1;
}
else{
return -1;
}
}
// compareTo ends here
/* Remaining methods of point class*/
public double distancefromOrigin(){
return Math.sqrt(x*x + y*y);
}
public String toString(){
return "(" + this.x + "," + this.y +")";
}
public boolean equals(Object o){
if( o instanceof point){
point other = (Point) o;
return this.x == other.getX() &&
this.y == other.getY();
}else{
return false;
}
}
public Point translate( int dx, int dy){
return new point(this.x +dx,this.y + dy);
}
}