Skip to content

Instantly share code, notes, and snippets.

@rodrigomanhaes
Created November 25, 2011 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodrigomanhaes/1394684 to your computer and use it in GitHub Desktop.
Save rodrigomanhaes/1394684 to your computer and use it in GitHub Desktop.
Soluções feitas em aula para os exercícios de Programação Orientada a Objetos (Java)
package exercicio06;
public class TV {
public static final int PRIMEIRO_CANAL = 1;
public static final int ULTIMO_CANAL = 60;
private boolean ligada;
private int canal;
private int volume;
public void ligar() {
ligada = true;
}
public boolean estaLigada() {
return ligada;
}
public void desligar() {
ligada = false;
}
public void trocarCanal(int canal) {
if (canal < 1 || canal > 60)
throw new RuntimeException();
this.canal = canal;
}
public int canal() {
return canal;
}
public int volume() {
return volume;
}
public void aumentarVolume() {
if (volume < 30)
volume++;
}
public void diminuirVolume() {
if (volume > 0)
volume--;
}
}
package exercicio06;
import static org.junit.Assert.*;
import org.junit.Test;
public class TVTest {
@Test
public void permiteSerLigadaEDesligada() {
TV tv = new TV();
tv.ligar();
assertTrue(tv.estaLigada());
tv.desligar();
assertFalse(tv.estaLigada());
}
@Test
public void permiteTrocarDeCanal() {
TV tv = new TV();
tv.trocarCanal(2);
assertEquals(2, tv.canal());
}
@Test
public void canalSoPodeSerAlteradoDentroDeUmaFaixa() {
TV tv = new TV();
tv.trocarCanal(TV.PRIMEIRO_CANAL);
tv.trocarCanal(TV.ULTIMO_CANAL);
try {
tv.trocarCanal(TV.PRIMEIRO_CANAL - 1);
fail("Canal fora da faixa, deveria ter disparado exceção");
}
catch(RuntimeException e) {
}
try {
tv.trocarCanal(TV.ULTIMO_CANAL + 1);
fail("Canal fora da faixa, deveria ter disparado exceção");
}
catch(RuntimeException e) {
}
}
@Test
public void permiteAumentarEDiminuirVolume() {
TV tv = new TV();
assertEquals(0, tv.volume());
tv.aumentarVolume();
assertEquals(1, tv.volume());
tv.diminuirVolume();
assertEquals(0, tv.volume());
}
@Test
public void volumeSoPodeSerAlteradoDentroDeUmaFaixa() {
TV tv = new TV();
assertEquals(0, tv.volume());
tv.diminuirVolume();
assertEquals(0, tv.volume());
for (int i = 1; i <= 30; i++)
tv.aumentarVolume();
assertEquals(30, tv.volume());
tv.aumentarVolume();
assertEquals(30, tv.volume());
}
}
package exercicio08;
public class Ponto {
private double x, y;
public String toString() {
return "(" + this.x + ", " + this.y + ")";
}
public Ponto(double x, double y) {
this.x = x;
this.y = y;
}
public double[] comoVetor() {
return new double[] { this.x(), this.y() };
}
public double x() {
return this.x;
}
public double y() {
return this.y;
}
public boolean equals(Object outro) {
Ponto outroPonto = (Ponto) outro;
return this.x == outroPonto.x && this.y == outroPonto.y;
}
}
package exercicio08;
import static org.junit.Assert.*;
import org.junit.Test;
public class PontoTest {
@Test
public void possuiDimensoesXeY() {
Ponto ponto = new Ponto(1, 2);
assertEquals(1, ponto.x(), 0);
assertEquals(2, ponto.y(), 0);
}
@Test
public void comparaIgualdadeComOutroPonto() {
Ponto p1 = new Ponto(1, 2);
Ponto p2 = new Ponto(1, 2);
assertEquals(p1, p2);
}
@Test
public void produzUmaRepresentacaoComoArray() {
Ponto p = new Ponto(1, 2);
assertArrayEquals(new double[] { 1, 2 }, p.comoVetor(), 0);
}
}
package exercicio08;
public class Retangulo {
private double altura, largura;
private Ponto centro;
public Retangulo(double altura, double largura, Ponto centro) {
this.altura = altura;
this.largura = largura;
this.centro = centro;
}
public double altura() {
return this.altura;
}
public double largura() {
return this.largura;
}
public Ponto centro() {
return centro;
}
public Ponto[] vertices() {
double xDeslocamento = largura / 2,
yDeslocamento = altura / 2;
return new Ponto[] {
new Ponto(centro.x() - xDeslocamento, centro.y() - yDeslocamento),
new Ponto(centro.x() + xDeslocamento, centro.y() - yDeslocamento),
new Ponto(centro.x() - xDeslocamento, centro.y() + yDeslocamento),
new Ponto(centro.x() + xDeslocamento, centro.y() + yDeslocamento)
};
}
}
package exercicio08;
import static org.junit.Assert.*;
import org.junit.Test;
public class RetanguloTest {
@Test
public void possuiLarguraAlturaECentro() {
Ponto centro = new Ponto(10, 8);
Retangulo retangulo = new Retangulo(3, 4, centro);
assertEquals(3, retangulo.altura(), 0);
assertEquals(4, retangulo.largura(), 0);
assertEquals(centro, retangulo.centro());
}
@Test
public void calculaSeusVertices() {
Retangulo retangulo = new Retangulo(4, 2, new Ponto(10, 8));
assertArrayEquals(
new Ponto[] {
new Ponto(9, 6),
new Ponto(11, 6),
new Ponto(9, 10),
new Ponto(11, 10)
},
retangulo.vertices());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment