-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathU3-Ejercicio-2.py
More file actions
82 lines (61 loc) · 1.89 KB
/
U3-Ejercicio-2.py
File metadata and controls
82 lines (61 loc) · 1.89 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
Crea una clase base Figura con método area() y perimetro().
Luego crea clases hijas:
- Circulo
- Rectangulo
- Triangulo
Cada una debe implementar los métodos de la clase base.
"""
import math
class Figura:
def area(self):
raise NotImplementedError("Método area() debe ser implementado")
def perimetro(self):
raise NotImplementedError("Método perimetro() debe ser implementado")
class Circulo(Figura):
def __init__(self, radio):
self.radio = radio
def area(self):
return math.pi * self.radio ** 2
def perimetro(self):
return 2 * math.pi * self.radio
def __str__(self):
return f"Círculo (radio: {self.radio})"
class Rectangulo(Figura):
def __init__(self, base, altura):
self.base = base
self.altura = altura
def area(self):
return self.base * self.altura
def perimetro(self):
return 2 * (self.base + self.altura)
def __str__(self):
return f"Rectángulo ({self.base}x{self.altura})"
class Triangulo(Figura):
def __init__(self, base, altura, lado1, lado2, lado3):
self.base = base
self.altura = altura
self.lado1 = lado1
self.lado2 = lado2
self.lado3 = lado3
def area(self):
return (self.base * self.altura) / 2
def perimetro(self):
return self.lado1 + self.lado2 + self.lado3
def __str__(self):
return f"Triángulo (base: {self.base}, altura: {self.altura})"
# Demo del sistema
def demo_figuras():
figuras = [
Circulo(5),
Rectangulo(4, 6),
Triangulo(4, 3, 3, 4, 5)
]
print("📐 CÁLCULO DE ÁREAS Y PERÍMETROS\n")
for figura in figuras:
print(f"{figura}")
print(f" Área: {figura.area():.2f}")
print(f" Perímetro: {figura.perimetro():.2f}")
print()
# Ejecutar
demo_figuras()