按要求编写Java程序---圆柱体计算

1. 题目描述

(1)定义接口A,里面包含值为3.14的常量PI和抽象方法double area()。
(2)定义接口B,里面包含抽象方法void setColor(String c)。
(3)定义接口C,该接口继承了接口A和B,里面包含抽象方法void volume()。
(4)定义圆柱体类Cylinder实现接口C,该类中包含三个成员变量:底圆半径radius、
圆柱体的高height、颜色color。
(5)创建主类来测试类Cylinder。

2. 代码实现

//(1)定义接口A,里面包含值为3.14的常量PI和抽象方法double area()。 
interface A{
    double PI = 3.14;
    double area();
}
//(2)定义接口B,里面包含抽象方法void setColor(String c)。 
interface B{
    void setColor(String c);
}
//(3)定义接口C,该接口继承了接口A和B,里面包含抽象方法void volume()。 
interface C extends A, B{
    void volume();
}
//(4)定义圆柱体类Cylinder实现接口C,该类中包含三个成员变量:
//底圆半径radius、 圆柱体的高height、颜色color。
class Cylinder implements C{
    private int radius; // 底圆半径
    private int height; // 高
    private String color;   // 颜色

    // 构造函数
    public Cylinder(int radius, int height, String color)
    {
        this.radius = radius;
        this.height = height;
        this.color = color;
    }
    // 圆柱体的表面积
    public double area()
    {
        return (PI * radius * radius * 2) + (2 * PI * radius * height);
    }
    // 圆柱体表面颜色
    public void setColor(String c)
    {
        this.color = c;
        // 下面一句话知乎是为了测试使用
        System.out.println(this.color);
    }
    // 圆柱体体积
    public void volume()
    {
        System.out.println(PI * radius * radius * height);
    }
}
//(5)创建主类来测试类Cylinder。
public class Test03{
    public static void main(String[] args){
        Cylinder cl = new Cylinder(2, 2, "红色");
        System.out.println(cl.area());
        cl.setColor("绿色");
        cl.volume();
    }
}

运行结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/meng_lemon/article/details/83820911