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

📄 rectangle.java

📁 Beginning Java 2, SDK 1.4 Edition Exercise Code samples for this book
💻 JAVA
字号:
// Chapter 6 Exercises 1 & 2

public class Rectangle extends Shape {
  // Bottom right of rectangle is defined relative to the reference point, position:
  private Point bottomRight;        // Bottom right of rectangle. 

// Constructors:
  public Rectangle(Point startDiag, Point endDiag) {
    // Position of rectangle is top left corner - minimum x and y:
    position = new Point(Math.min(startDiag.x,endDiag.x),Math.min(startDiag.y,endDiag.y));

    // Bottom right is relative to top left:
    bottomRight = new Point(Math.max(startDiag.x,endDiag.x) - position.x,
                            Math.max(startDiag.y,endDiag.y) - position.y);
  }

  // Overrides the method inherited from Object:
  public String toString()  { 
    // Return a string representation of the object:
    return "Rectangle: Top Left: " + position + " Bottom Right: " + 
                                                     position.add(bottomRight);
  }

  // Display the rectangle:  
  public void show() {				
    System.out.println("\n" + toString());
  }
}

⌨️ 快捷键说明

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