急!求程序!JAVA设计圆柱体的类,计算其表面积和体积
答案:1 悬赏:50 手机版
解决时间 2021-12-23 06:18
- 提问者网友:遁入空寂
- 2021-12-22 12:17
用JAVA设计圆柱体的类,计算其表面积和体积,然后生成环柱体的类(要从圆柱体的类中继承),利用覆盖方法生成新方法计算环柱体的表面积和体积。求相应的程序代码。
最佳答案
- 五星知识达人网友:佘樂
- 2021-12-22 12:56
public class Cylinder {//圆柱体类
private double radius;
private double height;
public Cylinder(){}//无参构造
public Cylinder(double radius,double height){
this.radius = radius;
this.height = height;
}
public double getPerimeter(){//得到底圆周长
return 2 * Math.PI * this.radius;
}
public double getBottomArea(){//得到底面积
return Math.PI * this.radius * this.radius ;
}
public double getVolume(){//得到体积
return this.getBottomArea() * this.height;
}
public double getArea(){//得到表面积
return this.getBottomArea()*2 + this.getPerimeter() * this.height;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
public class RingCylinder extends Cylinder {
private double outerRadius;//外半径
private double interRadius;//内半径
private double height;
public RingCylinder(){}
public RingCylinder(double outerRadius,double interRadius,double height){
this.outerRadius = outerRadius;
this.interRadius = interRadius;
this.height = height;
}
@Override
public double getPerimeter() {//得到内圆周长和外圆周长的和
return 2 * Math.PI * (this.outerRadius + this.interRadius);
}
@Override
public double getBottomArea() {//得到底部面积
return Math.PI * (this.outerRadius - this.interRadius) * (this.outerRadius - this.interRadius);
}
@Override
public double getVolume() {//得到体积
return this.getBottomArea() * this.height;
}
@Override
public double getArea() {
return this.getBottomArea()*2 + this.getPerimeter()*this.height;
}
public double getOuterRadius() {
return outerRadius;
}
public void setOuterRadius(double outerRadius) {
this.outerRadius = outerRadius;
}
public double getInterRadius() {
return interRadius;
}
public void setInterRadius(double interRadius) {
this.interRadius = interRadius;
}
public double getHeight() {
return this.height;
}
public void setHeight(double height) {
this.height = height;
}
}
private double radius;
private double height;
public Cylinder(){}//无参构造
public Cylinder(double radius,double height){
this.radius = radius;
this.height = height;
}
public double getPerimeter(){//得到底圆周长
return 2 * Math.PI * this.radius;
}
public double getBottomArea(){//得到底面积
return Math.PI * this.radius * this.radius ;
}
public double getVolume(){//得到体积
return this.getBottomArea() * this.height;
}
public double getArea(){//得到表面积
return this.getBottomArea()*2 + this.getPerimeter() * this.height;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
public class RingCylinder extends Cylinder {
private double outerRadius;//外半径
private double interRadius;//内半径
private double height;
public RingCylinder(){}
public RingCylinder(double outerRadius,double interRadius,double height){
this.outerRadius = outerRadius;
this.interRadius = interRadius;
this.height = height;
}
@Override
public double getPerimeter() {//得到内圆周长和外圆周长的和
return 2 * Math.PI * (this.outerRadius + this.interRadius);
}
@Override
public double getBottomArea() {//得到底部面积
return Math.PI * (this.outerRadius - this.interRadius) * (this.outerRadius - this.interRadius);
}
@Override
public double getVolume() {//得到体积
return this.getBottomArea() * this.height;
}
@Override
public double getArea() {
return this.getBottomArea()*2 + this.getPerimeter()*this.height;
}
public double getOuterRadius() {
return outerRadius;
}
public void setOuterRadius(double outerRadius) {
this.outerRadius = outerRadius;
}
public double getInterRadius() {
return interRadius;
}
public void setInterRadius(double interRadius) {
this.interRadius = interRadius;
}
public double getHeight() {
return this.height;
}
public void setHeight(double height) {
this.height = height;
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯