Last active
October 7, 2015 04:27
-
-
Save lrlucena/3105752 to your computer and use it in GitHub Desktop.
Gabarito da Lista de Exercícios 12
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
# Escreva uma classe, chamada Ponto, que representa um ponto no | |
# plano cartesiano. A figura abaixo mostra quais atributos e métodos | |
# da classe. | |
class Ponto | |
attr_reader :x, :y | |
def initialize(x,y) | |
@x = x | |
@y = y | |
end | |
def distancia_para(outro) | |
x = (@x - outro.x) | |
y = (@y - outro.y) | |
Math.sqrt(x*x + y * y) | |
end | |
def distancia_origem | |
distancia_para(Ponto.new(0,0)) | |
end | |
end |
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
# Escreva uma classe, chamada Ponto, que representa um ponto no | |
# plano cartesiano. A figura abaixo mostra quais atributos e métodos | |
# da classe. | |
class Ponto | |
attr_reader :x, :y | |
def initialize(x,y) | |
@x = x | |
@y = y | |
end | |
def distancia_para(outro) | |
x = (@x - outro.x) | |
y = (@y - outro.y) | |
Math.sqrt(x*x + y * y) | |
end | |
def distancia_origem | |
distancia_para(Ponto.new(0,0)) | |
end | |
end | |
puts "Digite as coordenadas do primeiro ponto" | |
p1 = Ponto.new(gets.to_f, gets.to_f) | |
puts "A distância do ponto até a origem é #{p1.distancia_origem}" | |
puts "Digite as coordenadas do segundo ponto" | |
p2 = Ponto.new(gets.to_f, gets.to_f) | |
puts "A distância do ponto até a origem é #{p1.distancia_para(p2)}" |
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
class Triangulo | |
attr :p1, :p2, :p3, :perimetro | |
def initialize(p1, p2, p3) | |
@p1 = p1 | |
@p2 = p2 | |
@p3 = p3 | |
@lado1 = p1.distancia_para(p2).round(3) | |
@lado2 = p2.distancia_para(p3).round(3) | |
@lado3 = p3.distancia_para(p1).round(3) | |
end | |
def is_equilatero? | |
(@lado1==@lado2) and (@lado2==@lado3) | |
end | |
def is_isosceles? | |
not is_equilatero? and (@lado1==@lado2 or @lado2==@lado3 or @lado1==@lado3) | |
end | |
def is_escaleno? | |
not is_equilatero? and not is_isosceles? | |
end | |
def perimetro | |
(@lado1 + @lado2 + @lado3).round(3) | |
end | |
end |
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
#Escreva um programa que leia três coordenadas, verifique se as | |
#mesmas formam um triângulo e construam o objeto da classe | |
#triângulo. Por fim o programa mostra o tipo do triângulo: Escaleno, | |
#Isósceles ou Equilátero. | |
p1 = Ponto.new(0, 0) | |
p2 = Ponto.new(5,0) | |
p3 = Ponto.new(0,5) | |
t = Triangulo.new(p1, p2, p3) | |
if t.is_equilatero? then | |
puts "Triângulo Equilátero" | |
end | |
if t.is_isosceles? then | |
puts "Triângulo Isosceles" | |
end | |
if t.is_escaleno? then | |
puts "Triângulo Escaleno" | |
end | |
puts t.perimetro |
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
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) | |
@nota1 = nota if (nota_valida(nota)) | |
end | |
def nota2=(nota) | |
@nota2 = nota if (nota_valida(nota)) | |
end | |
def rec=(nota) | |
@rec = nota if (nota_valida(nota)) | |
end | |
def media | |
mp = (@nota1.to_f*2 + @nota2.to_f*3)/5 | |
if (mp<6.0 and mp>2.0 and @rec!=nil) then | |
mf = (mp + @rec) / 2.0 | |
else | |
mf = mp | |
end | |
return mf.round(1) | |
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 |
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
puts 40.times | |
.map{gets.chomp} | |
.map{|linha| | |
nome, nota1, nota2, rec = linha.split(':') | |
aluno = Aluno.new(nome) | |
aluno.nota1 = nota1.to_f | |
aluno.nota2 = nota2.to_f | |
aluno.rec = rec.to_f | |
[aluno.nome, aluno.nota1, aluno.nota2, aluno.rec, aluno.media, aluno.situacao].join(':') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment