CPS 4 Spring 2002 : Exam 3 Review


Collections

  1. Explain what casting is and why it is necessary to cast the value returned from a collection in the following statement:
    ((GP.Shape)myShapes.GetRandomElement()).SetColor(new GP.Attributes.Colors.Random());


  2. Explain the purpose of using a collection in the Blinker example.


  3. Explain the purpose of using a collection in the Nibble example.


  4. What is the difference in your whack-a-mole program if your collection remembers the holes or the moles? Make a case for storing one over the other.


Understanding the Examples

Blinker

  1. Explain the purpose of the Starter class.


  2. Explain the purpose of the GetShapes method of the TriBoard class.


  3. Explain how the flash behavior works. In particular, consider what would happen if it were run twice on the same shape --- both starting at the same time? one starting a half-second later? one starting a full second later?


  4. Explain the possible consequences of changing the delay in the flash behavior from 0.8 seconds to 1.5 seconds. Does the number of shapes in the board make any difference?


Nibble

  1. Explain the purpose of the Reset class.


  2. Explain how the GrowTail function of the Snake class works. How does it differ between the basic and optimized versions of the program?


  3. Explain how the HideTail function of the Snake class works.


  4. Explain how the Reset function of the Snake class works. In particular, how does one decide what to put in the Snake's constructor instead of the Reset function?

Combining Behaviors

Consider the following program that demonstrates a ball bouncing around within an applet by adding three small behaviors to a single shape rather than using one more complex behavior (as most of you did in Pong):

Explain how this version works. Is one easier to understand than the other? In particular, what difference does the order the behaviors are added matter or whether each behavior shares the same GP.Attributes.Vector or creates its own separate copy.

 
 
Reactions

  1. Write a reaction class that randomly changes the color of the given target shape when the mouse is clicked within the shape.
    public class ColorRandomly extends GP.Reactions.MouseClicked
    {
    
    
        public ColorRandomly (GP.Shape target)
        {
    
    
        }
    
        public void React ()
        {
    
    
        }
    }
  2. Write a reaction class that counts how many times the mouse has been clicked within a shape using the given GP.Attributes.Values.Counter to keep track of the number of clicks rather than an int or a double.
    public class Counter extends GP.Reactions.MouseClicked
    {
    
    
        public Counter (GP.Attributes.Values.Counter count)
        {
    
    
        }
    
        public void React ()
        {
    
    
        }
    }
    
  3. Write a reaction that changes the color of the given target shape when the mouse is clicked within it based on a sequence of two colors given within a collection. The first time the mouse is clicked within the shape, it should turn the first color in the collection, the next time it should turn the last color in the collection, then it should turn the first color again, and on and on. In other words, it should alternate colors when clicked.
    public class ColorAlternately extends GP.Reactions.MouseClicked
    {
    
    
        public ColorAlternately (GP.Shape target, GP.Collections.Sequence twoColors)
        {
    
        
        }
        
        public void React ()
        {
    
    
    
    
    
    
    
    
        }
    }
    
     
  4. Write a reaction class that changes the color of the given target shape when the mouse is clicked within the shape based on a given sequence of colors that has as few as two colors and as many as twelve colors. Specifically, the first time the mouse is clicked within the shape, it should turn the color of the first one in the collection, the next time it should turn the second color, and so on. When all the colors in the collection have been displayed, the cycle should start again from the first color in the collection.
    public class ColorSequentially extends GP.Reactions.MouseClicked
    {
    
    
        public ColorSequentially (GP.Shape target, GP.Collections.Sequence colors)
        {
    
    
    
        }
    
        public void React ()
        {
    
    
    
    
    
    
    
    
    
    
        }
    }