Java继承的两道实验题目

设计一个表示二维平面上点的类Point,包含有表示坐标位置的Protect类型的成员变量

获取和设置x和y值的public方法

package classwork_6;

public class Point {
    protected double x, y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public Point() {

    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

}

设计一个表示二维平面上的圆的类Circle,它继承父类Point,还包含有表示圆的半径的

protected类型的成员变量r,获取和设置r值的public方法和计算圆面积的public方法

 1 package classwork_6;
 2 
 3 public class Circle extends Point {
 4     protected double r;
 5 
 6     public Circle(double x, double y, double r) {
 7         super(x, y);
 8         this.r = r;
 9     }
10 
11     public Circle() {
12         super();
13     }
14 
15     public double getR() {
16         return r;
17     }
18 
19     public void setR(double r) {
20         this.r = r;
21     }
22 
23     public double S() {
24         return Math.PI * r * r;
25     }
26 
27 }
28 package classwork_6;
29 
30 public class Cylinder extends Circle {
31     protected double h;
32 
33     public Cylinder(double x, double y, double r, double h) {
34         super(x, y, r);
35         this.h = h;
36     }
37 
38     public double V() {
39         Circle a = new Circle(x, y, r);
40         return a.S() * h;
41     }
42 
43     @Override
44     public String toString() {
45         return "Cylinder [h=" + h + ", r=" + r + ", x=" + x + ", y=" + y + ", V()=" + V() + "]";
46     }
47 
48 }
49 package classwork_6;
50 
51 public class Test_pcc {
52 
53     public static void main(String[] args) {
54         Cylinder a = new Cylinder(0, 0, 1, 2);
55         System.out.println(a.toString());
56     }
57 
58 }

定义一个矩形类,该矩形具有左上角的坐标(x,y),长度,宽度属性

并具有计算面积,周长,显示矩形信息的方法

 1 package classwork_6;
 2 
 3 public class Jx {
 4       protected double x;
 5       protected double y;
 6       protected double l;
 7       protected double w;
 8       
 9       public Jx(double x, double y, double l, double w) {
10         this.x = x;
11         this.y = y;
12         this.l = l;
13         this.w = w;
14     }
15       
16     public Jx() {
17     }
18     
19     
20     public double getL() {
21         return l;
22     }
23 
24     public void setL(double l) {
25         this.l = l;
26     }
27 
28     public double getW() {
29         return w;
30     }
31 
32     public void setW(double w) {
33         this.w = w;
34     }
35 
36     public double S() {
37           return l*w;
38       }
39       public double C() {
40           return (l+w)*2;
41       }
42 
43     @Override
44     public String toString() {
45         return "Jx [x=" + x + ", y=" + y + ", l=" + l + ", w=" + w + ", S()=" + S() + ", C()=" + C() + "]";
46     }
47       
48       
49 }
50 package classwork_6;
51 
52 public class Jxlft extends Jx {
53     private double h;
54 
55     public Jxlft(double x, double y, double l, double w, double h) {
56         super(x, y, l, w);
57         this.h = h;
58     }
59 
60     public Jxlft() {
61         super();
62     }
63 
64     public Jxlft(double x, double y, double l, double w) {
65         super(x, y, l, w);
66     }
67     public double V() {
68         Jx a =new Jx(x, y, l, w);
69         return a.S()*h;
70     }
71     public double BS() {
72         Jx a =new Jx();
73         return (a.S()+l*h+w*h)*2;
74     }
75 
76     @Override
77     public String toString() {
78         return "Jxlft [h=" + h + ", x=" + x + ", y=" + y + ", l=" + l + ", w=" + w + ", V()=" + V() + ", BS()=" + BS()
79                 + "]";
80     }
81     
82 }
83 package classwork_6;
84 
85 public class Test_jx {
86 
87     public static void main(String[] args) {
88         Jxlft a=new Jxlft(0, 0, 1, 2, 2);
89         System.out.println(a.toString());
90     }
91 
92 }

 

posted @ 2020-11-21 00:05  丁帅帅dss  阅读(652)  评论(0)    收藏  举报