标签:
程序清单11-1
GeometricObject1.java
1 package yinchaoTest; 2 3 public class GeometricObject1{ 4 private String color = "white"; 5 private boolean filled; 6 private java.util.Date dateCreated; 7 /**Construct a default geometric object*/ 8 public GeometricObject1(){ 9 dateCreated = new java.util.Date(); 10 } 11 /**Construct a geometric object with specified color 12 * and filled value*/ 13 public GeometricObject1(String color, boolean filled){ 14 dateCreated = new java.util.Date(); 15 this.color = color; 16 this.filled = filled; 17 } 18 /**return color*/ 19 public String getColor(){ 20 return color; 21 } 22 /**set a new color*/ 23 public void setColor(String color){ 24 this.color = color; 25 } 26 /**Return filled. Since filled is boolean, 27 * its get method is named isFilled*/ 28 public boolean isFilled(){ 29 return filled; 30 } 31 /**Set a new filled*/ 32 public void setFilled(boolean filled){ 33 this.filled = filled; 34 } 35 /**Get dateCreate*/ 36 public java.util.Date getDateCreate(){ 37 return dateCreated; 38 } 39 //**Return a String representation of this object*/ 40 public String toString(){ 41 return "create on " + dateCreated + "\ncolor: " + color + 42 " and filled: " + filled; 43 } 44 }
程序清单11-2
Circle4.java
1 package yinchaoTest; 2 3 public class Circle4 extends GeometricObject1{ 4 private double radius; 5 6 public Circle4(){ 7 } 8 9 public Circle4(double radius){ 10 this.radius = radius; 11 } 12 13 public Circle4(double radius,String color,boolean filled){ 14 this.radius = radius; 15 setColor(color); 16 setFilled(filled); 17 } 18 19 /**Return radius*/ 20 public double getRadius(){ 21 return radius; 22 } 23 24 /**Set a new radius*/ 25 public void setRadius(double radius){ 26 this.radius = radius; 27 } 28 29 /**Return area*/ 30 public double getArea(){ 31 return radius*radius*Math.PI; 32 } 33 34 /**Return diameter*/ 35 public double getDiameter(){ 36 return 2 * radius; 37 } 38 39 /**Return perimeter*/ 40 public double getPerimeter(){ 41 return 2 * radius * Math.PI; 42 } 43 /**Print the circle info*/ 44 public void printCircle(){ 45 System.out.println("The circle is created" + getDateCreate() + 46 "and the radius is " + radius); 47 } 48 }
程序清单11-3
Rectangle1.java
1 package yinchaoTest; 2 3 4 public class Rectangle1 extends GeometricObject1{ 5 private double width; 6 private double height; 7 public Rectangle1(){ 8 9 } 10 11 public Rectangle1(double width, double height){ 12 this.width = width; 13 this.height = height; 14 } 15 16 public Rectangle1(double width, double height, String color, 17 boolean filled){ 18 this.width = width; 19 this.height = height; 20 setColor(color); 21 setFilled(filled); 22 } 23 24 /**Return width*/ 25 public double getWidth(){ 26 return width; 27 } 28 29 /**Set a new width*/ 30 public void setWidth(){ 31 this.width = width; 32 } 33 34 /**Return heigth*/ 35 public double getHeigth(){ 36 return height; 37 } 38 39 /**Set a new heigth*/ 40 public void setHeigth(){ 41 this.height = height; 42 } 43 44 /**Return area*/ 45 public double getArea(){ 46 return width * height; 47 } 48 49 /**Return perimeter*/ 50 public double getPerimeter(){ 51 return 2 * (width + height); 52 } 53 }
程序清单11-4
TestCircleRectangle.java
1 package yinchaoTest; 2 3 public class TestCircleRectangle { 4 public static void main(String[] args){ 5 Circle4 circle = new Circle4(1); 6 System.out.println("A circle " + circle.toString()); 7 System.out.println("The radius is " + circle.getRadius()); 8 System.out.println("The area is " + circle.getArea()); 9 System.out.println("The diameter is " + circle.getDiameter()); 10 11 Rectangle1 rectangle = new Rectangle1(2, 4); 12 System.out.println("\nA rectangle " + rectangle.toString()); 13 System.out.println("The area is " + rectangle.getArea()); 14 System.out.println("The perimeter is " + 15 rectangle.getPerimeter()); 16 } 17 }
标签:
原文地址:http://www.cnblogs.com/yin-chao/p/5118072.html