added assignment 9 ex 1
This commit is contained in:
parent
c13f9851e3
commit
43e0694d9d
29
src/Assignment9/Assignment9.scala
Normal file
29
src/Assignment9/Assignment9.scala
Normal file
@ -0,0 +1,29 @@
|
||||
import Assignment9.Kelvin.kel2cel
|
||||
|
||||
import scala.language.implicitConversions
|
||||
|
||||
|
||||
package object Assignment9 {
|
||||
sealed trait Temperature {
|
||||
}
|
||||
|
||||
object Temperature {
|
||||
implicit def cel2kel(celsius: Celsius): Kelvin = new Kelvin(celsius.value + 273.15)
|
||||
implicit def kel2cel(kelvin: Kelvin): Celsius = new Celsius(kelvin.value - 273.15)
|
||||
}
|
||||
|
||||
case class Celsius(value: Double) extends Temperature {
|
||||
override def toString: String = s"$value°C"
|
||||
}
|
||||
object Celsius {
|
||||
implicit def val2cel(value: Double): Celsius = new Celsius(value)
|
||||
}
|
||||
|
||||
case class Kelvin(value: Double) extends Temperature {
|
||||
override def toString: String = s"$value K"
|
||||
}
|
||||
object Kelvin {
|
||||
implicit def kel2cel(value: Double): Kelvin = new Kelvin(value)
|
||||
|
||||
}
|
||||
}
|
16
src/Assignment9/Ex1.scala
Normal file
16
src/Assignment9/Ex1.scala
Normal file
@ -0,0 +1,16 @@
|
||||
package Assignment9
|
||||
|
||||
import scala.language.implicitConversions
|
||||
|
||||
object Ex1 extends App {
|
||||
val a: Celsius = 30
|
||||
val b: Kelvin = 30
|
||||
val c: Kelvin = Celsius(10)
|
||||
val d: Celsius = c
|
||||
val e: Temperature = d
|
||||
|
||||
println(a) // Should print "30°C"
|
||||
println(b) // Should print "30 K"
|
||||
|
||||
println()
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user