3

在这里需要一些帮助。致力于“类和面向对象的开发”,并且可以在我的逻辑和代码方面为教科书中的问题提供一些帮助。

问题:我被要求修改我之前的 Rectangle 类示例以覆盖 equals() 和 toString() 方法。当两个矩形都具有相同的长度和宽度时,它们是相等的。

我的方法:我试图改变它来做到这一点,然后决定按区域比较会更容易,而不是按宽度和长度进行比较,所以下面是我到目前为止的内容。让我知道您是否有任何想法可以提供帮助。前面有一个 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 方法应始终具有以下结构:

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

这是因为您不想对空引用执行操作(这会引发错误),并且无论如何this都不能等于。null其次:如果 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;
}

这比较了宽度和长度。如果它们都相等,则两个矩形相等。您正在比较该区域,但这可能会产生误报。举个例子:

Rectangle r1;
Rectangle r2;

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

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

您的代码会产生积极的影响,而这些矩形的方向不同。

于 2013-04-16T20:44:48.507 回答
2

我不认为在该equals方法中比较区域是一个好主意,因为 2x8 的矩形将等于 4x4 的矩形,因为两个区域都是 16。这与您的要求相矛盾:

当两个矩形都具有相同的长度和宽度时,它们是相等的。

在这里,您的r2变量未定义。但除此之外,该equals方法不应该比较其他两个对象,它应该将this对象与另一个对象进行比较。

true如果r对象是 aRectangle并且此矩形的长度与另一个矩形的长度匹配并且此矩形的宽度与另一个矩形的宽度匹配,则您应该返回。

于 2013-04-16T20:40:57.567 回答
0

当两个矩形的长度和宽度都相同时,它们相等。

以下这些矩形是否相同?

100x100

200x50

嘿!两者的面积是一样的吧?

所以简而言之,不,您可以&&查看两者的长度和宽度是否相等,或者您可以比较.toString()两个矩形的 s 以查看它们是否准确。

于 2013-04-16T20:43:21.293 回答
0

添加到其他答案(先阅读那些)

如果您正在比较“物理”对象(例如一块木头),那么您可能还想比较长度与宽度以及宽度与长度。对于两个相同的对象,用户输入的方向可能会有所不同。在阅读您的要求时考虑这一点。

于 2013-04-16T20:46:29.810 回答