CPS 4 Summer 2003 : Quiz 6


Name: Grader: Score:

Syntax

  1. Explain, briefly, the purpose java keyword extends.







  2. None of the following functions are called directly from code you write. Explain when each function is called:



Understanding Attributes

  1. Assume two GP.Attributes.Vectors, one, v, with a direction of 90 degrees and a speed of 10 and the other, u, with direction of 180 degrees and speed 30. For each of the following statements, what is the direction and speed of v after it is executed? Assume each statement is executed independently, not sequentially, i.e., do not calculate the cumulative effect of the statements.
    v.SetSpeed(40);
    direction = 
    speed = 
     
    v.Turn(new GP.Attributes.Angle(45));
    direction = 
    speed = 
     
    v.Turn(u.GetDirection().Difference(v.GetDirection()));
    direction = 
    speed = 
     
    v.Accelerate(u.GetSpeed() - v.GetSpeed());
    direction = 
    speed = 
     
     
  2. Now assume that shape is a GP.Shape with a position of (200, 200) and an orientation of 180. For each of the following statements, what is the position and orientation of shape after it has been executed? Assume each statement is executed independently, not sequentially, i.e., do not calculate the cumulative effect of the statements. Assume, v starts with the same speed and direction as above.
    shape.Move(v);
    position = 
    orientation = 
     
    shape.Turn(v.GetDirection());
    position = 
    orientation = 
     
    shape.SetOrientation(v.GetDirection());
    position = 
    orientation = 
     

Flow of Control With Inheritance

  1. For the classes below, show the order in which each line is performed by the computer when the class is created (i.e., not necessarily the order they are written on the page).
      public class CoolLight
      {
          public CoolLight (double lightSize)
          {
    a         int count = 0;
    b         while (count < 3)
              {
    c             MakeLight(count, lightSize);
    d             count = count + 1;
              }
          }
    
          public void MakeLight (int number, double size)
          {
    e         CoolShape light = ChooseShape(number);
    f         light.SetPosition(new GP.Attributes.Coordinate(0, number * size));
    g         light.SetSize(new GP.Attributes.Dimension(size, size));
    h         light.SetStuff();
          }
    
          public CoolShape ChooseShape (int number)
          {
    i         CoolShape result;
    
    j         if (number == 0)
              {
    k             result = new CoolShape();
              }
    l         else if (number == 1)
              {
    m             result = new CoolerShape();
              }
    n         else
              {
    o             result = new CoolestShape();
              }
    
    p         return result;
          }
      }
      public class CoolShape extends GP.Shapes.Hexagon
      {
          public CoolShape ()
          {
    q         SetColor(new GP.Attributes.Colors.Red());
          }
    
          public void SetStuff ()
          {
    r         SetFill(new GP.Attributes.Fills.None());
          }
      }
      public class CoolerShape extends CoolShape
      {
          public CoolerShape ()
          {
    s         SetColor(new GP.Attributes.Colors.Yellow());
          }
    
          public void SetStuff ()
          {
    t         SetOrientation(new GP.Attributes.Angle(45));
          }
      }
      public class CoolestShape extends CoolShape
      {
          public CoolestShape ()
          {
    u         SetColor(new GP.Attributes.Colors.Green());
          }
    
          public void SetStuff ()
          {
    v         Scale(1.05, 1.05);
          }
      }

Debugging

  1. The following behavior is supposed to shrink the given shape by the given percentage repeatedly until it gets too small to see. Although it compiles correctly, it has a logical error that prevents it from running properly.
public class Shrinker extends GP.Behaviors.Perpetual
{
    GP.Shape myTarget;
    double percent;

    public Shrinker (GP.Shape target, double percent)
    {
        myTarget = target;
    }

    public void Step ()
    {
        if (myTarget.GetSize().GetWidth() > 1)
        {
            myTarget.Scale(percent / 100.0, percent / 100.0);
        }
    }
}

When given a target that is a triangle and percent of 99.0, instead of shrinking the triangle incrementally one percent at a time, it makes the triangle disappear after only one call to Step. What is the error that causes the size to be reduced so drastically and how could you fix it? You may note your fixes on the code below rather than rewriting it from scratch under each question.



Extra Credit

In the forest there is a cabin. All the doors and windows are sealed off and everyone inside of the cabin is dead. How did all the people die?