Uziel Cocolan@ushieru
Requerimientos:
-
Java o Python.
-
Bucles y condicionales.
-
Funciones.
¿Qué es la POO?
La programación orientada a objetos no es mas que la capacidad de transformar objetos (como los de la vida cotidiana) a código.
Ejemplo
Para definir una clase tenemos que tener en cuenta un par de cosas:
-
Nombre
-
Propiedades (Características o estados)
-
Métodos (¿Qué puede hacer?)
El nombre de nuestro objeto es: Televisor.
Sus propiedades son: volumen y canal.
Sus métodos son: cambiar de canal, obtener canal actual, subir volumen, bajar volumen y obtener volumen.
Una vez definido nuestro objeto Manos a la obra (Trabajaremos con Java 8 y Python 3.8 por la facilidad para hacer CLIs) 🚀🚀
Python
1class Televisor: 2 3 def __init__(self, volumen: int, canal: int): 4 self.volumen = volumen 5 self.canal = canal 6 7 def subir_volumen(self): 8 self.volumen += 1 9 10 def bajar_volumen(self): 11 self.volumen -= 1 12 13 def obtener_volumen(self): 14 return self.volumen 15 16 def cambiar_canal(self, canal: int): 17 self.canal = canal 18 19 def obtener_canal(self) -> int: 20 return self.canal
Java
1package main; 2 3public class Television { 4 private int volumen; 5 private int canal; 6 7 public Television(int volumen, int canal){ 8 this.volumen = volumen; 9 this.canal = canal; 10 } 11 12 public void subir_volumen(){ 13 this.volumen++; 14 } 15 16 public void bajar_volumen(){ 17 this.volumen--; 18 } 19 20 public int obtener_volumen(){ 21 return this.canal; 22 } 23 24 public void cambiar_canal(int canal){ 25 this.canal = canal; 26 } 27 28 public int obtener_canal(){ 29 return this.canal; 30 } 31}
En hora buena!
Acabamos de crear nuestro primer objeto! Podríamos ser rigurosos con validaciones o con métodos, pero esto es solo un primer vistazo. Ahora te invito a jugar con el programa de aquí abajo, cambia los valores, mejóralo tanto como quieras. Cuéntame tu experiencia o deja tus preguntas en comentarios. Suerte Devs! 👩💻👨💻
Python
1class Televisor: 2 3 MAX_VOLUMEN = 30 4 MAX_CANAL = 30 5 6 def __init__(self, volumen: int, canal: int): 7 self.volumen = volumen 8 self.canal = canal 9 self.io = False 10 11 def IO(self): 12 self.io = not self.io 13 14 def obtener_estado(self) -> str: 15 if self.io: 16 return 'Encendido' 17 else: 18 return 'Apagado' 19 20 def subir_volumen(self): 21 if not self.io: 22 print('[Televisor Apagado]') 23 return 24 elif self.volumen == self.MAX_VOLUMEN: 25 print('[Max volumen]') 26 return 27 self.volumen += 1 28 29 def bajar_volumen(self): 30 if not self.io: 31 print('[Televisor Apagado]') 32 return 33 elif self.volumen == 0: 34 print('[Min volumen]') 35 return 36 37 self.volumen -= 1 38 39 def obtener_volumen(self): 40 if not self.io: 41 print('[Televisor Apagado]') 42 return 43 44 return self.volumen 45 46 def cambiar_canal(self, canal: int): 47 if not self.io: 48 print('[Televisor Apagado]') 49 return 50 elif canal > self.MAX_CANAL: 51 print('[Max Canal]', self.MAX_CANAL) 52 return 53 elif canal < 1: 54 print('[Min Canal]', 1) 55 return 56 57 self.canal = canal 58 59 def obtener_canal(self) -> int: 60 if not self.io: 61 print('[Televisor Apagado]') 62 return 63 return self.canal 64 65 66if __name__ == "__main__": 67 print('5 para apagar o encender. Usa las teclas 8 y 2 para volumen. 4 y 6 para cambiar de canal. 7 ir a canal.') 68 print('Salir: ctrl + c') 69 televisor = Televisor(0, 1) 70 71 while True: 72 key = input() 73 if key == '5': 74 televisor.IO() 75 print(televisor.obtener_estado()) 76 elif key == '7': 77 try: 78 canal = int(input()) 79 televisor.cambiar_canal(canal) 80 print('Canal:', televisor.obtener_canal()) 81 except: 82 print('Canal no valido') 83 elif key == '8': 84 televisor.subir_volumen() 85 print('Volumen:', televisor.obtener_volumen()) 86 elif key == '2': 87 televisor.bajar_volumen() 88 print('Volumen:', televisor.obtener_volumen()) 89 elif key == '4': 90 televisor.cambiar_canal(televisor.obtener_canal() - 1) 91 print('Canal:', televisor.obtener_canal()) 92 elif key == '6': 93 televisor.cambiar_canal(televisor.obtener_canal() + 1) 94 print('Canal:', televisor.obtener_canal()) 95 print('------------------------------------------------------')
Java
1package main; 2 3import java.util.Scanner; 4 5/** 6 * 7 * @author USHIERU 8 */ 9public class Main { 10 11 public static void main(String[] args) { 12 Scanner scanner = new Scanner(System.in); 13 Television televisor = new Television(0, 1); 14 15 System.out.println("5 para apagar o encender. Usa las teclas 8 y 2 para volumen. 4 y 6 para cambiar de canal. 7 ir a canal."); 16 17 while (true) { 18 String comando = scanner.next(); 19 20 switch (comando) { 21 case "5": 22 televisor.IO(); 23 System.out.println(televisor.obtenerEstado()); 24 break; 25 case "7": 26 try { 27 int canal = scanner.nextInt(); 28 televisor.cambiarCanal(canal); 29 System.out.println("Canal: " + televisor.obtenerCanal()); 30 } catch (Exception e) { 31 System.out.println("Valor invalido"); 32 } 33 break; 34 case "8": 35 televisor.subirVolumen(); 36 System.out.println("Volumen: " + televisor.obtenerVolumen()); 37 break; 38 case "2": 39 televisor.bajarVolumen(); 40 System.out.println("Volument: " + televisor.obtenerVolumen()); 41 break; 42 case "4": 43 televisor.cambiarCanal(televisor.obtenerCanal() - 1); 44 System.out.println("Canal: " + televisor.obtenerCanal()); 45 break; 46 case "6": 47 televisor.cambiarCanal(televisor.obtenerCanal() + 1); 48 System.out.println("Canal: " + televisor.obtenerCanal()); 49 break; 50 } 51 52 System.out.println("------------------------------------------------------"); 53 } 54 } 55} 56 57class Television { 58 59 private int volumen; 60 private int canal; 61 private boolean io; 62 private final int MAX_VOLUMEN = 50; 63 private final int MAX_CANAL = 30; 64 65 public Television(int volumen, int canal) { 66 this.volumen = volumen; 67 this.canal = canal; 68 this.io = false; 69 } 70 71 public void IO() { 72 this.io = !this.io; 73 } 74 75 public String obtenerEstado() { 76 if (this.io) { 77 return "Encendido"; 78 } else { 79 return "Apagado"; 80 } 81 } 82 83 public void subirVolumen() { 84 if (!this.io) { 85 System.out.println("[Televisor Apagado]"); 86 return; 87 } else if (this.volumen == this.MAX_VOLUMEN) { 88 System.out.println("[Max Volumen]"); 89 return; 90 } 91 this.volumen++; 92 } 93 94 public void bajarVolumen() { 95 if (!this.io) { 96 System.out.println("[Televisor Apagado]"); 97 return; 98 } else if (this.volumen == 0) { 99 System.out.println("[Min Volumen]"); 100 return; 101 } 102 this.volumen--; 103 } 104 105 public int obtenerVolumen() { 106 if (!this.io) { 107 System.out.println("[Televisor Apagado]"); 108 return 0; 109 } 110 return this.volumen; 111 } 112 113 public void cambiarCanal(int canal) { 114 if (!this.io) { 115 System.out.println("[Televisor Apagado]"); 116 return; 117 } else if (canal > this.MAX_CANAL) { 118 System.out.println("[Max Canal " + this.MAX_CANAL + "]"); 119 return; 120 } 121 this.canal = canal; 122 } 123 124 public int obtenerCanal() { 125 if (!this.io) { 126 System.out.println("[Televisor Apagado]"); 127 return 0; 128 } 129 return this.canal; 130 } 131}
