圆类---父类
public class yuan2 {//圆类--父类
private double r;//圆的半径
public yuan2() {
}
public yuan2(double r) {
this.r=r;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public double zc() {//圆的周长
double zc=2*Math.PI*r;
return zc;
}
public double mj() {//圆的面积和体积
double mj=Math.PI*(r*r);
return mj;
}
public void disp() {
System.out.println("半径"+r);
System.out.println("半径为"+this.r+"的圆的周长是"+zc());
System.out.println("半径为"+this.r+"的圆的面积是"+mj());
}
}
圆锥体--子类
public class yuanzhui extends yuan2{//圆锥体
private double h;//圆锥的高
public yuanzhui() {
}
public yuanzhui(double r,double h) {
super(r);
this.h=h;
}
public double getH() {
return h;
}
public void setH(double h) {
this.h = h;
}
public double ce() {//侧面积
double ce=this.zc()*h;
return ce;
}
public double dmj() {//底面积
double dmj=Math.PI*(getR()*getR());
return dmj;
}
public double bmj() {//表面积
double bmj=Math.PI*getR()*(getR()+getR());
return bmj;
}
public void disp() {
System.out.println("母线"+getR());
System.out.println("圆锥的侧面积"+ce());
System.out.println("圆锥的底面积"+dmj());
System.out.println("圆锥的表面积"+bmj());
}
}
测试类
public class yztest {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner input=new Scanner(System.in);
System.out.println("圆的边长:\n圆锥的高:");
double r=input.nextDouble();
double h=input.nextDouble();
yuan2 yaun2=new yuan2(r);
yaun2.disp();
yuanzhui yzYuanzhui=new yuanzhui(r,h);
yzYuanzhui.disp();
}
}
测试结果
圆的边长:
圆锥的高:
2
1
半径2.0
半径为2.0的圆的周长是12.566370614359172
半径为2.0的圆的面积是12.566370614359172
母线2.0
圆锥的侧面积12.566370614359172
圆锥的底面积12.566370614359172
圆锥的表面积25.132741228718345