java矩形对象的个数_java-矩形面向对象的开发-Equals()方法

在这里需要一些帮助.从事“类和面向对象的开发”工作,可以在我的逻辑和代码中对教科书中的问题使用一些帮助.

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

我的方法:我尝试进行更改以实现此目的,然后决定按区域进行比较将比按宽度和长度进行比较容易,所以下面是到目前为止的内容.让我知道您是否有任何帮助的想法.前面有一个equals()方法的示例,该方法比较一个圆的半径,但是在比较两个不同的物体时没有帮助.谢谢大家!如果您想知道为什么它们都不是各自独立的文件,那么本章中我还没有提到,所以可以参考我知道的内容; 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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值