酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

使用java语言,编写应用程序volume,程序**有3个重载方法calvolume,每个方法的参数不同,分别计算长方体、球体、和圆柱体的体积。在主方法中,计算给出长宽高为32.5,23.2,14的长方体、半径为12的球体、和半径为5高度为20的圆柱体的体积并输出。

```java public class Volume { // 计算长方体体积 public double calVolume(double length, double width, double height) { return length * width * height; } // 计算球体体积 public double calVolume(double radius) { return 4.0 / 3 * Math.PI * Math.pow(radius, 3); } // 计算圆柱体体积 public double calVolume(double radius, double height) { return Math.PI * Math.pow(radius, 2) * height; } public static void main(String[] args) { Volume volume = new Volume(); // 计算长方体体积 double length = 32.5; double width = 23.2; double height = 14; double cuboidVolume = volume.calVolume(length, width, height); System.out.println("长方体体积: " + cuboidVolume); // 计算球体体积 double radius = 12; double sphereVolume = volume.calVolume(radius); System.out.println("球体体积: " + sphereVolume); // 计算圆柱体体积 radius = 5; height = 20; double cylinderVolume = volume.calVolume(radius, height); System.out.println("圆柱体体积: " + cylinderVolume); } } ``` 额外注意点: 1. 确保计算体积的公式准确无误。 2. 注意数据类型的精度,避免计算结果出现较大误差。 3. 检查输出的体积结果是否符合预期。 [2024-11-27 11:02:36 | AI写代码神器 | 362点数解答]

相关提问