package thirdjava;
import java.util.Scanner;
class Cir{
public double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
return radius*radius*Math.PI;
}
}
class Cyl extends Cir{
public double height;
public Cyl() {};//定义默认构造方法
public Cyl(double radius,double height) {//定义带radius和height两个参数的构造方法
this.height=height;
this.radius=radius;
}
public double getArea() {
return 2*Math.PI*radius+2*radius*Math.PI*height;
}
public double getVolume() {
return radius*radius*Math.PI*height;
}
}
public class one {
private static Scanner inScanner;
public static void main(String[] args) {
double CylinderRadius,CylinderHeight1;
System.out.println("请依次输入圆柱体的半径和高:");
inScanner = new Scanner(System.in);
CylinderRadius=inScanner.nextDouble();
CylinderHeight1=inScanner.nextDouble();
Cyl cy=new Cyl(CylinderRadius,CylinderHeight1);
System.out.printf("圆柱体的表面积为:"+"%.2f%n",cy.getArea());
System.out.printf("圆柱体的体积为:"+"%.2f%n",cy.getVolume());
}
}
(1)定义一个名为Cylinder类表示圆柱,它继承Circle类(参见实验三第3小题),要求定义一个变量height表示圆柱高度。覆盖getArea()方法求圆柱的表面积,定义getVolume()
最新推荐文章于 2022-05-26 14:56:46 发布