GIGO: words unreadable aloud
Mishrogo Weedapeval
 

 

  Sunday 1 August 2004
Chapter 7, Exercise 1, Thinking in Scala

Exercise 1 asks: Add a new method in the base class of Shapes.java that prints a message, but dont override it in the derived classes. Explain what happens. Now override it in one of the derived classes but not the others, and see what happens. Finally, override it in all the derived classes.

Ok, I translated Shapes.java into scala. I added the extra method. First explanation: Ummm, nothing happens differently at all if you don't also call the new method. So I did. Explanations: Omit the derived class roll methods, and they all roll using the Shape.roll. Omit only the Triangle.roll override (they don't roll so good anyway) and the Triangles end up rolling using the Shape.roll, while the others roll in their own peculiar ways.

// c7xShapes.scala ... Java-style Polymorphism in scala.
package x;
trait Shape {
    def draw : unit = ();
    def erase : unit = ();
    def roll : unit = W.rn( "Shape.roll" );
}
class Circle with Shape {
    override def draw : unit = W.rn( "Circle.draw" );
    override def erase : unit = W.rn( "Circle.erase");
    override def roll : unit = W.rn( "Circle.roll smoothly");
} 
class Square with Shape {
    override def draw : unit = W.rn( "Square.draw" );
    override def erase : unit = W.rn( "Square.erase");
    override def roll : unit = W.rn( "Square.roll klunk klunk klunk klunk");
} 
class Triangle with Shape {
    override def draw : unit = W.rn( "Triangle.draw" );
    override def erase : unit = W.rn( "Triangle.erase");
    override def roll : unit = W.rn( "Triangle.roll klank klank klank");
} 
// A "factory" that randomly creates shapes:
class RandomShapeGenerator {
    private var rand = new java.util.Random();
    def next : Shape = {
        rand.nextInt(3) match {
         case 0 => new Circle;
         case 1 => new Square;;
         case 2 => new Triangle;
        }
    }
} 
object Shapes {
    private var gen = new RandomShapeGenerator;
    def main ( args: Array[String] ) = {
        var s = new Array[Shape]( 9 );
        for( val i <- Iterator.range(0, s.length) )
            s(i) = gen.next;
        for( val i <- Iterator.range(0, s.length) )
            s(i).draw;
        for( val i <- Iterator.range(0, s.length) )
            s(i).roll;
        /* One of these days, I ought to write a Scala
           equivalent of Bruce's Test/monitor classes. */
    }
}

Oh, yeah, I guess I never really made it explicit that I use a small abbreviation for all of these expository printout messages. Here's W.scala:

package x;
object W {
    def r ( s: String ) = Console.print( s );
    def rn ( s: String ) = Console.println( s );
}

10:19:00 PM   comment/     
Thinking in Scala, August 1

Hit the summer doldrums. Busy times at work. Off to Montana on Wednesday for a well-deserved, much-needed, too-short, bittersweet vacation. Here's the usual info for the first posting of the month:

I've been using

I do kinda see reasons (aside from getting a job) why Larry might have slowed down in the middle of Chapter 6. There are several exercises that are pretty specific to Java's private/protected/package access controls, which fine distinctions are shared by neither Common Lisp nor Scala.
9:36:41 PM   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:41:32 .
Click here to send an email to the editor of this weblog.

August 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 31        
Jul   Sep

Previous/Next