2014-10-20 29 views
-1

有人能告訴我我做錯了什麼嗎?我在代碼下面列出了錯誤。我只是用Java語言讓自己的腳步變得如此糟糕,請在你的迴應中表現出色。當我運行驅動一個點類的位置

//The Point class definition 
public class Point 
    { 
    private int x; 
    private int y; 

    // Constructors 
    public Point() 
    { 
     x = 0; 
     y = 0; 
    } 

    public Point(int newX, int newY) 
    { 
     x = newX; 
     y = newY; 
    } 
    // Getters and Setters 
    public int getX() 
    { 
     return x; 
    } 

    public int getY() 
    { 
     return y; 
    } 

    public void setX(int newX) 
    { 
     x = newX; 
    } 

    public void setY(int newY) 
    { 
     y = newY; 
    } 

    public double distance(Point another) //takes one parameter of Point type and returns a double 
    { 
     double xDiff = x - another.x; 
     double yDiff = y - another.y; 
     return Math.sqrt(xDiff*xDiff + yDiff*yDiff); 
    } 

    public void translate(int dx, int dy) 
//takes two int parameters for values used to translate,returns a new point with a new location (do not update current location) point. 
    {      
     x = x + dx; 
     y = y + dy; 
    } 

    public void setXY(int newX, int newY) //updates the location of the point all at one time. 
    { 
     x = newX; 
     y = newY; 
    } 

    public String toString() 
    { 
     return "(" + x + "," + y + ")"; 
    } 

} 

錯誤:

PointDriver.java:24: error: method distance in class Point cannot 
be applied to given types; 
     pt3 = pt2.distance(3, -10); 
       ^ required: Point found: int,int reason: actual and formal argument lists differ in length 
PointDriver.java:31: error: method distance in class Point cannot be 
applied to given types; 
     pt1 = pt1.distance(4, -2); 
       ^ required: Point 
+3

難道你不明白什麼部分錯誤消息的? – SLaks 2014-10-20 20:11:10

+0

由於錯誤發生在'PointDriver'可能表明代碼會有好處?你是否應該定義一個方法'公共雙距離(int x,int y){}'? – clcto 2014-10-20 20:13:03

+1

您已經定義了distance()方法來獲取Point參數,但試圖傳入兩個整數。 – BarrySW19 2014-10-20 20:14:17

回答

0

它看起來像錯誤發生,因爲您傳遞兩個整數給需要一個Point的方法。但是如果沒有看到錯誤中實際引用的代碼,則很難確定。

我要去猜測,你行:

pt3 = pt2.distance(3, -10); 

或許應該是這樣的:

pt3 = pt2.distance(new Point(3, -10)); 

或者可能是這樣的:

Point p4 = new Point(3,-10); 
pt3 = pt2.distance(pt4); 

或者,也許你應該在Point中寫入新方法:

public double distance(int x, int y) 
{ 
    Point p = new Point(x,y); 
    return this.distance(p); 
} 
0

距離方法需要一個點不是分隔座標。 使用:

pt3 = pt2.distance(new Point(3, -10)); 
0

你應該傳遞一個點對象不是2個整數的距離。嘗試這樣的:

pt2.distance(new Point(3, -10)); 

這就是你可能正在尋找。

0

問題是,您沒有用Point調用方法距離。它會的工作,當你做這樣的:

Point t = new Point(4, -2); 
pt1.distance(t); 

另一個問題在於

pt1 = pt1.distance(4, -2),因爲PT1是一個點,但返回的值是一個雙。

你可以是這樣做的:

double p1 = pt1.distance(4, -2) 
相關問題