标签:ring 变量 实验报告 system out 图片 end new null
Circle
。Circle
的成员变量:radius
表示圆的半径。Circle
的方法成员: Circle():构造方法,将半径置0
Circle(double r):构造方法,创建Circle对象时将半径初始化为r
double getRadius():获得圆的半径值
double getPerimeter():获得圆的周长
void disp():将圆的半径和圆的周长,圆的面积输出到屏幕
public class Circle {
private double radius;
public Circle(double r) {
radius = r;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}
public static void main(String[] args) {
Circle c = new Circle(4);
System.out.println("周长:" + c.getPerimeter());
System.out.println("半径:" + c.getRadius());
System.out.println("面积:" + c.getArea());
}
}
Circle
类,派生圆柱体类Cylinder
。要求如下:Cylinder
的成员变量:height
表示圆柱体的高。Cylinder
的成员方法: Cylinder(double r,double h)构造方法,创建Cylinder对象时将圆半径初始化为r,圆柱体高初始化为h。
double getHeight():获得圆柱体的高
double getVol():获得圆柱体的体积
void dispVol():将圆柱体的体积输出到屏幕
Cylinder
类,接上键盘的输入值作为圆的半径和圆柱体的高,计算圆柱体的体积public class Cylinder extends Circle {
private double height;
public Cylinder(double h, double r) {
super(r);
this.setHeight(height);
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getVol() {
return super.getArea() * height;
}
public static void main(String[] args) {
Cylinder cy = null;
cy = new Cylinder(0, 0);
cy.setRadius(7.0);
cy.height = 8.0;
System.out.println("半径:" + cy.getRadius());
System.out.println("高:" + cy.getHeight());
System.out.println("体积:" + cy.getVol());
}
}
标签:ring 变量 实验报告 system out 图片 end new null
原文地址:https://www.cnblogs.com/hhl296738954/p/11626705.html