GIGO: words unreadable aloud
Mishrogo Weedapeval
 

 

  Tuesday 23 November 2004
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   comment/     



Click here to visit the Radio UserLand website. Click to see the XML version of this web page. © Copyright 2007 Doug Landauer .
Last update: 07/2/6; 12:42:43 .
Click here to send an email to the editor of this weblog.

November 2004
Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        
Oct   Dec

Previous/Next