CPS 4 Summer 2002 : Quiz 5


Name: Grader: Score:

Syntax

  1. Consider the following code:
    public class Fred
    {
        double a = 2.87;
    
        public Fred ()
        {
            double b = 3.14;
            a = b;
        }
    
        public void MyFunction (double b)
        {
            a = b;
        }
    }

    What is the value of the instance variable a before and after Fred's MyFunction is called with the parameter value 0?
    Before:

    1. 0
    2. 3.14
    3. 2.87

    After:

    1. 0
    2. 3.14
    3. 2.87

Implementing Functions

  1. Write a function, named Max2, that takes two numbers and returns the one with the maximum value.
    public double Max2 (double a, double b)
    {
    
    }
  2. Write a function, named Max3, that takes three numbers and returns the one with the maximum value. Your implementation must call the function Max2 that you wrote above at least once and use its result in determining the result of this function.
    public double Max3 (double a, double b, double c)
    {
    
    }
  3. Write a function, named MakeSize, that takes three numbers and makes a GP.Attributes.Dimension object that represents the maximum value of the given numbers. Your implementation must call the function Max3 that you wrote above at least once and use its result in creating the size.
    public GP.Attributes.Dimension MakeSize (double a, double b, double c)
    {
    
    }

Flow of Control

  1. For the classes below, show the order in which each line is performed by the computer when the applet is created (i.e., not necessarily the order they are written on the page).
public class Applet extends GP.Containers.Applet
{
  public Applet ()
  {
a     MakeFallingOval();
b     MakeFallingHexagon();
  }

  public void MakeFallingHexagon ()
  {
c     GP.Shape s = new GP.Shapes.Hexagon();
d     s.SetPosition(new GP.Attributes.Coordinate(300, 0));
e     s.AddBehavior(new FallBehavior(s, 2));
  }

  public void MakeFallingOval ()
  {
f     GP.Shape s = new GP.Shapes.Oval();
g     s.SetPosition(new GP.Attributes.Coordinate(100, 0));
h     s.AddBehavior(new FallBehavior(s, 1));
  }
}
public class FallBehavior extends GP.Behaviors.Perpetual 
{
  GP.Shape myTarget;
  GP.Attributes.Vector myVelocity;

  public FallBehavior (GP.Shape target, double speed)
  {
i     myTarget = target;
j     myVelocity = new GP.Attributes.Vector(new GP.Attributes.Angle(-90),speed);
  }

  public void Step ()
  {
k     myTarget.Move(myVelocity);
  }
}
  1. It often helps when you are writing code to be able to hand simulate, or trace, what the computer is doing, i.e., check the results of each instruction by writing it on paper. This helps you to be able to find bugs in your programs and to better understand how the computer executes instructions. Hand simulate the recursive code below by showing when each function is called and the parameter values for each call made when the object is created with an initial size of 32.
public class Mystery
{
  public Mystery (double size)
  {
    MakeRecursively(200, 200, size, 1);
    MakeRecursively(200, 200, size, -1);
  }

  public void MakeRecursively(double cx, double cy, double size, int direction)
  {
    if (size > 2)
    {
      MakeCircle(cx, cy, size);
      MakeRecursively(cx + direction * 3 * size / 4, cy, size / 2, direction);
    }
  }

  public void MakeCircle(double cx, double cy, double size)
  {
    GP.Shapes.Oval o = new GP.Shapes.Oval();
    o.SetPosition(new GP.Attributes.Coordinate(cx, cy));
    o.SetSize(new GP.Attributes.Dimension(size));
  }
}

Check your Understanding

  1. Explain, briefly, why the purpose of the three different kinds of variables in Java: local, instance, and parameter.
     
     
     
  2. The following behavior is supposed to turn the given shape by steps of two pixels repeatedly until it gets to a certain angle. Although it compiles correctly, it has a logical error that prevents it from running properly. You must explain the logical error and how to fix it. You may note your fixes on the code below rather than rewriting it from scratch under each question.
    public class Spinner extends GP.Behaviors.Perpetual
    {
        GP.Shape myTarget;
    
        public Spinner (GP.Shape target)
        {
            myTarget = target;
            myTarget.SetOrientation(1);
        }
    
        public void Step ()
        {
            if (myTarget.GetOrientation().GetRoll() == 180)
            {
                Stop();
            }
            else
            {
                myTarget.Turn(2);
            }
        }
    }


Extra Credit

  1. Two cultures of aliens live on the planet Trekia, the carpals and the tarsals. The carpals always lie. The tarsals always tell the truth. A space traveler arrives on Trekia and meets a party of three aliens. She asks the aliens to which culture they belong. The first one murmurs something that is too soft to hear. The second replies, "It said it was a carpal." The third says to the second, "You are a liar!" From this information, figure out what culture the third alien belongs to.