2012-02-09 27 views
0

我需要創建一個方法largerThan(見下文),它將一個Rectangle對象作爲參數,如果調用對象的面積大於對象的面積參數,否則返回false。我以前做過這個,但根本無法回想如何在這部分方法中完成代碼。任何幫助將真正感激!注意:教授不希望我們使用「這個」操作符! :-(如何編寫簡單的布爾方法? (不記得如何編碼下面的代碼)

public class Rectangle 
{ 

    private double length; 

    private double width; 

    public Rectangle() 
    { 
     length = 0; 
     width = 0; 
    } 
    public Rectangle(double l, double w) 
    { 
     length = l; 
     width = w; 
    } 
    public void setRectangle(double l, double w) 
    { 
     length = l; 
     width = w; 
    } 
    public double getLength() 
    { 
     return length; 
    } 
    public double getWidth() 
    { 
     return width; 
    } 
    public double perimeter() 
    { 
     return length + width; 
    } 
    public double Area() 
    { 
     return length*width; 
    } 
    **public boolean largerThan(Rectangle r1) 
    { 
     if() 
     return True; 
     else 
     return False; 
    }** 
    public String toString() 
    { 
     return "Length is " + length + " width is " + width; 
    } 
} 
+0

所以......讓我們摸不着頭腦逐步 - 你如何計算面積? – 2012-02-09 03:27:56

+1

這裏是富勒代碼'public boolean largerThan(Rectangle that){return this.getArea()> that.getArea();}' – Nishant 2012-02-09 03:30:47

回答

3
public boolean largerThan(Rectangle otherRec){ 
    return this.Area() > otherRec.Area(); 
} 
+0

Tysm!但我忘了提及我的教授不希望我們使用「這個」! :-( – junaidkaps 2012-02-09 03:33:48

+1

「this」是可選的,你可以把關鍵字留出。:P – Timeout 2012-02-09 03:37:05

+1

嗯..我知道那..是嗎?也許?Hehehehe ...謝謝一堆!:-) – junaidkaps 2012-02-09 03:43:13

1

你可以這樣說:

public boolean largerThan(Rectangle r1){ 
    return this.Area() > r1.Area(); 
} 
+0

Tysm!但我忘了提及我的教授不希望我們使用「這個」! :-( - – junaidkaps 2012-02-09 03:36:41

+1

只是放棄了這個,'return Area()> r1.Area();' – 2012-02-09 03:41:42

1

你的骨架是基本上沒有,現在你想要做什麼拿的英文單詞:

如果調用對象的面積大於作爲參數的對象的面積,則返回true,否則返回false

,並把它的代碼:

public boolean largerThan(Rectangle r1) 
{ 
    if(this.Area() > r1.Area()) 
    return True; 
    else 
    return False; 
} 
+0

Tysm!但我忘了提及我的教授不希望我們使用「this」!:-( - – junaidkaps 2012-02-09 03:36:31

+1

@ junaidkaps:那麼不要爲了天哪而使用「this」! – 2012-02-09 03:41:47