Created
July 13, 2012 12:59
-
-
Save lrlucena/3104767 to your computer and use it in GitHub Desktop.
Aula de Classes e 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
class Aluno | |
attr :nome, :media, :situacao | |
attr_accessor :nota1, :nota2, :rec | |
def initialize(nome="Sem nome") | |
@nome = nome.to_s | |
@nota1 = nil # Primeiro bimestre | |
@nota2 = nil # Segundo bimestre | |
@rec = nil # Recuperacao | |
end | |
def nota1=(nota) | |
if (nota_valida(nota)) then | |
@nota1 = nota | |
end | |
end | |
def nota2=(nota) | |
if (nota_valida(nota)) then | |
@nota2 = nota | |
end | |
end | |
def rec=(nota) | |
if (nota_valida(nota)) then | |
@rec = nota | |
end | |
end | |
def media | |
m = (@nota1.to_f*2 + @nota2.to_f*3)/5 | |
if (m<6.0 and m>2.0 and @rec!=nil) then | |
return (m + @rec) / 2.0 | |
else | |
return m | |
end | |
end | |
def situacao | |
if (nota1==nil or nota2==nil) then | |
s = "Matriculado" | |
elsif (media>=6.0) then | |
s = "Aprovado" | |
elsif (media>=2.0 and rec==nil) then | |
s = "Em Recuperação" | |
else | |
s = "Reprovado" | |
end | |
return s | |
end | |
private | |
def nota_valida(nota) | |
if (nota.respond_to?('to_f')) then | |
(nota == nil) or (nota.to_f >= 0.0 and nota.to_f <= 10.0) | |
else | |
false | |
end | |
end | |
end | |
aluno = Aluno.new("Eu") | |
aluno.nota2 = 10 | |
aluno.nota1 = 11 | |
puts aluno.nota1 | |
puts aluno.media | |
puts aluno.situacao |
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 Dado | |
def rolar | |
return 1 + rand(6) | |
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
class Dado | |
attr_reader :mostrado | |
def rolar | |
@mostrado = 1 + rand(6) | |
return @mostrado | |
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
class Fracao | |
attr_reader :num, :den | |
def initialize(num, den) | |
m = mdc(num.to_i, den.to_i) | |
@num = num.to_i / m | |
@den = den.to_i / m | |
end | |
def to_f | |
@num.to_f / @den.to_f | |
end | |
def to_s | |
"#{@num}/#{@den}" | |
end | |
def *(outra) | |
num = @num * outra.num | |
den = @den * outra.den | |
Fracao.new(num,den) | |
end | |
def /(outra) | |
self * Fracao.new(outra.den, outra.num) | |
end | |
def +(outra) | |
num = @num * outra.den + outra.num * @den | |
den = @den * outra.den | |
Fracao.new(num, den) | |
end | |
def -(outra) | |
self + Fracao.new(-outra.num, outra.den) | |
end | |
private | |
def mdc(a,b) | |
if b==0 then a else mdc(b, a%b) end | |
end | |
end | |
f1 = Fracao.new(10,20) | |
f2 = Fracao.new(20,30) | |
f3 = f1.*(f2) | |
f4 = f1 * f2 | |
puts f1 | |
puts f2 | |
puts f3 | |
puts f4 |
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 Dado | |
attr_reader :mostrado | |
def rolar | |
@mostrado = 1 + rand(6) | |
return @mostrado | |
end | |
end | |
dado1 = Dado.new | |
dado2 = Dado.new | |
puts dado1.rolar | |
puts dado2.rolar | |
puts dado1.mostrado | |
puts dado1.mostrado | |
puts dado2.mostrado | |
dado1.rolar | |
puts dado1.mostrado |
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 Dado | |
attr_reader :mostrado | |
def initialize | |
rolar | |
end | |
def rolar | |
@mostrado = 1 + rand(6) | |
return @mostrado | |
end | |
end | |
dado1 = Dado.new | |
dado2 = Dado.new | |
puts dado1.rolar | |
puts dado2.rolar | |
puts dado1.mostrado | |
puts dado1.mostrado | |
puts dado2.mostrado | |
dado1.rolar | |
puts dado1.mostrado |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment