Line Class卡在java的几个方法上

时间:2014-11-03 05:18:18

标签: java

使用istance字段x1,y1,x2,y2,slope,yIntercept和slopeDefined实现一个“Line”类。我的代码中也包含四种方法。我必须实现以下方法。

public boolean intersects(Line L2)public boolean equals(Line L2)public boolean isParalell(Line, L2)

public class LineClass {

    double x1, y1, x2, y2;
    double yIntercept;
    boolean slopeDefined;

    // slope and point
    public LineClass(double s, double xOne, double yOne) {
        x1 = xOne;
        y1 = yOne;
        slope = s;
    }

    // two points
    public LineClass(double xOne, double yOne, double xTwo, double yTwo) {
        x1 = xOne;
        x2 = xTwo;
        y1 = yOne;
        y2 = yTwo;
    }

    // slope and y- intercept
    public LineClass(double s, double y) {
        slope = s;
        yIntercept = y;
    }

    // vertical line, x-intercept
    public LineClass(double xOne) {
        x1 = xOne;
    }

    // returns true if Line this intersects Line L2, false otherwise
    public boolean intersects(LineClass L2) {
        if (this.slope != L2.slope)
            slopeDefined = true;
        else
            slopeDefined = false;
        return slopeDefined;
    }

    // returns true if Line this is the same as Line L2, false otherwise
    public boolean equals(LineClass L2) {
        if (this.slopeDefined == L2.slope) && (this.)

    }
}

问题出在public boolean equals,我使用什么方法?因为当我使用if(this.slopeDefined == L2.slope)时它不起作用

2 个答案:

答案 0 :(得分:0)

// returns true if Line this is the same as Line L2, false otherwise
public boolean equals(LineClass L2) {
    return x1 == L2.x1 &&
           x2 == L2.x2 &&
           y1 == L2.y1 &&
           y2 == L2.y2 && 
           slopeDefined == L2.slopeDefined &&
           yIntercept == L2.yIntercept;
}

如果LineClass个对象中的变量的所有值都相同,则返回true。

答案 1 :(得分:0)

您尚未声明所有变量,并且您的equals方法不完整。

添加:

  

双斜率;

和此:

  

//如果Line与Line L2相同则返回true,否则返回

     

public boolean equals(LineClass L2){

     

if(L2.slope == slope&& L2.yIntercept == yIntercept)

   return true;
     

否则

   return false;
     

}

这假设您只是检查线条在数学上是否相等。