3

ここで助けが必要です。「クラスとオブジェクト指向開発」に取り組んでおり、教科書の質問に対するロジックとコードの両方で助けを借りることができます。

質問: Rectangle クラスの前の例を変更して、equals() および toString() メソッドをオーバーライドするように求められました。2 つの長方形は、長さと幅が同じ場合に等しくなります。

私のアプローチ:これを行うために変更しようとしましたが、幅と長さの両方で比較するよりも、面積で比較する方が簡単だと判断したので、これまでのところ以下のとおりです。役立つアイデアがあれば教えてください。円の半径を比較する equals() メソッドの前の例がありますが、2 つの異なるものを比較するときには役に立ちません。すべての前にありがとう!なぜそれらがすべて独自の個別のファイルではないのか疑問に思っている場合は、まだ章に到達していないので、見ることがたくさんあります.P

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chapter8javaExamples;

/**
 *
 * @author Eric
 */
public class Rectangle {
    private double length, width;


/**
 * constructor
 * pre: none
 * post: A rectangle class is created. 
 */
public Rectangle() {
    length = 2;   //default length
    width = 4;    //default width
}


/**
 * constructor
 * pre: none
 * post: A rectangle object is created with length and width.
 */
public Rectangle (double l, double w) {
    length = l;
    width = w;

}


/**
 * Changes the length of the rectangle
 * pre: none
 * post: Length has been changed.
 */
public void setLength (double newLength) {
    length = newLength;
}




/**
 * Changes the width of the rectangle.
 * pre: none
 * post: Width has been changed.
 */
public void setWidth (double newWidth) {
    width = newWidth;
}


/**
 * Returns the length of the rectangle.
 * pre: none
 * post: The length of the rectangle has been returned. 
 */
public double getLength() {
    return(length);
}


/**
 * Returns the width of the rectangle.
 * pre: none
 * post: The width of the rectangle has been returned.
 */
public double getWidth() {
    return(width);
}


/**
 * Returns the area of rectangle
 * pre: none
 * post: The area of the rectangle is returned
 */
public double area() {
    double triArea;

    triArea = length * width;
    return(triArea);
}


/**
 * Returns the perimeter of the rectangle
 * pre: none
 * post: The perimeter of the rectangle is returned
 */
public double perimeter() {
    double triPer;

    triPer = length + width + length + width;
    return(triPer);
}


/**
 * Displays the formula for area of a rectangle.
 * pre: none
 * post: The formula is displayed.
 */
public static void displayAreaFormula(){
    System.out.println("The formula for the area of a rectangle is a=l*w");
}

/**
 * Determines if the object is equal to another
 * Circle object.
 * pre: c is a Circle object.
 * post: true has been returned if the objects have
 * the same radii, false otherwise.
 */
public boolean equals(Object r) {
    Rectangle testObj = (Rectangle) r;
    Rectangle testObj2 = (Rectangle) r2;

    if (testObj.getArea() == area && testObj2.getArea == area()) {
        return(true);
    } else {
        return(false);
    }
}


/**
 * Returns a String that represents the Circle object.
 * pre: none
 * post: A string representing the Circle object has
 * been returned.
 */
public String toString(){
    String rectangleString;

    rectangleString = "Rectangle has the Area " + length*width;
    return(rectangleString);
}

/**
 * 
 * @param args 
 */
    public static void main(String [] args){
    Rectangle spot = new Rectangle();
    Rectangle spot2 = new Rectangle(5, 9);

    System.out.println("Area is: " + spot.area());
    System.out.println("Perimeter: " + spot.perimeter());
    Rectangle.displayAreaFormula();
}

}

4

4 に答える 4

2

equals両方の領域が16であるため、2x8の長方形は4x4の長方形に等しいため、この方法では領域を比較することは良い考えではないと思います。これは要件と矛盾します:

2 つの長方形は、長さと幅が同じ場合に等しくなります。

ここで、r2変数は未定義です。しかし、それ以上に、equalsメソッドは他の 2 つのオブジェクトを比較するべきではなく、thisオブジェクトを別のオブジェクトと比較するべきです。

オブジェクトが で、この四角形の長さが他の四角形の長さと一致し、この四角形の幅が他の四角形の幅と一致するtrue場合は、返す必要があります。rRectangle

于 2013-04-16T20:40:57.567 に答える
2

equals メソッドは常に次の構造を持つ必要があります。

public boolean equals(Object r) {
    if(r == null || !r instanceof Rectangle) return false;
    // rest of the code
}

これは、null 参照 (エラーをスローする) に対して操作を実行したくないためであり、とにかくthis等しいことはできませnullん。第 2 に、r が Rectangle クラスのインスタンスでない場合、Rectangle は String や Circle と等しくないため、他の操作を実行する前に終了できます。

さて、あなたの質問に行きましょう:幅と長さだけで等しいかどうかをチェックしたい場合、私は次のようにメソッドを書きます:

public boolean equals(Object r) {
    if(r == null || !r instanceof Rectangle) return false;
    if(length == r.length && width == w.width) return true;
    return false;
}

幅と長さの比較です。両方が等しい場合、2 つの長方形は等しいです。領域を比較していますが、これは誤検出を引き起こす可能性があります。次の例を見てください。

Rectangle r1;
Rectangle r2;

r1.width = 10;
r1.length = 5;

r2.width = 5;
r2.length = 10;

これらの長方形の向きは異なりますが、コードは正の値を生成します。

于 2013-04-16T20:44:48.507 に答える
0

2 つの長方形は、長さと幅が同じ場合に等しくなります。

これらの次の長方形は同じですか?

100x100

200x50

おい!どちらも同じ面積ですよね?

つまり、&&両方の長さと幅が等しいかどうかを確認したり.toString()、両方の長方形の s を比較して正確かどうかを確認したりする必要はありません。

于 2013-04-16T20:43:21.293 に答える
0

他の回答に追加するには(最初にそれらを読んでください)

「物理的な」オブジェクト (木のブロックなど) を比較する場合は、長さと幅、幅と長さも比較したい場合があります。ユーザーが入力した方向は、2 つの同じオブジェクトに対して異なる方法で送信される場合があります。要件を読むときは、これを考慮してください。

于 2013-04-16T20:46:29.810 に答える