java-矩形面向对象的开发-Equals()方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-矩形面向对象的开发-Equals()方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3563字,纯文字阅读大概需要6分钟。
内容图文
![java-矩形面向对象的开发-Equals()方法](/upload/InfoBanner/zyjiaocheng/677/c665ee2274844526a701a69b566872d9.jpg)
在这里需要一些帮助.从事“类和面向对象的开发”工作,可以在我的逻辑和代码中对教科书中的问题使用一些帮助.
问题:我被要求修改我前面的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();
}
}
解决方法:
我认为在equals方法中比较区域不是一个好主意,因为2×8的矩形将等于4×4的矩形,因为两个区域均为16.这与您的要求相矛盾:
Two rectangles are equal when they both have the same length and width.
在这里,您的r2变量是未定义的.但除此之外,equals方法不应比较其他两个对象,而应将此对象与另一个对象进行比较.
如果r对象是一个Rectangle并且此矩形的长度与另一个矩形的长度匹配,并且此矩形的宽度与另一个矩形的宽度匹配,则应返回true.
内容总结
以上是互联网集市为您收集整理的java-矩形面向对象的开发-Equals()方法全部内容,希望文章能够帮你解决java-矩形面向对象的开发-Equals()方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。