Created
February 13, 2012 11:43
-
-
Save rodrigomanhaes/1816252 to your computer and use it in GitHub Desktop.
Exemplo de Polimorfismo em Java feito na aula de POO (do modo em que estavam quando terminou a aula...)
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
package figura; | |
public class Circulo extends Figura { | |
private double raio; | |
public Circulo(double raio) { | |
this.raio = raio; | |
} | |
public double raio() { | |
return this.raio; | |
} | |
public double area() { | |
return Math.PI * Math.pow(raio, 2); | |
} | |
} |
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
package figura; | |
import static org.junit.Assert.*; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class CirculoTest { | |
private Circulo circulo; | |
@Before | |
public void criarCirculo() { | |
circulo = new Circulo(2); | |
} | |
@Test | |
public void possuiRaio() { | |
assertEquals(2, circulo.raio(), 0); | |
} | |
@Test | |
public void calculaSuaArea() { | |
assertEquals(12.57, circulo.area(), 0.01); | |
} | |
@Test | |
public void desenha() { | |
assertEquals("circulo(area=12.57)", circulo.desenhar()); | |
} | |
} |
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
package figura; | |
import java.util.LinkedList; | |
import java.util.List; | |
public class Desenhador { | |
private List<Figura> figuras; | |
public Desenhador(List<Figura> figuras) { | |
this.figuras = figuras; | |
} | |
public List<String> desenhar() { | |
List<String> resultado = new LinkedList<String>(); | |
for (Figura f: figuras) | |
resultado.add(f.desenhar()); | |
return resultado; | |
} | |
} |
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
package figura; | |
import java.util.LinkedList; | |
import java.util.List; | |
import org.junit.Test; | |
import static org.junit.Assert.*; | |
public class DesenhadorTest { | |
@Test | |
public void desenharFiguras() { | |
List<Figura> figuras = new LinkedList<Figura>(); | |
figuras.add(new Triangulo(3, 4)); | |
figuras.add(new Circulo(2)); | |
Desenhador desenhador = new Desenhador(figuras); | |
List<String> esperado = new LinkedList<String>(); | |
esperado.add("triangulo(area=6.00)"); | |
esperado.add("circulo(area=12.57)"); | |
assertEquals(esperado, desenhador.desenhar()); | |
} | |
} |
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
package figura; | |
public abstract class Figura { | |
public String desenhar() { | |
return String.format("%s(area=%.2f)", | |
this.getClass().getSimpleName().toLowerCase(), | |
area()); | |
} | |
public abstract double area(); | |
} | |
abstract class Foo extends Figura { | |
public double area() { | |
return 0; | |
} | |
} | |
class Bar extends Foo {} |
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
package figura; | |
import static org.junit.Assert.*; | |
import org.junit.Test; | |
public class FiguraTest { | |
@Test | |
public void calculaSuaArea() { | |
Figura figura = new Figura(); | |
assertEquals(0, figura.area(), 0); | |
} | |
} |
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
package figura; | |
public class Triangulo extends Figura { | |
private double base, altura; | |
public Triangulo(double base, double altura) { | |
this.base = base; | |
this.altura = altura; | |
} | |
public double area() { | |
return base * altura / 2; | |
} | |
public double base() { | |
return this.base; | |
} | |
public double altura() { | |
return this.altura; | |
} | |
} |
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
]package figura; | |
import static org.junit.Assert.assertEquals; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class TrianguloTest { | |
private Triangulo triangulo; | |
@Before | |
public void criarTriangulo() { | |
triangulo = new Triangulo(3, 4); | |
} | |
@Test | |
public void possuiBaseEAltura() { | |
assertEquals(3, triangulo.base(), 0); | |
assertEquals(4, triangulo.altura(), 0); | |
} | |
@Test | |
public void calculaSuaArea() { | |
assertEquals(6, triangulo.area(), 0); | |
} | |
@Test | |
public void desenha() { | |
assertEquals("triangulo(area=6.00)", triangulo.desenhar()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment