Created
November 28, 2011 13:34
-
-
Save ThawanFidelis/1400414 to your computer and use it in GitHub Desktop.
Soluções para os exercícios de Programação Orientada a Objetos (Java)
This file contains hidden or 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 exercicio_1; | |
public class Bola { | |
private String cor; | |
public Bola(String cor){ | |
if (cor == null || cor.equals("")) | |
throw new RuntimeException(); | |
this.cor = cor; | |
} | |
public String cor(){ | |
return cor; | |
} | |
public void trocarCor(String novaCor){ | |
if (novaCor == null || novaCor.equals("")) | |
throw new RuntimeException(); | |
this.cor = novaCor; | |
} | |
} |
This file contains hidden or 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 exercicio_1; | |
import static exercicio_1.Bola.*; | |
import static org.junit.Assert.*; | |
import org.junit.Test; | |
public class BolaTest { | |
@Test | |
public void bolaSoPodeSerCriadaComUmaCor(){ | |
try{ | |
new Bola(null); | |
fail("bola existe sem uma cor, deveria gerar exceção"); | |
} | |
catch(RuntimeException e){ | |
} | |
} | |
@Test | |
public void permiteTrocarACor(){ | |
Bola bola = new Bola("azul"); | |
bola.trocarCor("verde"); | |
assertEquals("verde", bola.cor()); | |
} | |
@Test | |
public void corDaBolaNaoPodeSerTrocadaParaNull(){ | |
Bola bola = new Bola("branco"); | |
try{ | |
bola.trocarCor(""); | |
fail("bola existe sem uma cor, deveria gerar exceção"); | |
} | |
catch(RuntimeException e){ | |
} | |
} | |
} |
This file contains hidden or 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 exercicio_2; | |
public class Quadrado { | |
private double lado; | |
public Quadrado(double lado){ | |
if(lado < 1) | |
throw new RuntimeException(); | |
this.lado = lado; | |
} | |
public double lado(){ | |
return lado; | |
} | |
public double area(){ | |
return (lado * lado); | |
} | |
public void alterarLado(double novoLado){ | |
if(novoLado < 1) | |
throw new RuntimeException(); | |
lado = novoLado; | |
} | |
} |
This file contains hidden or 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 exercicio_2; | |
import static exercicio_2.Quadrado.*; | |
import static org.junit.Assert.*; | |
import org.junit.Test; | |
public class QuadradoTest { | |
@Test | |
public void quadradoSoDeveExistirSeLadoForMaiorQueZero(){ | |
try{ | |
new Quadrado(0); | |
fail("Quadrado com lado 0, deveria gerar exceção"); | |
} | |
catch(RuntimeException e){ | |
} | |
} | |
@Test | |
public void devePermitirTrocarOValorDoLado(){ | |
Quadrado quadrado = new Quadrado(2); | |
assertEquals(2, quadrado.lado(), 0); | |
quadrado.alterarLado(3); | |
assertEquals(3, quadrado.lado(), 0); | |
} | |
@Test | |
public void deveCalcularArea(){ | |
Quadrado quadrado = new Quadrado(1); | |
assertEquals(1, quadrado.area(), 0); | |
} | |
} |
This file contains hidden or 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 exercicio_7; | |
public class BombaCombustivel { | |
private double capacidadeDaBomba; | |
private double precoPorLitro; | |
private double estado; | |
public BombaCombustivel(double capacidade, double preco){ | |
this.capacidadeDaBomba = capacidade; | |
this.precoPorLitro = preco; | |
estado = capacidade; | |
} | |
public double capacidade(){ | |
return capacidadeDaBomba; | |
} | |
public double precoPorLitro(){ | |
return precoPorLitro; | |
} | |
public void encher(){ | |
estado = capacidadeDaBomba; | |
} | |
public double quantidadeCombustivelDisponivel(){ | |
return estado; | |
} | |
public double abastecerPorValor(double valor){ | |
double quantidadeAbastecida = valor / precoPorLitro; | |
estado -= quantidadeAbastecida; | |
return quantidadeAbastecida; | |
} | |
public double abastecerPorLitro(double litro){ | |
if (verificarSeHaCapacidadeSuficienteParaAbastecer(litro)){ | |
estado -= litro; | |
} | |
return litro * precoPorLitro; | |
} | |
private boolean verificarSeHaCapacidadeSuficienteParaAbastecer(double quantidadeAserAbastecida){ | |
if (quantidadeAserAbastecida > quantidadeCombustivelDisponivel()) | |
throw new RuntimeException(); | |
return true; | |
} | |
} |
This file contains hidden or 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 exercicio_7; | |
import static exercicio_7.BombaCombustivel.*; | |
import static org.junit.Assert.*; | |
import org.junit.Test; | |
public class BombaCombustivelTest { | |
@Test | |
public void deveSerCapazDeDefinirCapacidadeDaBoma(){ | |
BombaCombustivel bomba = new BombaCombustivel(10, 5); | |
assertEquals(10, bomba.capacidade(), 0); | |
assertEquals(5, bomba.precoPorLitro(), 0); | |
bomba.encher(); | |
assertEquals(10, bomba.quantidadeCombustivelDisponivel(), 0); | |
} | |
@Test | |
public void deveSerCapazDeAbastecerPorValorEporLitros(){ | |
BombaCombustivel bomba = new BombaCombustivel(10, 5); | |
assertEquals(2, bomba.abastecerPorValor(10), 0); | |
assertEquals(40, bomba.abastecerPorLitro(8), 0); | |
assertEquals(0, bomba.quantidadeCombustivelDisponivel(), 0); | |
} | |
@Test | |
public void naoDeveAbastecerQuandoQuantidadeDisponivelForMenorQueQuantidaderequisitada(){ | |
BombaCombustivel bomba = new BombaCombustivel(10, 5); | |
try{ | |
bomba.abastecerPorLitro(11); | |
fail("quantidade deisponivel na bomba menor que a requisitada, deveria gerar exceção."); | |
} | |
catch(RuntimeException e){ | |
} | |
} | |
@Test | |
public void deveSerCapazDeEncherABomba(){ | |
BombaCombustivel bomba = new BombaCombustivel(10, 5); | |
assertEquals(50, bomba.abastecerPorLitro(10), 0); | |
assertEquals(0, bomba.quantidadeCombustivelDisponivel(), 0); | |
bomba.encher(); | |
assertEquals(10, bomba.quantidadeCombustivelDisponivel(), 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment