Created
November 30, 2011 03:31
-
-
Save rodrigomanhaes/1407869 to your computer and use it in GitHub Desktop.
Soluções em Java da Lista 1 de exercícios de Programação Orientada a Objetos
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 exercicio01; | |
public class Bola { | |
private String cor; | |
public Bola(String cor) { | |
this.cor = cor; | |
} | |
public String getCor() { | |
return this.cor; | |
} | |
public void setCor(String novaCor) { | |
this.cor = novaCor; | |
} | |
} |
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 exercicio01; | |
import static org.junit.Assert.*; | |
import org.junit.Test; | |
public class BolaTest { | |
@Test | |
public void possuiUmaCor() { | |
Bola bola = new Bola("verde"); | |
assertEquals("verde", bola.getCor()); | |
} | |
@Test | |
public void alteraSuaCor() { | |
Bola bola = new Bola("verde"); | |
bola.setCor("vermelha"); | |
assertEquals("vermelha", bola.getCor()); | |
} | |
} |
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 exercicio07; | |
public class Bomba { | |
private double capacidade; | |
private double precoPorLitro; | |
private double quantidade; | |
public Bomba(double capacidade, double precoPorLitro) { | |
setCapacidade(capacidade); | |
setPrecoPorLitro(precoPorLitro); | |
} | |
public void encher() { | |
this.quantidade = this.capacidade; | |
} | |
public double abastecerPorValor(double valor) { | |
double litros = valor / precoPorLitro; | |
verificarQuantidade(litros); | |
this.quantidade -= litros; | |
return litros; | |
} | |
public double abastecerPorLitros(double litros) { | |
verificarQuantidade(litros); | |
this.quantidade -= litros; | |
return litros * precoPorLitro; | |
} | |
public void setPrecoPorLitro(double precoPorLitro) { | |
if (precoPorLitro <= 0) | |
throw new RuntimeException(); | |
this.precoPorLitro = precoPorLitro; | |
} | |
private void setCapacidade(double capacidade) { | |
validar(capacidade); | |
this.capacidade = capacidade; | |
} | |
private void verificarQuantidade(double litros) { | |
if (this.quantidade < litros) | |
throw new RuntimeException(); | |
} | |
private void validar(double medida) { | |
if (medida<= 0) | |
throw new RuntimeException(); | |
} | |
public double quantidade() { | |
return quantidade; | |
} | |
public double capacidade() { | |
return capacidade; | |
} | |
public double precoPorLitro() { | |
return precoPorLitro; | |
} | |
} |
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 exercicio07; | |
import static org.junit.Assert.*; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class BombaTest { | |
private Bomba bomba; | |
@Before | |
public void criarBomba() { | |
bomba = new Bomba(1000, 3.00); | |
} | |
@Test | |
public void possuiCapacidadeEPrecoPorLitro() { | |
assertEquals(1000, bomba.capacidade(), 0); | |
assertEquals(3, bomba.precoPorLitro(), 0); | |
} | |
@Test | |
public void permiteSerEnchida() { | |
assertEquals(0, bomba.quantidade(), 0); | |
bomba.encher(); | |
assertEquals(1000, bomba.quantidade(), 0); | |
} | |
@Test | |
public void abastecePorValor() { | |
bomba.encher(); | |
assertEquals(10, bomba.abastecerPorValor(30), 0); | |
assertEquals(990, bomba.quantidade(), 0); | |
} | |
@Test | |
public void abastecePorLitros() { | |
bomba.encher(); | |
assertEquals(90, bomba.abastecerPorLitros(30), 0); | |
assertEquals(970, bomba.quantidade(), 0); | |
} | |
@Test | |
public void permiteAlterarPrecoPorLitro() { | |
bomba.setPrecoPorLitro(2); | |
bomba.encher(); | |
assertEquals(2, bomba.precoPorLitro(), 0); | |
assertEquals(60, bomba.abastecerPorLitros(30), 0); | |
} | |
@Test(expected=RuntimeException.class) | |
public void naoAceitaPrecoZeroNaInicializacao() { | |
new Bomba(1000, 0); | |
} | |
@Test(expected=RuntimeException.class) | |
public void naoAceitaPrecoZeroNaAlteracao() { | |
bomba.setPrecoPorLitro(0); | |
} | |
@Test(expected=RuntimeException.class) | |
public void naoAceitaPrecoNegativoNaInicializacao() { | |
new Bomba(1000, -0.1); | |
} | |
@Test(expected=RuntimeException.class) | |
public void naoAceitaPrecoNegativoNaAlteracao() { | |
bomba.setPrecoPorLitro(-0.1); | |
} | |
@Test(expected=RuntimeException.class) | |
public void naoAceitaCapacidadeZero() { | |
new Bomba(0, 2.00); | |
} | |
@Test(expected=RuntimeException.class) | |
public void naoAceitaCapacidadeNegativa() { | |
new Bomba(-0.1, 2.00); | |
} | |
@Test(expected=RuntimeException.class) | |
public void naoAceitaAbastecimentoPorLitroSeNaoHaCombustivelSuficiente() { | |
bomba.encher(); | |
bomba.abastecerPorLitros(990); | |
bomba.abastecerPorLitros(10.1); | |
} | |
@Test(expected=RuntimeException.class) | |
public void naoAceitaAbastecimentoPorValorSeNaoHaCombustivelSuficiente() { | |
bomba.encher(); | |
bomba.abastecerPorValor(2900); | |
bomba.abastecerPorValor(100.1); | |
} | |
} |
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 exercicio05; | |
public class Conta { | |
private String nomeCorrentista; | |
private String numero; | |
private double saldo; | |
public Conta(String nomeCorrentista, String numero) { | |
this.nomeCorrentista = nomeCorrentista; | |
this.numero = numero; | |
this.saldo = 0; | |
} | |
public String nomeCorrentista() { | |
return nomeCorrentista; | |
} | |
public String numero() { | |
return numero; | |
} | |
public double saldo() { | |
return saldo; | |
} | |
public void depositar(double valor) { | |
this.saldo += valor; | |
} | |
public void sacar(double valor) { | |
if (this.saldo - valor < 0) | |
throw new RuntimeException(); | |
this.saldo -= valor; | |
} | |
} |
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 exercicio05; | |
import static org.junit.Assert.*; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class ContaTest { | |
private Conta conta; | |
@Before | |
public void criarConta() { | |
conta = new Conta("Linus", "12345"); | |
} | |
@Test | |
public void possuiNomeDoCorrentistaENumero() { | |
assertEquals("Linus", conta.nomeCorrentista()); | |
assertEquals("12345", conta.numero()); | |
} | |
@Test | |
public void possuiSaldoInicialZero() { | |
assertEquals(0, conta.saldo(), 0); | |
} | |
@Test | |
public void permiteRealizarDepositos() { | |
conta.depositar(110); | |
assertEquals(110, conta.saldo(), 0); | |
conta.depositar(10.5); | |
assertEquals(120.5, conta.saldo(), 0); | |
} | |
@Test | |
public void permiteRealizarSaques() { | |
conta.depositar(100); | |
conta.sacar(75); | |
assertEquals(25, conta.saldo(), 0); | |
conta.sacar(10.5); | |
assertEquals(14.5, conta.saldo(), 0); | |
} | |
@Test(expected=RuntimeException.class) | |
public void disparaExcecaoParaSaqueMaiorQueSaldo() { | |
conta.depositar(100); | |
conta.sacar(100.1); | |
} | |
@Test | |
public void permiteSacarAteZerarSaldo() { | |
conta.depositar(100); | |
conta.sacar(75); | |
conta.sacar(25); | |
assertEquals(0, conta.saldo(), 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 exercicio11; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Numero { | |
private int valor; | |
public Numero(int valor) { | |
this.valor = valor; | |
} | |
public boolean ehPrimo() { | |
for (int i = 2; i <= valor/2; i++) | |
if (valor % i == 0) | |
return false; | |
return true; | |
} | |
public boolean ehPerfeito() { | |
int soma = 0; | |
for (int i = 1; i <= valor/2; i++) | |
if (valor % i == 0) | |
soma += i; | |
return soma == valor; | |
} | |
public boolean ehFeliz() { | |
int calculado = valor; | |
List<Integer> anteriores = new ArrayList<Integer>(); | |
while (calculado != 1) { | |
int velhoCalculado = calculado; | |
calculado = 0; | |
for (int n: partes(velhoCalculado)) | |
calculado += n * n; | |
if (anteriores.contains(calculado)) | |
return false; | |
anteriores.add(calculado); | |
} | |
return true; | |
} | |
private int[] partes(int numero) { | |
String strValor = String.valueOf(numero); | |
int[] partes = new int[strValor.length()]; | |
for (int i = 0; i < strValor.length(); i++) | |
partes[i] = Integer.parseInt(String.valueOf(strValor.charAt(i))); | |
return partes; | |
} | |
} |
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 exercicio09; | |
public class NumeroComplexo { | |
private double real; | |
private double imaginaria; | |
public NumeroComplexo(double real, double imaginaria) { | |
this.real = real; | |
this.imaginaria = imaginaria; | |
} | |
public NumeroComplexo somar(NumeroComplexo outro) { | |
return new NumeroComplexo(this.real() + outro.real(), | |
this.imaginaria() + outro.imaginaria()); | |
} | |
public NumeroComplexo subtrair(NumeroComplexo outro) { | |
return new NumeroComplexo(this.real() - outro.real(), | |
this.imaginaria() - outro.imaginaria()); | |
} | |
public NumeroComplexo multiplicar(NumeroComplexo outro) { | |
return new NumeroComplexo( | |
this.real() * outro.real() - this.imaginaria() * outro.imaginaria(), | |
this.imaginaria() * outro.real() + this.real() * outro.imaginaria() | |
); | |
} | |
public String toString() { | |
return String.format("%.1f+%.1fi", this.real(), this.imaginaria()); | |
} | |
public boolean equals(Object o) { | |
NumeroComplexo outro = (NumeroComplexo) o; | |
return this.real == outro.real() && this.imaginaria() == outro.imaginaria(); | |
} | |
public double real() { | |
return real; | |
} | |
public double imaginaria() { | |
return imaginaria; | |
} | |
} |
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 exercicio09; | |
import static org.junit.Assert.*; | |
import org.junit.Test; | |
public class NumeroComplexoTest { | |
private NumeroComplexo complexo(double r, double i) { | |
return new NumeroComplexo(r, i); | |
} | |
@Test | |
public void possuiPartesRealEImaginaria() { | |
NumeroComplexo complexo = complexo(2, 3); | |
assertEquals(2, complexo.real(), 0); | |
assertEquals(3, complexo.imaginaria(), 0); | |
} | |
@Test | |
public void adicao() { | |
NumeroComplexo c1 = complexo(-2, 3); | |
assertEquals(complexo(2, 1), c1.somar(complexo(4, -2))); | |
} | |
@Test | |
public void subtracao() { | |
NumeroComplexo c1 = complexo(-2, 3); | |
assertEquals(complexo(-6, 5), c1.subtrair(complexo(4, -2))); | |
} | |
@Test | |
public void produto() { | |
NumeroComplexo c1 = complexo(-2, 3); | |
assertEquals(complexo(-2, 16), c1.multiplicar(complexo(4, -2))); | |
} | |
@Test | |
public void forneceRepresentacaoEmString() { | |
assertEquals("-2.0+3.0i", complexo(-2, 3).toString()); | |
} | |
} |
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 exercicio10; | |
public class NumeroRacional { | |
private int numerador; | |
private int denominador; | |
public NumeroRacional(int numerador, int denominador) { | |
this.numerador = numerador; | |
this.denominador = denominador; | |
simplificar(); | |
} | |
public NumeroRacional somar(NumeroRacional outro) { | |
int mmc = mmc(denominador, outro.denominador); | |
return new NumeroRacional( | |
mmc / denominador * numerador + mmc / outro.denominador * outro.numerador, | |
mmc); | |
} | |
public NumeroRacional subtrair(NumeroRacional outro) { | |
return this.somar(outro.aditivaInversa()); | |
} | |
public NumeroRacional multiplicar(NumeroRacional outro) { | |
return new NumeroRacional(numerador * outro.numerador(), | |
denominador * outro.denominador()); | |
} | |
public NumeroRacional dividir(NumeroRacional outro) { | |
return this.multiplicar(outro.multiplicativaInversa()); | |
} | |
public String toString() { | |
return numerador + "/" + denominador; | |
} | |
public String comoPontoFlutuante(int casas) { | |
return String.format("%." + casas + "f", this.valor()); | |
} | |
public boolean equals(Object o) { | |
NumeroRacional outro = (NumeroRacional) o; | |
return this.denominador() == outro.denominador() && | |
this.numerador() == outro.numerador(); | |
} | |
public int numerador() { | |
return numerador; | |
} | |
public int denominador() { | |
return denominador; | |
} | |
private double valor() { | |
return numerador / (double) denominador; | |
} | |
private NumeroRacional aditivaInversa() { | |
return new NumeroRacional(-numerador, denominador); | |
} | |
public NumeroRacional multiplicativaInversa() { | |
return new NumeroRacional(denominador, numerador); | |
} | |
private void simplificar() { | |
int mdc = mdc(numerador, denominador); | |
numerador /= mdc; | |
denominador /= mdc; | |
} | |
private int mdc(int n1, int n2) { | |
int menor = n1 < n2 ? n1 : n2; | |
for (int divisor = menor; divisor >= 1; divisor--) | |
if (n1 % divisor == 0 && n2 % divisor == 0) | |
return divisor; | |
return 1; | |
} | |
private int mmc(int n1, int n2) { | |
int mmcCandidato = n1 > n2 ? n1 : n2; | |
int incremento = mmcCandidato; | |
while (!(mmcCandidato % n1 == 0 && mmcCandidato % n2 == 0)) | |
mmcCandidato += incremento; | |
return mmcCandidato; | |
} | |
} | |
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 exercicio10; | |
import static org.junit.Assert.*; | |
import org.junit.Test; | |
public class NumeroRacionalTest { | |
private NumeroRacional racional(int numerador, int denominador) { | |
return new NumeroRacional(numerador, denominador); | |
} | |
@Test | |
public void possuiNumeradorEDenominador() { | |
NumeroRacional r = racional(3, 5); | |
assertEquals(3, r.numerador()); | |
assertEquals(5, r.denominador()); | |
} | |
@Test | |
public void ficaSempreNaFormaSimplificada() { | |
NumeroRacional r = racional(4, 6); | |
assertEquals(2, r.numerador()); | |
assertEquals(3, r.denominador()); | |
} | |
@Test | |
public void adicao() { | |
NumeroRacional r1 = racional(1, 3); | |
NumeroRacional r2 = racional(2, 5); | |
assertEquals(racional(11, 15), r1.somar(r2)); | |
} | |
@Test | |
public void subtracao() { | |
NumeroRacional r1 = racional(1, 3); | |
NumeroRacional r2 = racional(2, 5); | |
assertEquals(racional(-1, 15), r1.subtrair(r2)); | |
} | |
@Test | |
public void multiplicacao() { | |
NumeroRacional r1 = racional(1, 3); | |
NumeroRacional r2 = racional(2, 5); | |
assertEquals(racional(2, 15), r1.multiplicar(r2)); | |
} | |
@Test | |
public void divisao() { | |
NumeroRacional r1 = racional(1, 3); | |
NumeroRacional r2 = racional(2, 5); | |
assertEquals(racional(5, 6), r1.dividir(r2)); | |
} | |
@Test | |
public void forneceRepresentacaoString() { | |
assertEquals("7/3", racional(7, 3).toString()); | |
} | |
@Test | |
public void forneceRepresentacaoPontoFlutuante() { | |
assertEquals("0.1", racional(1, 8).comoPontoFlutuante(1)); | |
assertEquals("0.13", racional(1, 8).comoPontoFlutuante(2)); | |
assertEquals("0.125", racional(1, 8).comoPontoFlutuante(3)); | |
} | |
} |
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 exercicio11; | |
import static org.junit.Assert.*; | |
import org.junit.Test; | |
public class NumeroTest { | |
@Test | |
public void respondeSeEPrimo() { | |
assertTrue(new Numero(1).ehPrimo()); | |
assertTrue(new Numero(2).ehPrimo()); | |
assertTrue(new Numero(3).ehPrimo()); | |
assertFalse(new Numero(4).ehPrimo()); | |
assertTrue(new Numero(5).ehPrimo()); | |
assertFalse(new Numero(6).ehPrimo()); | |
assertTrue(new Numero(7).ehPrimo()); | |
assertFalse(new Numero(8).ehPrimo()); | |
assertFalse(new Numero(9).ehPrimo()); | |
} | |
@Test | |
public void respondeSeEPerfeito() { | |
assertFalse(new Numero(3).ehPerfeito()); | |
assertFalse(new Numero(4).ehPerfeito()); | |
assertFalse(new Numero(5).ehPerfeito()); | |
assertTrue(new Numero(6).ehPerfeito()); | |
assertFalse(new Numero(8).ehPerfeito()); | |
assertTrue(new Numero(28).ehPerfeito()); | |
assertFalse(new Numero(350).ehPerfeito()); | |
assertTrue(new Numero(496).ehPerfeito()); | |
assertFalse(new Numero(500).ehPerfeito()); | |
} | |
@Test | |
public void respondeSeEFeliz() { | |
assertTrue(new Numero(1).ehFeliz()); | |
assertFalse(new Numero(2).ehFeliz()); | |
assertFalse(new Numero(3).ehFeliz()); | |
assertFalse(new Numero(4).ehFeliz()); | |
assertFalse(new Numero(5).ehFeliz()); | |
assertFalse(new Numero(6).ehFeliz()); | |
assertTrue(new Numero(7).ehFeliz()); | |
assertFalse(new Numero(8).ehFeliz()); | |
assertFalse(new Numero(9).ehFeliz()); | |
assertTrue(new Numero(10).ehFeliz()); | |
assertFalse(new Numero(11).ehFeliz()); | |
assertFalse(new Numero(12).ehFeliz()); | |
assertTrue(new Numero(13).ehFeliz()); | |
assertFalse(new Numero(14).ehFeliz()); | |
assertFalse(new Numero(15).ehFeliz()); | |
assertFalse(new Numero(16).ehFeliz()); | |
assertFalse(new Numero(17).ehFeliz()); | |
assertFalse(new Numero(18).ehFeliz()); | |
assertTrue(new Numero(19).ehFeliz()); | |
assertFalse(new Numero(20).ehFeliz()); | |
} | |
} |
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 exercicio04; | |
public class Pessoa { | |
private int idade; | |
private double peso; | |
private double altura; | |
public Pessoa(int idade, double peso, double altura) { | |
this.idade = idade; | |
this.peso = peso; | |
this.altura = altura; | |
} | |
public void aniversario() { | |
this.idade++; | |
if (this.idade < 21) | |
this.altura += 0.015; | |
} | |
public void emagrecer() { | |
this.peso--; | |
} | |
public void engordar() { | |
this.peso++; | |
} | |
public int idade() { | |
return this.idade; | |
} | |
public double peso() { | |
return this.peso; | |
} | |
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 exercicio04; | |
import static org.junit.Assert.*; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class PessoaTest { | |
private Pessoa pessoa; | |
@Before | |
public void criarPessoa() { | |
pessoa = new Pessoa(18, 70, 1.70); | |
} | |
@Test | |
public void possuiIdadePesoEAltura() { | |
assertEquals(18, pessoa.idade()); | |
assertEquals(70, pessoa.peso(), 0); | |
assertEquals(1.70, pessoa.altura(), 0); | |
} | |
@Test | |
public void envelhece() { | |
pessoa.aniversario(); | |
assertEquals(19, pessoa.idade()); | |
} | |
@Test | |
public void engorda() { | |
pessoa.engordar(); | |
assertEquals(71, pessoa.peso(), 0); | |
} | |
@Test | |
public void emagrece() { | |
pessoa.emagrecer(); | |
assertEquals(69, pessoa.peso(), 0); | |
} | |
@Test | |
public void cresceAoEnvelhecerSeMenorDe21() { | |
pessoa.aniversario(); | |
assertEquals(1.715, pessoa.altura(), 0.0001); | |
assertEquals(19, pessoa.idade()); | |
pessoa.aniversario(); | |
assertEquals(1.73, pessoa.altura(), 0.0001); | |
assertEquals(20, pessoa.idade()); | |
pessoa.aniversario(); | |
assertEquals(1.73, pessoa.altura(), 0.0001); | |
assertEquals(21, pessoa.idade()); | |
} | |
} |
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 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(Ponto outro) { | |
return this.x == outro.x && this.y == outro.y; | |
} | |
*/ | |
public boolean equals(Object outro) { | |
Ponto outroPonto = (Ponto) outro; | |
return this.x == outroPonto.x && this.y == outroPonto.y; | |
} | |
} |
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 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); | |
} | |
} |
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 exercicio02; | |
public class Quadrado { | |
private double lado; | |
public Quadrado(double lado) { | |
validar(lado); | |
this.lado = lado; | |
} | |
public double area() { | |
return lado * lado; | |
} | |
public double getLado() { | |
return this.lado; | |
} | |
public void setLado(double novoLado) { | |
validar(novoLado); | |
this.lado = novoLado; | |
} | |
private void validar(double lado) { | |
if (lado <= 0) | |
throw new RuntimeException("Valor invalido para o 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
package exercicio02; | |
import static org.junit.Assert.*; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class QuadradoTest { | |
private Quadrado quadrado; | |
@Before | |
public void criarQuadrado() { | |
quadrado = new Quadrado(5.0); | |
} | |
@Test | |
public void possuiTamanhoDosLados() { | |
assertEquals(5.0, quadrado.getLado(), 0); | |
} | |
@Test | |
public void alteraTamanhoDosLados() { | |
quadrado.setLado(5.1); | |
assertEquals(5.1, quadrado.getLado(), 0); | |
} | |
@Test | |
public void calculaArea() { | |
assertEquals(25, quadrado.area(), 0); | |
} | |
@Test(expected=RuntimeException.class) | |
public void naoAceitaLadoZero() { | |
new Quadrado(0); | |
} | |
@Test(expected=RuntimeException.class) | |
public void naoAceitaAlterarLadoParaZero() { | |
quadrado.setLado(0); | |
} | |
@Test(expected=RuntimeException.class) | |
public void naoAceitaLadoNegativo() { | |
new Quadrado(-0.1); | |
} | |
@Test(expected=RuntimeException.class) | |
public void naoAceitaAlterarLadoParaNegativo() { | |
quadrado.setLado(-0.1); | |
} | |
} |
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 exercicio03; | |
public class Retangulo { | |
private double largura; | |
private double altura; | |
public Retangulo(double largura, double altura) { | |
this.setLargura(largura); | |
this.setAltura(altura); | |
} | |
public double area() { | |
return largura * altura; | |
} | |
public double perimetro() { | |
return largura * 2 + altura * 2; | |
} | |
public double getLargura() { | |
return largura; | |
} | |
public void setLargura(double largura) { | |
validar(largura); | |
this.largura = largura; | |
} | |
public double getAltura() { | |
return altura; | |
} | |
public void setAltura(double altura) { | |
validar(altura); | |
this.altura = altura; | |
} | |
private void validar(double medida) { | |
if (medida <= 0) | |
throw new RuntimeException("Valor inválido!"); | |
} | |
} |
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 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) | |
}; | |
} | |
public void setCentro(Ponto ponto) { | |
this.centro = ponto; | |
} | |
} |
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 exercicio03; | |
import static org.junit.Assert.*; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class RetanguloTest { | |
private Retangulo retangulo; | |
@Before | |
public void criarRetangulo() { | |
retangulo = new Retangulo(4, 3); | |
} | |
@Test | |
public void possuiLarguraEAltura() { | |
assertEquals(4, retangulo.getLargura(), 0); | |
assertEquals(3, retangulo.getAltura(), 0); | |
} | |
@Test | |
public void alteraLarguraEAltura() { | |
retangulo.setLargura(10); | |
assertEquals(10, retangulo.getLargura(), 0); | |
retangulo.setAltura(20); | |
assertEquals(20, retangulo.getAltura(), 0); | |
} | |
@Test | |
public void calculaArea() { | |
assertEquals(12, retangulo.area(), 0); | |
} | |
@Test | |
public void calculaPerimetro() { | |
assertEquals(14, retangulo.perimetro(), 0); | |
} | |
@Test(expected=RuntimeException.class) | |
public void naoAceitaLarguraZero() { | |
new Retangulo(0, 1); | |
} | |
@Test(expected=RuntimeException.class) | |
public void naoAceitaAlterarLarguraParaZero() { | |
retangulo.setLargura(0); | |
} | |
@Test(expected=RuntimeException.class) | |
public void naoAceitaLarguraNegativa() { | |
new Retangulo(-0.1, 1); | |
} | |
@Test(expected=RuntimeException.class) | |
public void naoAceitaAlterarLarguraParaNegativo() { | |
retangulo.setLargura(-0.1); | |
} | |
@Test(expected=RuntimeException.class) | |
public void naoAceitaAlturaZero() { | |
new Retangulo(1, 0); | |
} | |
@Test(expected=RuntimeException.class) | |
public void naoAceitaAlterarAlturaParaZero() { | |
retangulo.setAltura(0); | |
} | |
@Test(expected=RuntimeException.class) | |
public void naoAceitaAlturaNegativa() { | |
new Retangulo(1, -0.1); | |
} | |
@Test(expected=RuntimeException.class) | |
public void naoAceitaAlterarAlturaParaNegativo() { | |
retangulo.setAltura(-0.1); | |
} | |
} |
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 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()); | |
} | |
@Test | |
public void permiteAlterarCentro() { | |
Retangulo retangulo = new Retangulo(4, 2, new Ponto(10, 8)); | |
retangulo.setCentro(new Ponto(5, 5)); | |
assertArrayEquals( | |
new Ponto[] { | |
new Ponto(4, 3), | |
new Ponto(6, 3), | |
new Ponto(4, 7), | |
new Ponto(6, 7) | |
}, | |
retangulo.vertices()); | |
} | |
} |
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 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--; | |
} | |
} |
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 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()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment