Created
November 25, 2011 23:43
-
-
Save rodrigomanhaes/1394691 to your computer and use it in GitHub Desktop.
Soluções feitas em aula para os exercícios de Programação Orientada a Objetos (Ruby)
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 < PRIMEIRO_CANAL || canal > ULTIMO_CANAL || !this.estaLigada()) | |
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.ligar(); | |
tv.trocarCanal(2); | |
assertEquals(2, tv.canal()); | |
} | |
@Test | |
public void canalSoPodeSerAlteradoDentroDeUmaFaixa() { | |
TV tv = new TV(); | |
tv.ligar(); | |
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()); | |
} | |
} |
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(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 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) | |
}; | |
} | |
} |
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()); | |
} | |
} |
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
class NumeroRacional | |
def initialize(numerador, denominador) | |
@numerador, @denominador = numerador, denominador | |
simplificar | |
end | |
attr_reader :numerador, :denominador | |
def somar(outro) | |
NumeroRacional.new(@numerador + outro.numerador, @denominador) | |
end | |
def ==(outro) | |
numerador == outro.numerador && denominador == outro.denominador | |
end | |
private | |
def simplificar | |
mdc = calcular_mdc(numerador, denominador) | |
@numerador = numerador / mdc | |
@denominador = denominador / mdc | |
end | |
def calcular_mdc(n1, n2) | |
menor = n1 < n2 ? n1 : n2 | |
menor.downto(1) do |divisor| | |
return divisor if n1 % divisor == 0 && n2 % divisor == 0 | |
end | |
end | |
end |
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
# coding: utf-8 | |
require './racional' | |
describe NumeroRacional do | |
it 'possui numerador e denominador' do | |
numero = NumeroRacional.new(1, 2) | |
numero.numerador.should == 1 | |
numero.denominador.should == 2 | |
end | |
it 'é armazenado na forma reduzida' do | |
numero = NumeroRacional.new(2, 4) | |
numero.numerador.should == 1 | |
numero.denominador.should == 2 | |
end | |
it 'realiza adição' do | |
numero = NumeroRacional.new(3, 5) | |
outro = NumeroRacional.new(1, 5) | |
soma = NumeroRacional.new(4, 5) | |
numero.somar(outro).should == soma | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment