1 public class Dimension extends Object { 2 3 // Instance variables: 4 protected double width, height; 5 6 // Constructors: 7 public Dimension(double width, double height) { 8 setWidth(width); setHeight(height); 9 } 10 public Dimension(Dimension d) { 11 setWidth(d.width); setHeight(d.height); 12 } 13 public Dimension() { } 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 String toString() { 40 return super.toString(); // to be overridden 41 } 42 43 }