CPS 4 Summer 2002 : Quiz 4


Name: Grader: Score:

Syntax

  1. For each function declared below, explain the meaning or purpose of each word in the signature as it relates to declaring the method.
    public void SetPosition (GP.Attributes.Coordinate pos)
    
    
    
    
    private double CalculateTotal (double a, double b, int c)
     
    
    
    
    
  2. Explain, briefly, at least two syntactic differences between a class' constructor and any other function.






Conditionals

  1. What value is assigned to the variable fee by the conditional statements below when the driver's speed is set to 74?
    int fee = 0;
    if (speed > 35)
    {
        fee = 20;
    }
    if (speed > 50)
    {
        fee = 40;
    }
    if (speed > 75)
    {
        fee = 60;
    }
  2. What value is assigned to the variable fee by the conditional statements below when the driver's speed is set to 74?
    int fee = 0;
    if (speed > 75)
    {
        fee = 60;
    }
    if (speed > 50)
    {
        fee = 40;
    }
    if (speed > 35)
    {
        fee = 20;
    }
  3. What value is assigned to the variable fee by the conditional statements below when the driver's speed is set to 74?
    int fee = 0;
    if (speed > 75)
    {
        fee = 60;
    }
    else if (speed > 50)
    {
        fee = 40;
    }
    else if (speed > 35)
    {
        fee = 20;
    }

Loops

  1. For the class below, show the order in which each line is performed by the computer when the class is created (not necessarily the order they are written on the page).
      public class StopLight extends GP.Shape
      {
        public StopLight (double lightSize)
        {
    a     MakeBackground(lightSize);
    
    b     int count = 0;
    c     while (count < 3)
          {
    e       MakeLight(count, lightSize);
    f       count = count + 1;
          }
        }
    
        public void MakeLight (int n, double sz)
        {
    g     GP.Shape light = new GP.Shapes.Oval();
    h     light.SetPosition(new GP.Attributes.Coordinate(0, (n - 1) * sz));
    i     light.SetSize(new GP.Attributes.Dimension(size, size));
    j     light.SetColor(ChooseColor(n));
        }
    
        public GP.Attributes.Color ChooseColor (int number)
        {
    k     GP.Attributes.Color result;
    
    l     if (number == 0)
          {
    m       result = new GP.Attributes.Colors.Red();
          }
    n     else if (number == 1)
          {
    o       result = new GP.Attributes.Colors.Yellow();
          }
    p     else
          {
    q       result = new GP.Attributes.Colors.Green();
          }
    
    r     return result;
        }
    
        public void MakeBackground (double lightSize)
        {
    s     GP.Shape bg = new GP.Shapes.Rectangle();
    t     bg.SetSize(new GP.Attributes.Dimension(lightSize, 3 * lightSize));
    u     bg.SetColor(new GP.Attributes.Colors.White());
        }
      }
    
     
  2. 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. In order to hand simulate the code below, you will write down the value of each variable after each lettered instruction is executed.
    Line

    Code

    num

    result

    rem

     
    public class Mystery
     
     
     
     
    {
     
     
     
     
       public Mystery ()
     
     
     
     
       { 
     
     
     
    a
          int num = 9876;
    
     
     
     
    b
          int result = num / 10;
    
     
     
     
    c
          int rem = num % 10;
    
     
     
     
     
          if (num < 0) 
     
     
     
     
          {
     
     
     
    d
             num = -num; 
    
     
     
     
     
          }
     
     
     
     
          while (num != 0) 
     
     
     
     
          {
     
     
     
    e
             result = (result * 10) + rem;
    
     
     
     
    f
             num = num / 10;
    
     
     
     
    g
             rem = num % 10;
    
                 
                 
     
     
          }
     
     
              
     
       }
     
     
     
     
    }
     
     
     

    Explain the purpose of the Mystery function.

     



     

  3. The following class is supposed to make a circle that is divided into a given number of equally sized pie sections (like a perfectly cut pizza). 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. Note your fixes on the code below rather than rewriting it from scratch under the question.
    public class Wedgie
    {
        public Wedgie (int numWedges)
        {
            final double END = 359.99999;
            double angle = END / numWedges;
    
            double start = 0;
            while (start <= END)
            {
                MakeWedge(start, start + angle);
                start = start + angle;
            }
        }
    
        public void MakeWedge (double start, double end)
        {
            GP.Shapes.Pie p = new GP.Shapes.Pie();
            p.SetSize(new GP.Attributes.Dimension(400, 400));
            p.SetPosition(new GP.Attributes.Coordinate(200, 200));
            p.SetStartAngle(new GP.Attributes.Angle(start));
            p.SetEndAngle(new GP.Attributes.Angle(end));
        }
    }
    



Extra Credit

  1. What are the next two entries for the following sequence of numbers? What is a general formula for computing numbers in this sequence?
  2. 0, 1, -1, 2, -3, 5, -8, 13, -21, ?, ? ...