(1)定义一个圆类Circle,成员变量:半径 radius;成员方法:构造方法、get和set半径的方法、 计算面积和周长的方法。定义圆柱和圆锥类,定义相应的变量成员和成员方法。使用以上类编 程,输出圆的面积和圆柱、圆锥的体积。
class Circle{
double r;
double PI = 3.14;
double h ;
public Circle(double r,double h){
this.r = r;
this.h = h;
}
public double getR()
{
return r;
}
public void setR(double r)
{
this.r = r;
}
public double getyuanS(){
return PI*r*r;
}
public double getzhoucheng(){
return 2*PI*r;
}
public double yuanzhuV(){
return PI*r*r*h;
}
public double yuanzhuiV(){
return (PI*r*r*h)/3;
}
}
public class shiyan1 {
public static void main(String[] args){
Circle yuan = new Circle(1,2);
System.out.println("半径为"+yuan.getR());
System.out.println("圆面积"+yuan.getyuanS());
System.out.println("圆柱V"+yuan.yuanzhuV());
System.out.println("圆锥V"+yuan.yuanzhuiV());
yuan.setR(2);
System.out.println("半径为"+yuan.getR());
System.out.println("圆面积"+yuan.getyuanS());
System.out.println("圆柱V"+yuan.yuanzhuV());
System.out.println("圆锥V"+yuan.yuanzhuiV());
}
}