Chapter 7, Exercise 13
Exercise 13: Following the example in Transmogrify.java,
create a Starship class containing an AlertStatus reference
that can indicate three different states. Include methods to
change the states.
package x;
abstract class AlertStatus { def Alert : Unit; }
class RedAlert extends AlertStatus { def Alert = W.rn( "Carnation" ); }
class OrangeAlert extends AlertStatus { def Alert = W.rn( "Navel" ); }
class YellowAlert extends AlertStatus { def Alert = W.rn( "Goldenrod" ); }
class Starship {
var buzz : AlertStatus = new YellowAlert;
def cryWolf = { buzz = new OrangeAlert; }
def cryBlitzer = { buzz = new RedAlert; }
def lookAlert = buzz.Alert;
}
object c7xH {
def main ( args: Array[String] ) = {
var quark = new Starship;
quark.lookAlert;
quark.cryWolf;
quark.lookAlert;
quark.cryBlitzer;
quark.lookAlert;
}
}
I suppose that idiomatic Scala would use an algebraic
type (a.k.a. a few case classes) here, instead of the
more purely object oriented approach seen here.
1:45:47 AM