编写程序,创建类 MyMath,计算圆的周长和面积以及球的表面积和体积,并编写测试 代码,结果均保留两位小数。
时间: 2023-09-16 17:09:58 浏览: 159
以下是 MyMath 类的实现:
```python
import math
class MyMath:
def __init__(self, radius):
self.radius = radius
def circle_circumference(self):
return round(2 * math.pi * self.radius, 2)
def circle_area(self):
return round(math.pi * self.radius**2, 2)
def sphere_surface_area(self):
return round(4 * math.pi * self.radius**2, 2)
def sphere_volume(self):
return round((4/3) * math.pi * self.radius**3, 2)
```
以下是测试代码:
```python
my_math = MyMath(5)
print("Circle circumference:", my_math.circle_circumference())
print("Circle area:", my_math.circle_area())
print("Sphere surface area:", my_math.sphere_surface_area())
print("Sphere volume:", my_math.sphere_volume())
```
输出结果为:
```
Circle circumference: 31.42
Circle area: 78.54
Sphere surface area: 314.16
Sphere volume: 523.6
```
阅读全文