CSC120: Homework 2

Out: Tuesday October 3;
Due: Tuesday October 10;

Program 1

Write an applet that draws a checkerboard of red and blue squares. The number of squares, and their size, should be specified as static final int's. Also, the program should by recompiling:

Hint: You will need two for loops nested. Suppose you call the index variables of the for loops i and j. Make the color choice based on i and j general: write a method getMyColor taking two int's and returning a Color. Then by modifing the body of getMyColor you can answer all the sub-tasks simply.

Program 2

Write an applet that draws n-pointed stars. The size and centering of the star and the N should be defined as static final int's. You will need the math package rountines for sine and cosine:

       double x, sine_of_x;
       double y, cosine_of_y;
       sine_of_x = Math.sin(x) ;
       cosine_of_y = Math.cos(y) ;
A 5-pointed star can be generated by starting at angle 0.0 and incrementing by (2.0*Math.Pi)*2.0/5.0 each time through the loop. (Increment by (2.0*Math.Pi)/5.0 to draw a pentagon). Generalize these equations for a general NSIDES and SKIP so that your program can be recompiled to draw all these things:

Hint: You will need a single for loop and a double angle variable which starts out 0.0 and is incremented each time through the loop. The Graphics.drawLine() method takes 4 ints - the x,y start of the line and the x,y end of the line. You will need to keep tract of the old x,y as you pass through the loop calculating the new x,y. Then at the bottom of the loop you will copy the new x,y over to the old x,y for the next pass through the loop.