-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCubeCalc.py
63 lines (50 loc) · 2.25 KB
/
CubeCalc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Cube:
"""
This class represents a 3D cube and provides methods to calculate its geometrical properties
like surface area and volume.
Attributes:
side_length (float): The length of a side of the cube in centimeters.
"""
def __init__(self, side_length):
"""
Initializes a Cube object.
Args:
side_length (float): The positive length of a side of the cube in centimeters.
Raises:
TypeError: If the side_length is not a number.
ValueError: If the side_length is not positive.
"""
if not isinstance(side_length, (int, float)):
raise TypeError("Side length must be a number.")
if side_length <= 0:
raise ValueError("Side length must be a positive number.")
self.side_length = side_length
def surface_area(self):
"""
Calculates and returns the total surface area of the cube in square centimeters.
Formula: surface area = 6 * side_length^2
Returns:
float: The surface area of the cube in square centimeters, rounded to 5 decimal places.
"""
surface_area = 6 * pow(self.side_length,2)
return round(surface_area, 5)
def volume(self):
"""
Calculates and returns the volume of the cube in cubic centimeters.
Formula: volume = side_length^3
Returns:
float: The volume of the cube in cubic centimeters, rounded to 5 decimal places.
"""
volume = pow(self.side_length, 3)
return round(volume, 5)
def get_description(self):
"""
Returns a tuple containing two formatted strings describing the cube's surface area and volume.
Returns:
tuple[str, str]: A tuple containing user-friendly descriptions of the cube's surface area
and volume in square centimeters and cubic centimeters respectively.
"""
surface_area_info = "Cube with side length {} cm has surface area {} cm2.".format(self.side_length, round(self.surface_area(),3))
volume_info = "Cube with side length {} cm has volume {} cm3.".format(self.side_length, round(self.volume(),3))
return(surface_area_info, volume_info)
# help(Cube)