| 
       
         
          |  |  
              
 
  
    |   | Monday 31 May 2004 |  
  
    | 
        Chapter 4, Exercise 19 variant
        
	I thought the c4x19 solution might look better this way
(with print and create folded into the 2D array class),
but it gets me a null pointer crash in the compiler.
I guess I oughta download a more recent nightly build
and try it again.  Makes me leery about trying the 3D
version, though. 
(PS, I really did post the c4x19 one before heading out
for Tucson for the long weekend.  The Radio Userland
server(s?) seem to suck pretty bad a lot of the time, though.) 
 11:59:23 PM
class arDbl {
    var ar : Array[Double] = _;
    def length = ar.length;
    def apply( i: Int ) = ar(i);
    def update( i:Int, nv: Double) = ar.update(i, nv );
    def this( sz: Int ) = {
        this();
        ar= new Array[Double]( sz );
    }
};
class ar2D {
    var ard : Array[ arDbl ] = _;
    def length = ard.length; 
    def apply( i: Int ) = ard(i);
    def update( i:Int, j:Int, nv:Double ) = {
        ard(i).update(j, nv );
    }
    def this( rows:Int, cols: Int ) = {
        this();
        ard= new Array[ arDbl ]( rows );
        for( val j <- Iterator.range(0, rows) ) {
            ard(j) = new arDbl(cols);
        }
    }
    def this( rows:Int, cols: Int, start:Double, end:Double ) = {
        this(rows,cols);
        var current = start;
        var step = (end - start) / (rows * cols -1);
        for( val j <- Iterator.range( 0, rows ) ) {
            var curr_row = ard(j);
            for( val i <- Iterator.range( 0, cols ) ) {
                curr_row.update( i, current);
                current = current + step;
            }
        } 
    }
    def print = {
        var rows = ard.length;
        var cols = ard(0).length;
        for( val i <- Iterator.range( 0, rows ) ) {
            for( val j <- Iterator.range( 0, cols ) ) {
                Console.print( ard(i)(j) + " " );
            }
            Console.println("");
        }
    }
}
object c4x19 {
    def main (args: Array[String] ) = {
        Console.println("--> new ar2D( 3, 3, 1.0, 10.0 )" );
        val arm = new ar2D( 3, 3, 1.0, 10.0 );
        arm.print; 
        Console.println("");
        Console.println("--> new ar2D( 3, 4, 1.0, 20.0 )" );
        (new ar2D( 3, 4, 1.0, 20.0 )).print;
        Console.println("");
        Console.println("--> new ar2D( 5, 5, -10.0, 10.0 )" );
        (new ar2D( 5, 5, -10.0, 10.0 )) print
    }
}
   |  |  
 
              
              
             
 
               
                |   |   | © Copyright 
                  2007
                  Doug Landauer
                  . Last update: 
                  07/2/6; 12:40:05
                  .
 |   
                |   |  
 |  | 
    
       
       |