【问题标题】:I am having an issue calling a method for the area of a rectangle我在调用矩形区域的方法时遇到问题
【发布时间】: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


    【解决方案1】:

    从不调用计算面积setArea 的方法。按照你的编码方式,你可以把它放在toString

    public String toString ( ) {
       setArea(width, height);
       String x = "Rectangle width is " + width + ", height is " + height +
        ", color is " + color + " and the area is " + area;
       return x;
    } 
    

    但是您的代码不是应该如何完成的标准方式。

    【讨论】:

      【解决方案2】:

      您的 Rectangles 没有完全构造,因为方法 setArea(double width, double height) 从未被调用,所有对象的区域都保留为默认值 0.0.....您应该在构造函数中调用该方法。

      public Rectangle ( double w, double h, String co) { 
          width = w;
          height = h;
          color = (co);
          System.out.println("Overloaded constructor" );
          //overloaded constructor
         setArea (width, height);   ///here!
      } 
      

      注意: 面积可以用类成员w和h立即计算,无需定义方法

      setArea (width, height); 
      

      使用同一个类的参数,setArea()就足够了

      【讨论】:

      • 最好也更新 setWidth 和 setHeight 方法;)
      【解决方案3】:

      findArea是一个可计算的结果,这意味着它的结果是基于计算其他值的结果。根据您的代码示例,计算 area 的唯一位置是在调用 setArea 时,但从不调用 setArea。

      更好的解决方案是让findArea 自己执行计算

      public double findArea (){
          return width * height;
      }
      

      这样做的好处是,当width 或height 发生变化时,您不必记得调用setArea 方法

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-21
        • 2012-05-25
        • 2021-10-15
        • 1970-01-01
        • 2019-10-10
        • 2019-09-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多