CS 101 - Introduction to Computing

Spring 2010

Exam 2 Solutions

  1. d
  2. d
  3. d
  4. b
  5. b
  6. d
  7. a
  8. c
  9. c
  10. b
  11. a
  12. c
  13. a
  14. d
  15. c
  16. a
  17. c
  18. a
  19. d
  20. b

  21.  public static Picture q21 (Picture p1)
     {
       int wid = p1.getWidth();
       int hgt = p1.getHeight();
       Picture p2 = new Picture (wid, hgt);
       Pixel pix1, pix2;
       Color c;
       
       for (int x = 0; x < wid; x++)
       {
         for (int y = 0; y < hgt/2; y++)
            {
             pix1 = p1.getPixel(x, y);
             c = pix1.getColor();
             pix2 = p2.getPixel(x, y);
             pix2.setColor(c);
             pix2 = p2.getPixel(x, hgt - 1 - y);
             pix2.setColor(c);
            }
         } 
       return p2;
     }
    

  22.  public static Picture q22 (Picture p)
     {
       Pixel arr[] = p.getPixels();
       
       for (int i = 0; i < arr.length; i++)
       {
         Pixel pix = arr[i];
         int lum = (pix.getRed() + pix.getGreen() + pix.getBlue())/3;
         
         if (lum < 64)
           pix.setColor(Color.blue);
         else if (lum < 128)
           pix.setColor(Color.red);
         else if (lum < 192)
           pix.setColor(Color.green);
         else
           pix.setColor(Color.yellow);
       }
       return p;
     }
    

Exam 1 Solutions

The answer to the test questions are:
  1. a
  2. c
  3. b
  4. c
  5. b
  6. b
  7. a
  8. c
  9. c
  10. d
  11. c
  12. a
  13. b
  14. c
  15. c
  16. e
  17. c
  18. a
  19. a
  20. c

  21. World w = new World ();
    Turtle t = new Turtle (w);
    int len = SimpleInput.getIntNumber ("Enter an integer.");
    int i;
    
    t.setPenColor (Color.blue);
    for (i = 0; i<4; i++)
       {
        t.forward (len);
        t.turn (-90);  // making a left turn
       }
    
    t.penUp();
    t.turn(90);
    t.forward (len / 2);
    t.turn (-90);
    t.penDown();
    
    t.setPenColor(Color.red);
    for (i = 0; i<4; i++)
       {
        t.forward (len);
        t.turn (90);  // making a right turn
       }
    
    w.show();
    

  22. public static void drawShape (Turtle t, int len)
    {
      int i;
      int numOfSides = 9;
      int turnDegree = 360/numOfSides;  // the result is 40
    
      for (i = 0; i < numOfSides; i++)
        {
         t.forward (len);
         t.turn (turnDegree);
        }
    }