2016-01-27 26 views
-1

我有下面的類來寫: 寫了一個名爲XYRectangle_LastName,其中名字被替換爲您的姓氏類。該XYRectangle_LastName類應具有以下字段:寫一個XYRectangle類在Java中

的名爲TopLeft的XYPoint。這存儲了Rectangle的頂部角落的位置。

一個名爲Length的雙。這存儲矩形的長度。

一個名爲Width的雙。這存儲矩形的寬度。

的XYRectangle類應該有以下方法:

一個無參數的構造函數,隨機地確定的矩形的左上角。 x和y的值應該在-10和10之間。此外,它爲值爲5和10之間的矩形選擇隨機寬度和長度。

3參數構造函數,它爲XY左上角,長度和寬度。

爲一個Get方法的長度,寬度,左上,topRight,BOTTOMLEFT和bottomRight

對於長度,寬度的一組方法,和左上

名爲isInside的布爾方法,其採用的Xypoint,並且確定是否它在這個矩形裏面。

一個名爲reflectX的方法,它返回一個已反映在x軸上的矩形。

一種名爲reflectY的方法,用於返回已在y軸上反射的矩形。

這是我到目前爲止的代碼:

public class XYRectangle { 

    private XYPoint topLeft; 
    private double length; 
    private double width; 

    public XYRectangle() { 
     Random rnd = new Random(); 
     int x = (rnd.nextInt(21) - 10); 
     int y = (rnd.nextInt(21) -10); 

     XYPoint topLeft = new XYPoint(x, y); 

     int width = (rnd.nextInt(5) + 5); 
     int height = (rnd.nextInt(5) + 5); 
    } 

    public XYRectangle(XYPoint topLeft, double length, double width) { 
     this.topLeft = topLeft; 
     this.length = length; 
     this.width = width; 
    } 

    public double getLength() { return this.length; } 

    public void setLength(double length) { this.length = length; } 

    public double getWidth() { return this.width; } 

    public void setWidth(double width) { this.width = width; } 

    public XYPoint getTopLeft() { return this.topLeft; } 

    public void setTopLeft(XYPoint topLeft) { this.topLeft = topLeft; } 
我在與topRight,BOTTOMLEFT和bottomRight麻煩了方法和反映方法

。我甚至不確定到目前爲止我寫的代碼是否寫入。任何人都可以幫助並告訴我如何繼續,如果我一直在做錯什麼?

+0

嗯,這聽起來很瘋狂我知道,但是...那麼你如何試着編譯你的代碼並逐步查看它是否有效? – scottb

+0

那種愚蠢的我不去嘗試明顯的。謝謝! – ch1maera

回答

1

你沒有關於topRight,BOTTOMLEFT和bottomRight,但具有的左上角,寬度,長度的信息,它完全定義了其他幾點:

topRight = new XYPoint(topLeft.getX() + length, topLeft.getY()); 
bottomRight = new XYPoint(topLeft.getX() + length, topLeft.getY() + width); 
bottomLeft = new XYPoint(topLeft.getX(), topLeft.getY() + width); 

你可以決定存儲這個當你構造你的對象或每次調用get方法時計算它的信息。

關於空的構造函數時,稱這是「角」時,它應該被稱爲:

public XYRectangle(){ 
    //code here 
} 

通常,當我們覆蓋的構造,我們稱之爲基本構造是這樣的:

public XYRectangle(){ 
    Random rnd = new Random(); 
    int x = (rnd.nextInt(21) - 10); 
    int y = (rnd.nextInt(21) -10); 

    XYPoint topLeft = new XYPoint(x, y); 

    int width = (rnd.nextInt(5) + 5); 
    int height = (rnd.nextInt(5) + 5); 
    this(topLeft, width, height) 
} 

我希望你能自己弄清楚反射方法。 ;)

+0

非常感謝!這絕對讓它更容易。 – ch1maera