Created
May 23, 2018 00:35
-
-
Save tiagox/d6d97dda08ba701e119d91682c0ac7e5 to your computer and use it in GitHub Desktop.
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 App { | |
public static void main(String[] args) { | |
Forma techo = new Triangulo(10.0, 4.0); | |
Forma pared = new Cuadrado(8.0); | |
Forma paredSegundoPiso = new Cuadrado(6.0); | |
Forma balcon = new Cuadrado(2.0); | |
FormaCompuesta segundaPlanta = new FormaCompuesta(); | |
segundaPlanta.agregar(paredSegundoPiso); | |
segundaPlanta.agregar(balcon); | |
FormaCompuesta casa = new FormaCompuesta(); | |
casa.agregar(techo); | |
casa.agregar(pared); | |
casa.agregar(segundaPlanta); | |
// Area total: 124.0 | |
System.out.println(casa.calcularArea()); | |
} | |
} |
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 Cuadrado extends Forma { | |
private double lado; | |
public Cuadrado(double lado) { | |
this.lado = lado; | |
} | |
@Override | |
public double calcularArea() { | |
return lado * lado; | |
} | |
} |
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 abstract class Forma { | |
public abstract double calcularArea(); | |
} |
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
import java.util.LinkedList; | |
public class FormaCompuesta extends Forma { | |
private LinkedList<Forma> formas = new LinkedList<Forma>(); | |
@Override | |
public double calcularArea() { | |
double resultado = 0; | |
for (Forma forma : formas) { | |
resultado += forma.calcularArea(); | |
} | |
return resultado; | |
} | |
public void agregar(Forma forma) { | |
formas.add(forma); | |
} | |
} |
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 Triangulo extends Forma { | |
private double lado; | |
private double altura; | |
public Triangulo(double lado, double altura) { | |
this.lado = lado; | |
this.altura = altura; | |
} | |
@Override | |
public double calcularArea() { | |
return lado * altura / 2.0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment