2014-02-05 64 views
0
// CentredBall class: 
    2 // Class attribute: quantity - number of balls created 
    3 // Instance attributes: colour, radius, centre 
    4 import java.awt.*; 
    5 
    6 class CentredBall { 
    7 
    8  /************** Data members **********************/ 
    9  private static int quantity = 0; 
10 
11  private String colour; 
12  private double radius; 
13  private Point centre; 
14  private int xCoord, yCoord; 
15 
16  /************** Constructors **********************/ 
17  // Default constructor creates a yellow, radius 10.0 ball centred at origin (0,0) 
18  public CentredBall() { 
19   this.colour = "yellow"; 
20   this.radius = 10.0; 
21  } 
22  
23  public CentredBall(String colour, double radius, Point centre) { 
24   setColour(colour); 
25   setRadius(radius); 
26   setCentre(xCoord, yCoord); 
27   quantity++; 
28  } 
29  
30  public CentredBall(String colour, double radius, int xCoord, int yCoord) { 
31   setColour(colour); 
32   setRadius(radius); 
33   setxCoord(xCoord); 
34   setyCoord(yCoord); 
35   quantity++; 
36  } 
37  
38  /**************** Accessors ***********************/ 
39  public static int getQuantity() { 
40   return quantity; 
41  } 
42  public String getColour() { 
43   return this.colour; 
44  } 
45  public double getRadius() { 
46   return this.radius; 
47  } 
48  public Point getCentre() { 
49   return this.centre; 
50  } 
51  public int getxCoord() { 
52   return this.xCoord; 
53  } 
54  public int getyCoord() { 
55   return this.yCoord; 
56  } 
57 
58  /**************** Mutators ************************/ 
59  public void setColour(String colour) { 
60   this.colour = colour; 
61  } 
62  public void setRadius(double radius) { 
63   this.radius = radius; 
64  } 
65  public void setCentre(Point centre) { 
66   this.centre = centre; 
67  } 
68  public void setxCoord(int xCoord) { 
69   this.xCoord = xCoord; 
70  } 
71  public void setyCoord(int yCoord) { 
72   this.yCoord = yCoord; 
73  } 
74 
75  /***************** Overriding methods ******************/ 
76  // Overriding toString() method 
77  public String toString() { 
78   return "[" + getColour() + ", " + getRadius() + ", " + getxCoord() + ", " + getyCoord() + "]"; 
79  } 
80 
81  // Overriding equals() method 
82  public boolean equals(Object obj) { 
83   if (obj instanceof CentredBall) { 
84    CentredBall ball = (CentredBall) obj; 
85    return this.getColour().equals(ball.getColour()) && 
86      this.getRadius() == ball.getRadius() && 
87      this.getxCoord() == ball.getxCoord() && 
88      this.getyCoord() == ball.getyCoord(); 
89   } 
90   else 
91    return false; 
92  } 

我不斷收到此錯誤方法不能適用於給定類型的錯誤

CentredBall.java:26: error: method setCentre in class CentredBall cannot be applied to given types; 
      setCentre(xCoord, yCoord); 
      ^
required: Point 
found: int,int 
reason: actual and formal argument lists differ in length 
1 error 

我有幾個問題。請幫助。

  1. 爲什麼默認的構造函數沒有中心原點的參數?我試過這樣做(「黃色」,10.0,(0,0));但它不起作用

  2. 重寫方法的用途是什麼?我創建的重寫方法的toStringequals是否有錯誤?

回答

0

你所得到的錯誤,因爲setCentre()方法需要一個點,而不是2個整數:

setCentre(new Point(xCoord, yCoord)); 

而且爲:

1.爲什麼你不只是添加另一個構造函數原點:

public CentredBall(String colour, double radius) { 
setCentre(0,0); 

2.你寫了這個,但你不知道你爲什麼寫了嗎?你重寫它們,以便它們按照你希望它們爲你的對象的方式行事。

0

您正試圖通過xCoord,yCoord來代替Point類型的參數。相反通過

new Point(xCoord, yCoord) 

這構造了一個Point對象,該方法可以處理。

1

從參數中使用Point centre或創建一個新點像Point p = new Point(xCoord, yCoord)

相關問題