⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rectangle.java

📁 广告歌 广告歌 广告歌 广告歌
💻 JAVA
字号:
// Rectangle.java: The Rectangle class that extends GeometricObject
public class Rectangle extends GeometricObject
{
  protected double width;
  protected double height;

  // Default constructor
  public Rectangle()
  {
    this(1.0, 1.0, "white", 1.0);
  }

  // Construct a rectangle with specified width and height
  public Rectangle(double width, double height)
  {
    this.width = width;
    this.height = height;
  }

  // Construct a rectangle with specified width, height, weight, and color
  public Rectangle(double width, double height,
    String color, double weight)
  {
    super(color, weight);
    this.width = width;
    this.height = height;
  }

  // Getter method for width
  public double getWidth()
  {
    return width;
  }

  // Setter method for width
  public void setWidth(double width)
  {
    this.width = width;
  }

  // Getter method for height
  public double getHeight()
  {
    return height;
  }

  // Setter method for height
  public void getHeight(double height)
  {
    this.height = height;
  }

  // Implement the findArea method in GeometricObject
  public double findArea()
  {
    return width*height;
  }

  // Implement the findPerimeter method in GeometricObject
  public double findPerimeter()
  {
    return 2*(width + height);
  }

  // Override the equals() method defined in the Object class
  public boolean equals(Rectangle rectangle)
  {
    return (width == rectangle.getWidth()) &&
      (height ==  rectangle.getHeight());
  }

  // Override the toString() method defined in the Object class
  public String toString()
  {
    return "[Rectangle] width = " + width + " and height = " + height;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -