《Java语言程序设计基础篇》第8版第11章代码

程序清单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 }

 

转载于:https://www.cnblogs.com/yin-chao/p/5118072.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值