Worksheet4: Programming in Java

For our lab, we are going to draw Pascal's triangle as a triangle of colored circles.

First we need to create the array and fill in the values of the Pascal numbers mod N, for some N. Here is shown the notation for two for loops that will loop over both the rows and columns of the array. You are to fill in the part that defines the array values. You will need to use if statements to make the cases on the values of j:

Assume that we have R rows and that we are given the modulus N.

for ( i = 0; i < R; i++ )
{ for ( j = 0; j <= i ; j++ )
  {  
    if  ( j = 0 )  a[i][j] =                                                     ;

    else  if

    else  if
  }
}

Now we need to define how to draw each circle to represent one Pascal number, arranged to form the traditional triangle shape. Assume that we have a grapics coordinate system where pixels range from 0 to Width from left to right, and from 0 to Height from top to bottom. Here is shown the notation for two "for" loops that will loop over the rows and columns of the array. You are also given the statements that set the color and draw the circle (oval). You are to fill in the part that defines pixel values x and y where the upper left corner of each circle should be drawn.

Assume that Size is the size of the circle in pixels and Gap can be the number of
pixels inbetween the rows.

     for ( int i = 0; i < R; i++)
     {          
         for ( int j = 0; j <= i; j++ )
         {
         
            x = 
            
            y = 
            
            // set the drawing color
             if ( a [ i ] [ j ] == 0 )  {  g.setColor ( firstcolor ); }
                 else   { g.setColor ( secondcolor ); }
             g.fillOval ( x, y, Size,Size ) ;
             g.setColor ( Color.black );
             s = String.valueOf(a[i][j]);
             sx = (Size - fm.stringWidth(s))/2;
             sy = ((Size - fm.getHeight())/2) + fm.getAscent() + fm.getLeading();
             g.drawString ( s , x + sx, y + sy ) ;
           }
        }