JAVA:定义一个圆类Circle,成员变量:半径 radius;
JAVA:定义一个圆类Circle,成员变量:半径radius;成员方法:构造方法、get和set半径的方法、计算面积和周长的方法。定义圆柱和圆锥类,定义相应的变量成员和...
JAVA:定义一个圆类Circle,成员变量:半径 radius;成员方法:构造方法、get和set半径的方法、计算面积和周长的方法。
定义圆柱和圆锥类,定义相应的变量成员和成员方法。
使用以上类编程,输出圆柱和圆锥面积和体积。 展开
定义圆柱和圆锥类,定义相应的变量成员和成员方法。
使用以上类编程,输出圆柱和圆锥面积和体积。 展开
2018-06-08
展开全部
纯手写代码,望采纳,谢谢!
public class Main{
public static void main(String[] ags) {
double radius = 2.5;
double width = radius * 2;
double height = 10;
Circle circle = new Circle(radius);
System.out.println("圆形周长:" + circle.calculgirth());
System.out.println("圆形面积:" + circle.calculSquare());
Cylinder cylinder = new Cylinder(width, height);
System.out.println("圆柱体侧面积:" + cylinder.calculSideArea());
System.out.println("圆柱体面积:" + cylinder.calculSurfaceArea());
System.out.println("圆柱体体积:" + cylinder.calculVolume());
Vertebral vertebral = new Vertebral(width, height);
System.out.println("圆锥体体侧面积:" + vertebral.calculSideArea());
System.out.println("圆锥体面积:" + vertebral.calculSurfaceArea());
System.out.println("圆锥体体积:" + vertebral.calculVolume());
}
}
public class Circle {
private double radius;
private double pai = 3.14;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public Circle(double radius) {
this.radius = radius;
}
public Circle() {
}
/**
* 圆形周长
* 2πr
* 圆形周长 = 2 x π x 半径
*
* @return
*/
public double calculgirth() {
return 2 * pai * radius;
}
/**
* 圆形面积
* πr²
* 圆形面积=π x 半径的平方
*
* @return
*/
public double calculSquare() {
return pai * (radius * radius);
}
}
public class Cylinder {
private double width;
private double height;
private Circle circle;
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
//设置圆柱体宽度时并设置圆的半径
circle = new Circle(width / 2);
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public Cylinder(double width, double height) {
this.width = width;
this.height = height;
//设置圆柱体宽度时并设置圆的半径
circle = new Circle(width / 2);
}
public Cylinder() {
}
/**
* 计算侧面积
* 圆柱体侧面积=底面周长x高
*
* @return
*/
public double calculSideArea() {
return circle.calculgirth() * height;
}
/**
* 计算表面积
* 圆柱体表面积 = 侧面积+底面积x2
*
* @return
*/
public double calculSurfaceArea() {
return (calculSideArea() + circle.calculSquare()) * 2;
}
/**
* 计算体积 圆柱体积=π r² h=s底 h
* 表面积 = 底面积 x 高
*
* @return
*/
public double calculVolume() {
return circle.calculSquare() * height;
}
}
/**
* ①。侧面积=πRL
* ②。全面积=πR(L+R)
* ③。体积=πR²H/3
* ④。侧母线长L=√(R²+H²)
*/
public class Vertebral {
private double width;
private double height;
private Circle circle;
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
circle = new Circle(width / 2);
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public Vertebral(double width, double height) {
this.width = width;
this.height = height;
circle = new Circle(width / 2);
}
public Vertebral() {
}
/**
* 圆锥侧母线长L
* 圆锥侧母线长 = 开方(底面半径x底面半径+高度x高度)
*
* @return
*/
public double calculSideBusLine() {
return Math.sqrt(circle.radius * circle.radius + height * height);
}
/**
* 圆锥体侧面积
* 圆锥体侧面积 = π x R x L
* 圆锥体侧面积 = π x 底面半径 x 侧母线长
*
* @return
*/
public double calculSideArea() {
return circle.pai * circle.radius * calculSideBusLine();
}
/**
* 圆锥表面积
* 圆锥表面积 = πR(L+R)
* 圆锥表面积 = π x 底面半径 x (侧母线长 + 底边半径)
*/
public double calculSurfaceArea() {
return circle.pai * circle.radius * (calculSideBusLine() + circle.radius);
}
/**
* 圆锥体积
* 圆锥体积=πR²H/3
*/
public double calculVolume() {
return circle.pai * (circle.radius * circle.radius) * height / 3;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询