Last active
April 26, 2016 13:43
-
-
Save lrlucena/c83002d8a5c5a3ec1b258f947832b15b to your computer and use it in GitHub Desktop.
POO com Scala - Lista 08
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
/* | |
* Funcionário usando classe | |
*/ | |
trait Pessoa { | |
def nome: String | |
} | |
class Funcionario(val nome: String, _salario: Double) extends Pessoa { | |
def salario = _salario | |
} | |
class Vendedor(nome: String, _salario: Double, adicional: Double) extends Funcionario(nome, _salario){ | |
override def salario = _salario + adicional | |
} | |
class Gerente(nome: String, _salario: Double, numFuncionarios: Int) extends Funcionario(nome, _salario) { | |
override def salario = _salario * (1 + numFuncionarios / 10.0) | |
} | |
object Questao01 extends App { | |
val a = new Vendedor("Joao", 10000.00, 100.0) | |
println(a.nome) | |
println(a.salario) | |
println(s"O salário de ${a.nome} é ${a.salario}.") | |
} |
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
/* | |
* Funcionário usando trait | |
*/ | |
trait Pessoa { | |
val nome: String | |
} | |
trait Funcionario extends Pessoa { | |
def salario: Double | |
} | |
class Vendedor(val nome: String, _salario: Double, adicional: Double) extends Funcionario{ | |
override def salario = _salario + adicional | |
} | |
class Gerente(val nome: String, _salario: Double, numFuncionarios: Int) extends Funcionario { | |
override def salario = _salario * (1 + numFuncionarios / 10.0) | |
} | |
object Questao01 extends App { | |
val a = new Vendedor("Joao", 10000.00, 100.0) | |
println(a.nome) | |
println(a.salario) | |
println(s"O salário de ${a.nome} é ${a.salario}.") | |
} |
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
trait Figura3D { | |
def volume: Double | |
} | |
class Esfera(raio: Double) { | |
def volume = 4 * Math.PI * raio * raio * raio / 3 | |
} | |
class Cubo(aresta: Double) { | |
def volume = aresta * aresta * aresta | |
} | |
object Questao02 extends App { | |
val entrada = """Esfera:2.5 | |
|Esfera:3.6 | |
|Cubo:1.0 | |
|Cubo:1.5""".stripMargin | |
val linhas = entrada.split('\n') | |
for (linha <- linhas) { | |
val a = linha.split(':') | |
val tipo = a(0) | |
val valor = a(1).toDouble | |
tipo match { | |
case "Esfera" => println(tipo + " -> " + new Esfera(valor).volume) | |
case "Cubo" => println(tipo + " -> " + new Cubo(valor).volume) | |
case _ => | |
} | |
} | |
} |
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
trait Notas { | |
protected val notas = new Array[Double](3) | |
def apply(n: Int) = notas(n - 1) | |
def update(n: Int, valor: Double) = { notas(n - 1) = valor } | |
} | |
class Disciplina(val nome: String, val semestre: String) extends Notas { | |
private def mediaParcial = (this(1) * 2 + this(2) * 3) / 5.0 | |
def media: Double = if (mediaParcial >= 6.0) mediaParcial else (mediaParcial + this(3)) / 2.0 | |
def situacao: String = if (media > 6.0) "Aprovado" else "Reprovado" | |
override def toString = s"${nome} (${semestre}): ${notas.takeWhile { 0 < _ }.mkString(", ")}" | |
} | |
class Boletim { | |
private var disciplinas = List[Disciplina]() | |
def inserir(disciplina: Disciplina) = { | |
disciplinas = disciplina :: disciplinas | |
} | |
def remover(n: Int) = { | |
disciplinas = disciplinas.take(n) ++ disciplinas.drop(n + 1) | |
} | |
def apply(n: Int) = disciplinas(n) | |
def tamanho = disciplinas.size | |
override def toString = s"Boletim\n" + disciplinas.mkString("\n") | |
} | |
object Questao03 extends App { | |
val disc = new Disciplina("Programação de Computadores", "2016.1") | |
disc(1) = 10.0 | |
disc(2) = 9.5 | |
println(disc.media) | |
val boletim = new Boletim() | |
boletim.inserir(disc) | |
println(boletim) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment