我的代码运行得非常好,但是对于对象,我希望用户自己做,而不是放入变量。但我不知道该怎么做。
在演示/驱动程序中演示该类,该程序要求用户输入两片土地的尺寸。程序应该演示 toString 方法来显示每块土地的尺寸和面积,并使用 equals 方法来指示块大小是否相等,即,块的面积是否相同。
对象类:
public class MitchellLandTract
{
private int length;
private int width;
private int area;
/**
* Constructor
* @param l length
* @param w width
*/
public MitchellLandTract(int l, int w)
{
length = l;
width = w;
}
/**
* toString for the land tract
* @param str the string describing the object
*/
public String toString()
{
// Create a string describing the land tract.
String str = "With the width, " + width +
", and the length, " + length +
". The area is " + area;
// Return the string.
return str;
}
/**
* getLength method
* @return area of land tract
*/
public double getArea()
{
area = length * width;
return area;
}
/**
* equals method
* @param object2 the object being compared to original
* @return if the areas are the same or different
*/
public boolean equals(MitchellLandTract object2)
{
boolean status;
// Determine whether this object's area field
// is equal to object2's area field
if (getArea()==object2.getArea())
status = true; // Yes, the objects are equal.
else
status = false; // No, the objects are not equal.
// Return the value in status.
return status;
}
相关分类