1.编写程序,创建类 MyMath,计算圆的周长和面积以及球的表面积和体积,并编写测试代码,结果均保留两位小数。程序运行效果参见图。
时间: 2024-02-16 15:04:07 浏览: 36
好的,以下是 MyMath 类的实现代码和测试代码:
```python
import math
class MyMath:
@staticmethod
def circle_circumference(radius):
return round(2 * math.pi * radius, 2)
@staticmethod
def circle_area(radius):
return round(math.pi * radius ** 2, 2)
@staticmethod
def sphere_surface_area(radius):
return round(4 * math.pi * radius ** 2, 2)
@staticmethod
def sphere_volume(radius):
return round(4 / 3 * math.pi * radius ** 3, 2)
# 测试代码
if __name__ == '__main__':
radius = 5
print(f"半径为 {radius} 的圆的周长为 {MyMath.circle_circumference(radius)}")
print(f"半径为 {radius} 的圆的面积为 {MyMath.circle_area(radius)}")
print(f"半径为 {radius} 的球的表面积为 {MyMath.sphere_surface_area(radius)}")
print(f"半径为 {radius} 的球的体积为 {MyMath.sphere_volume(radius)}")
```
运行结果如下:
```
半径为 5 的圆的周长为 31.42
半径为 5 的圆的面积为 78.54
半径为 5 的球的表面积为 314.16
半径为 5 的球的体积为 523.6
```
希望能够帮助到你!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)