【发布时间】:2017-08-05 11:38:52
【问题描述】:
我的 Rectangle 类上有 getter 和 setter、默认和重载构造函数、toString 和我的 area 方法。我的 RectangleTest 只是我的价值观和输出语句。我一直在尝试让我的区域方法工作,但无论我尝试什么,都没有调用我的高度和宽度。
public class Rectangle {
public double width ;
private double height;
private String color ;
private double area;
public double getWidth ( ) { return width; }
public void setWidth ( double w ) {
this.width = w; }
public double getHeight ( ) {return height; }
public void setHeight ( double h ) {
this.height = h; }
public String getColor ( ) {return color; }
public void setColor ( String co ) {
this.color = (co); }
public Rectangle ( ) {
height = 0;
width = 0;
color = (" ");
System.out.println("Default constructor");
//default constructor
}
public Rectangle ( double w, double h, String co) {
width = w;
height = h;
color = (co);
System.out.println("Overloaded constructor" );
//overloaded constructor
}
public double findArea (){
return area;
}
public void setArea (double width, double height){
area = width * height;
}
public String toString ( ) {
String x = "Rectangle width is " + width + ", height is " + height +
", color is " + color + " and the area is " + area;
return x;
}
}
public class RectangleTest {
public static void main(String[] args) {
Rectangle shape = new Rectangle(2.0, 4.0, "red");
Rectangle r2 = new Rectangle(2.0, 3.0, "Yellow");
Rectangle r3 = new Rectangle(2.0, 3.0, "Yellow");
do { r2.width++ ;
} while (r2.width == (r3.width));
if (r2.equals (r3))
System.out.println("r2 still equals r3" );
else
System.out.println("r2 is now a square");
System.out.println("First rectangle is " + shape );
System.out.println("Second rectangle is " + r2 );
if (r2.equals (r3))
System.out.println("\nShape 2 equals shape 3");
else
System.out.println("\nShape 2 and 3 are not equal");
//System.exit(0);
}
}
【问题讨论】:
标签: java class methods constructor