编写程序,创建类MyMath,计算圆的周长和面积以及球的表面积和体积,并编写测试代码,结果均保留两位小数。
时间: 2024-05-11 22:13:57 浏览: 128
```python
import math
class MyMath:
def circle_circumference(self, radius):
return round(2 * math.pi * radius, 2)
def circle_area(self, radius):
return round(math.pi * radius ** 2, 2)
def sphere_surface_area(self, radius):
return round(4 * math.pi * radius ** 2, 2)
def sphere_volume(self, radius):
return round((4 / 3) * math.pi * radius ** 3, 2)
# 测试代码
math_obj = MyMath()
# 计算圆的周长和面积
radius = 5
circumference = math_obj.circle_circumference(radius)
print("The circumference of the circle is:", circumference)
area = math_obj.circle_area(radius)
print("The area of the circle is:", area)
# 计算球的表面积和体积
radius = 3
surface_area = math_obj.sphere_surface_area(radius)
print("The surface area of the sphere is:", surface_area)
volume = math_obj.sphere_volume(radius)
print("The volume of the sphere is:", volume)
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)