CS 140 - Homework 5 - Due Feb 6

Name:______________________________________

On Feb 11 we'll have 30-minute quiz. This homework is to help you review the techniques we've learned so far to help prepare for the quiz. On Wednesday we'll have a review day, no lab. We'll go over this HW and do some problems out of the book.

Answer the following questions:

  1. In the following code segments circle all the types you see:
          World w = new World();
          Turtle t = new Turtle(w);
          int d = 100;
          for (int i = 0; i < 100; i++) {
             t.forward(d);
             t.turn(25);
          }
        
  2. Now circle all the classes you see (in this and later questions, you may end up re-circling some of the things you circled in previous questions).
          World w = new World();
          Turtle t = new Turtle(w);
          int d = 100;
          for (int i = 0; i < 100; i++) {
             t.forward(d);
             t.turn(25);
          }
    						
  3. Circle all the primitive variables:
          World w = new World();
          Turtle t = new Turtle(w);
          int d = 100;
          for (int i = 0; i < 100; i++) {
             t.forward(d);
             t.turn(25);
          }
    						
  4. Circle all the object references:
          World w = new World();
          Turtle t = new Turtle(w);
          int d = 100;
          for (int i = 0; i < 100; i++) {
             t.forward(d);
             t.turn(25);
          }
        
  5. Circle all the objects:
          World w = new World();
          Turtle t = new Turtle(w);
          int d = 100;
          for (int i = 0; i < 100; i++) {
             t.forward(d);
             t.turn(25);
          }
         
  6. Circle all the methods:
          World w = new World();
          Turtle t = new Turtle(w);
          int d = 100;
          for (int i = 0; i < 100; i++) {
             t.forward(d);
             t.turn(25);
          }
         
  7. Circle all the parameters:
          World w = new World();
          Turtle t = new Turtle(w);
          int d = 100;
          for (int i = 0; i < 100; i++) {
             t.forward(d);
             t.turn(25);
          }
    					
  8. What is the maximum value a color component in a pixel can have?
  9. What are the RGB components for each of the following colors?
          Color 	   R 	        G 	         B
          
          Black 	  	  	 
          
          White
    						 	  	  	 
          Blue 	  	  	 
       
  10. What is the location of the top-left pixel in a Java Picture class?
  11. Suppose we have a picture which measures 3x2 pixels with the following RGB values:
          (10,20,0)   (40,255,100)  (50,50,50)
          (0,10,142)  (80,24,129)   (28,88,188)
    				

    Consider the following Picture method:

          void swapColors() {
             Pixel[] pixelArray = this.getPixels();
             for (Pixel p : pixelArray)  {
                int r = p.getRed();
                int g = p.getGreen();
                int b = p.getBlue();
                p.setColor(new Color(g,b,r));
             }
           }
        

    In the following table indicate the value of the pixels after the swapColors method is called on the above picture.

  12. Examine the following code:
          Turtle t1 = new Turtle(new World());
          Turtle t2 = t1;
          t1.forward(100);
          t2.turn(90);
    				

    What happens with the above code? How many Turtle objects exist in the above code? Explain why.