每日一题 day 03

12.3每日一题

今天又是深化面向对象的一天~

题目简介:

建立一个Po(点)类,包含数据成员X,Y(坐标点),构造器;

以Po为父类,实现一个Circle(圆)类作为子类,增加数据成员R(半径),构造器、求圆面积方法getArea(),求圆周长方法getCircumference();

再以Cicle类为父类,实现出一个Cylinder (圆柱体〕子类,增加数据成员H(高),构造器,求圆柱体积方法getVolumn()、求圆柱表面积方法getArea(),请编写程序实现。(圆周率取3.14)

要求:

自定义各个类的toString方法,要求当上面三个类的对象创建完毕后,我们直接打印这个三个对象可以显示这个对象的完整信息。

如打印Circle类的对象会输出:

Circle{X=1,Y=2,R=1,circumference=6.28,area=3.14}

友情提示X,Y,R应均为double类型;

package com.atamy.onequestionaday.day3;

public class Po {

    double X,Y;
    public  double PI = 3.14;

    public Po() {
    }

    public Po(double x, double y) {
        X = x;
        Y = y;
    }

    public String toString(){
        return "P0{" + "X=" + X + ",Y=" +Y + "}";
    }
}
package com.atamy.onequestionaday.day3;

public class Circle extends Po {
    double R;


    public Circle(double x, double y, double r) {
        super(x, y);
        R = r;
    }

    public double getArea(){
        return PI*R*R;
    }

    public double getCircumference(){
        return 2*PI*R;
    }

    public String toString(){
        return "Circle{" + "X=" + X +",Y=" + Y+ ",R="+R + ",area=" + getArea() +",circumference=" +getCircumference()+"}";
    }
}
package com.atamy.onequestionaday.day3;

public class Cylinder extends Circle{
    double H;

    public Cylinder(double x, double y, double r, double h) {
        super(x, y, r);
        this.H = h;
    }

    public double getVolumn(){
        return super.getArea()*this.H;
    }

    public double getArea(){
        return 2*super.getArea() + super.getCircumference()* this.H;
    }
    public String toString(){
        return "Cylinder{" + "X=" + X +",Y=" + Y+ ",R="+R + ",H=" + H +",area=" + getArea() +",volumn=" +getVolumn()+"}";
    }
}
package com.atamy.onequestionaday.day3;

public class Test {
    public static void main(String[] args) {
        Po po = new Po(1,1);
        System.out.println(po.toString());

        Circle circle = new Circle(1,1,1);
        System.out.println(circle.toString());

        Cylinder cylinder = new Cylinder(1,1,1,1);
        System.out.println(cylinder.toString());
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值