Java 公共最终比较器<;点>;斜率顺序

Java 公共最终比较器<;点>;斜率顺序,java,comparator,Java,Comparator,我必须实现一个带有“比较器”的API。但我不知道它是什么以及如何制作。请帮助。这是我的API: public class Point implements Comparable<Point> { public final Comparator<Point> SLOPE_ORDER; // compare points by slope to this point public Point(int x, int y)

我必须实现一个带有“比较器”的API。但我不知道它是什么以及如何制作。请帮助。这是我的API:

public class Point implements Comparable<Point> {
     public final Comparator<Point> SLOPE_ORDER;        // compare points by slope to   this point

     public Point(int x, int y)                         // construct the point (x, y)

     public   void draw()                               // draw this point
     public   void drawTo(Point that)                   // draw the line segment from       this point to that point
     public String toString()                           // string representation

     public    int compareTo(Point that)                // is this point lexicographically    smaller than that point?
     public double slopeTo(Point that)                  // the slope between this point and that point
公共类点实现可比较{
public final Comparator SLOPE_ORDER;//按斜率将点与该点进行比较
公共点(intx,inty)//构造点(x,y)
public void draw()//绘制此点
public void drawTo(该点)//绘制从该点到该点的线段
公共字符串toString()//字符串表示法
public int compareTo(Point that)//这一点在词典上比那一点小吗?
公共双坡度To(该点)//该点与该点之间的坡度
}

“公共最终比较器斜率_顺序”一行是什么意思?怎么做呢

这是我的密码:

/*************************************************************************
 * Name:
 * Email:
 *
 * Compilation:  javac Point.java
 * Execution:
 * Dependencies: StdDraw.java
 *
 * Description: An immutable data type for points in the plane.
 *
 *************************************************************************/

import java.util.Comparator;

public class Point implements Comparable<Point> {

// compare points by slope
public final Comparator<Point> SLOPE_ORDER;       // YOUR DEFINITION HERE

private final int x;                              // x coordinate
private final int y;                              // y coordinate

// create the point (x, y)
public Point(int x, int y) {
    /* DO NOT MODIFY */
    this.x = x;
    this.y = y;
}

// plot this point to standard drawing
public void draw() {
    /* DO NOT MODIFY */
    StdDraw.point(x, y);
}

// draw line between this point and that point to standard drawing
public void drawTo(Point that) {
    /* DO NOT MODIFY */
    StdDraw.line(this.x, this.y, that.x, that.y);
}

// slope between this point and that point
public double slopeTo(Point that) {
    /* YOUR CODE HERE */
    double slope = (that.y*1.0-this.y)/(that.x*1.0-this.x);
    return slipe;
}

// is this point lexicographically smaller than that one?
// comparing y-coordinates and breaking ties by x-coordinates
public int compareTo(Point that) {
    /* YOUR CODE HERE */
    if(this.y<that.y) return 1;
    else if(this.y==that.y) {
        if(this.x<that.x) return 1;
        else return 0;
    }
    else return 0;
} 

// return string representation of this point
public String toString() {
    /* DO NOT MODIFY */
    return "(" + x + ", " + y + ")";
}

// unit test
public static void main(String[] args) {
    /* YOUR CODE HERE */
}
}
/*************************************************************************
*姓名:
*电邮:
*
*编译:javac Point.java
*执行:
*依赖项:StdDraw.java
*
*描述:平面中点的不可变数据类型。
*
*************************************************************************/
导入java.util.Comparator;
公共类点实现了可比性{
//按坡度比较点
public final Comparator SLOPE_ORDER;//这里是您的定义
私有final int x;//x坐标
私有final int y;//y坐标
//创建点(x,y)
公共点(整数x,整数y){
/*不要修改*/
这个.x=x;
这个。y=y;
}
//将此点绘制到标准图纸
公众抽签(){
/*不要修改*/
标准点(x,y);
}
//在该点和该点之间绘制一条线,以绘制标准图形
公共无效提取到(指向){
/*不要修改*/
StdDraw.line(this.x,this.y,that.x,that.y);
}
//这一点和那一点之间的坡度
公共双坡(指向该点){
/*你的代码在这里*/
双斜率=(that.y*1.0-this.y)/(that.x*1.0-this.x);
回程滑轨;
}
//这一点比那一点小吗?
//比较y坐标和通过x坐标断开连接
公共int比较(指向该点){
/*你的代码在这里*/
如果(this.y像这样

public final Comparator<Point> SLOPE_ORDER = new Comparator<Point>()
{
    @Override
    public int compare(Point p0, Point p1)
    {
        double s0 = slopeTo(p0);
        double s1 = slopeTo(p1);
        return Double.compare(s0, s1);
    }
};
public final Comparator SLOPE\u ORDER=new Comparator()
{
@凌驾
公共整数比较(p0点,p1点)
{
双s0=斜率(p0);
双s1=斜率(p1);
返回Double.compare(s0,s1);
}
};

。那么你问我们你想要什么?我想说点的斜率
(x,y)
应该是
y/x
,所以比较这个属性不会太难…我知道你说什么,但我要求表单实现“公共最终比较器斜率_顺序。请注意我的代码。我想你只需要给出一个实现,就可以在
点给出的“自然”顺序之外使用另一种排序方法。compareTo