1  public class Rectangle {
  2  
  3    // Instance variables:
  4    protected double width, height;
  5    
  6    // Constructors:
  7    public Rectangle(double width, double height) {
  8      setWidth(width); setHeight(height);
  9    }
 10    public Rectangle(Dimension d) {
 11      setDimension(d);
 12    } 
 13    public Rectangle() { }  
 14    
 15    // Encapsulate the instance variables:
 16    public void setWidth(double width) {
 17      this.width = width;
 18    }
 19    public void setHeight(double height) {
 20      this.height = height;
 21    }
 22    public void setDimension(double width, double height) {
 23      setWidth(width); setHeight(height);
 24    }
 25    public void setDimension(Dimension d) {
 26      setWidth(d.width); setHeight(d.height);
 27    }
 28    public double getWidth() {
 29      return width;
 30    }
 31    public double getHeight() {
 32      return height;
 33    }
 34    public Dimension getDimension() {
 35      return new Dimension(width, height);
 36    }
 37      
 38    // Instance methods:
 39    public double perimeter() {
 40      return 2*(width + height);
 41    }
 42    public double area() {
 43      return width*height;
 44    }
 45    
 46  }