Last active
October 9, 2015 14:45
-
-
Save tiagox/1bef66ae2aca38e5ec65 to your computer and use it in GitHub Desktop.
Algoritmos y Programación 1 - Ejercicio de composición
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
El objetivo es modelar las entidades Punto y Circulo que representan estos elementos posicionados en un plano. | |
Cosas para prestar atención: | |
* El `Circulo` contiene un `Punto` que representa su centro en el plano. | |
* El `Circulo` puede utilizar al `Punto`, conociendo solo su interfaz y sin romper con el encapsulamiento de `Punto`. | |
* Por medio de la utilización de los constructores, nos aseguramos que cada objeto tenga los atributos necesarios, siempre. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Circulo { | |
private Punto centro; | |
private int radio; | |
public Circulo(Punto centro, int radio) { | |
this.centro = centro; | |
this.radio = radio; | |
} | |
/** | |
* Este método sabe identificar si un punto dado está contenido por el | |
* circulo. | |
* | |
* post: retorna verdadero si el punto está contenido en sí mismo. | |
*/ | |
public boolean contieneA(Punto punto) { | |
return this.centro.distanciaA(punto) < this.radio; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Punto { | |
private double x; | |
private double y; | |
public Punto(double x, double y) { | |
this.x = x; | |
this.y = y; | |
} | |
/** | |
* Este método calcula la distancia a un punto dado, desde el punto. | |
* | |
* post: devuelve la distancia entre el punto destino y sí mismo, | |
* representada por un numero real. | |
*/ | |
public double distanciaA(Punto destino) { | |
return Math.sqrt(Math.pow(this.x - destino.x, 2) | |
+ Math.pow(this.y - destino.y, 2)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment