-
Notifications
You must be signed in to change notification settings - Fork 3
Update oop-exercise.mdx #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,109 @@ id: oop-exercise | |
title: Ejercicio de programación orientada a objetos | ||
--- | ||
|
||
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. | ||
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure | ||
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non | ||
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. | ||
Este ejercicio es bastante similar a lo que encontrarías en un [Ejercicio Algorítmico](algorithmic-exercise) con la excepción de que este también se centrará en tus habilidades para modelar el problema utilizando programación orientada a objetos, y en la mayoría de los casos, el problema a resolver será un poco más sencillo que los presentados en un Ejercicio Algorítmico. El tiempo que se te dará para este ejercicio será de alrededor de 30 minutos a 1 hora. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above |
||
|
||
### Ejemplo de Ejercicio: | ||
#### Enunciado | ||
Dado un árbol binario representado con nodos internos y nodos hoja, imprime el valor contenido en todos los nodos hoja. | ||
|
||
- Un LeafNode es un nodo que no tiene ningún hijo y que contiene un valor entero. | ||
|
||
- Un InternalNode es un nodo que puede tener de 1 a 2 nodos hijos que pueden ser otro InternalNode o un LeafNode. | ||
|
||
#### Solución sin utilizar conceptos de POO | ||
|
||
```java | ||
class Node { | ||
Node left; | ||
Node right; | ||
int value; | ||
Node(int value, Node left, Node right) { | ||
this.value = value; | ||
this.left = left; | ||
this.right = right; | ||
} | ||
} | ||
|
||
public class Main { | ||
public static void printValuesInBTree(Node node) { // | ||
if (node == null) return; // () | ||
if (node.left == null && node.right == null) { // isLeaf // / \ | ||
System.out.println(node.value); // () 3 | ||
} else { // / \ | ||
printValuesInBTree(node.left); // 1 () | ||
printValuesInBTree(node.right); // / | ||
} // 2 | ||
} // | ||
|
||
public static void main(String[] args) { | ||
Node root = new Node(0, new Node(0, new Node(1, null, null), new Node(0, new Node(2, null, null), null)), new Node(3, null, null)); | ||
printValuesInBTree(root); | ||
} | ||
} | ||
``` | ||
|
||
#### Solución utilizando los cuatro pilares de la POO: Abstracción, Encapsulamiento, Herencia y Polimorfismo | ||
|
||
```java | ||
abstract class Node { | ||
// La mayoría de los lenguajes ya tienen una forma de recuperar el tipo de un objeto | ||
// Para ser genéricos, vamos a usar un método que nos dirá directamente el tipo | ||
abstract String type(); | ||
} | ||
|
||
class LeafNode extends Node { | ||
private int value; | ||
|
||
LeafNode(int value) { | ||
this.value = value; | ||
} | ||
String type() { | ||
return "leaf"; | ||
} | ||
int getValue() { | ||
return this.value; | ||
} | ||
} | ||
|
||
class InternalNode extends Node { | ||
private Node left; | ||
private Node right; | ||
|
||
InternalNode(Node left, Node right) { | ||
this.left = left; | ||
this.right = right; | ||
} | ||
String type() { | ||
return "internal"; | ||
} | ||
Node getLeftNode() { | ||
return this.left; | ||
} | ||
Node getRightNode() { | ||
return this.right; | ||
} | ||
} | ||
|
||
public class Main { | ||
public static void printValuesInBTree(Node node) { | ||
if (node == null) return; | ||
if (node.type() == "leaf") { | ||
LeafNode leaf = (LeafNode) node; | ||
System.out.println(leaf.getValue()); | ||
} else { | ||
InternalNode internalNode = (InternalNode) node; | ||
printValuesInBTree(internalNode.getLeftNode()); | ||
printValuesInBTree(internalNode.getRightNode()); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
Node root = new InternalNode(new InternalNode(new LeafNode(1), new InternalNode(new LeafNode(2), null)), new LeafNode(3)); | ||
printValuesInBTree(root); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing ``` |
||
|
||
Consejos generales para tener éxito en el Ejercicio de Diseño Orientado a Objetos: | ||
- Sigue el diagrama de flujo y los consejos presentados en Ejercicio Algorítmico durante la entrevista. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. en el ejercicio de algoritmia |
||
- Estudia más sobre Patrones de Diseño. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ejercicio de Algoritmia