Lab 10 Pre-Lab Assignment

  1. Constructors are a special type of method included in a class definition.
    1. When is a constructor executed?

       

       

    2. What are the two ways in which a constructor differs from a regular method?

       

       

  2. Often a class has more than one constructor - that is, the constructor method is overloaded. The Coin class in the textbook (pages 214-215) has one constructor that takes no parameter and initializes the Coin object to a random value (by calling the flip method). Write a second constructor method that takes one String parameter (whose value should be "heads" or "tails") and initializes the Coin object to the value specified by the parameter. The method should print an error message if the parameter is not valid and the object should be given a default value of heads.
      ___________________________________________________________ // header
            
      {
          // "Normalize" the parameter by making all letters lowercase
          // (use toLowerCase from the String class)
    
    
    
    
          // Set the appropriate instance variable to the value
          // specified by the parameter -- remember how to compare
          // String objects (for example, you must test to see if the
          // parameter is heads)
    
    
    
    
    
    
    
    
    
    
    
       }
    
  3. Write a statement that declares a Coin object named penny and initializes it to "tails".

     

     

  4. When a method (a constructor or a regular method) is overloaded how does the compiler know which method to execute?

     

     

  5. Suppose a Critter has a name (a String), a number of legs (an int), and a number of eyes (an int). Part of the class describing a Critter is as follows:
         public class Critter
         {
            private String name;
            private int numLegs;
            private int numEyes;
    
    	// Constructor #1
            public Critter()              
            {
               name = "It";
               numLegs = 13;
               numEyes = 8;
            }
    
    	// Constructor #2
            public Critter (String critterName, int legs)  
            {
               name = critterName;
               numLegs = legs;
               numEyes = 2;
            }
    
            // Constructor #3
    	public Critter (String critterName, int legs, int eyes)
            {
                name = critterName;
                numLegs = legs;
                numEyes = eyes;
            }
    
            ...
          }
    
    Show the state of each of the following Critter objects (by filling in the diagram below) after instantiation. Indicate which constructor is executed in each case. (Put the number in the blank beside the instantiation.)
        Critter cat = new Critter ("Isaac", 4);                 _________
        Critter millipede = new Critter ("Twiggy", 1000, 8);    _________
        Critter monster = new Critter ();                       _________
    
        int eyes = 1;
        int legs = 2;
        Critter cyclops = new Critter ("UGH", eyes, legs);    _________
    
        cat                            millipede
        _____________                 ________________
       |             |	         |                |
       |_____________|  name         |________________| name
       |             |               |                |
       |_____________| numLegs       |________________| numLegs
       |             |               |                |
       |_____________| numEyes       |________________| numEyes
        
    
        monster                       cyclops
        _____________                 ________________
       |             |	         |                |
       |_____________|  name         |________________| name
       |             |               |                |
       |_____________| numLegs       |________________| numLegs
       |             |               |                |
       |_____________| numEyes       |________________| numEyes
        
    
    
  6. In general, instance data in classes are given the private modifier and methods are given the public modifier. Why?

     

     

  7. The Circle class you defined in Postlab 9 has a constructor that generates a random radius between 100 and 200, a random x coordinate between 0 and 600 (this is the x coordinate of the upper left-hand corner of the square the circle is drawn in), and a random y coordinate between 0 and 400. The assumption is that the applet window has width 600 and height 400. In the Postlab assignment there was no attempt to guarantee that the circle was completely within the applet window.
    1. Write a method inBounds that has no parameters and returns a boolean value - true if the circle generated is totally in the bounds of the applet and false otherwise. This should be done with a single statement - a return of a boolean expression. Assume the instance variables are x, y, and radius and APPLET_WIDTH and APPLET_HEIGHT are constants giving the applet size.
      
            _______________________________________________________ //header
            {
               // return a boolean indicating whether or not the circle
               // is completely inside the applet window
      
       
      
      
      
      
      
            }
      
      
    2. Fill in the blanks below to use this method in the client program DrawCircles so that 20 circles completely in the applet window are drawn.
           Circle myCircle;
           int count = 0;
      
           do
           {
              // instantiate a new circle
      
      
      
      
      
      
              // if it is in the applet window draw it and count it
      
      
      
      
      
      
      
      
      
      
           } while ( _________________________________________________ );
      
    3. In the preceding exercise the client program needs to use the inBounds method to make sure the circle is inbounds before drawing it. Sometimes a method in a class needs to use another method in the class to get its work done. Suppose the constructor for the Circle class is changed to guarantee the circle constructed is inbounds -- it generates the x, y, and radius until it gets something completely in the applet window (NOTE: this is a very inefficient lousy way to get the circle entirely in the applet window - the point here is to understand how to invoke a method from within the class). Fill in the blank below to complete the loop (use the inBounds method in your condition!).
           do
           {
              // Randomly generate the x, y, and radius (as in postlab --
              // you don't need to fill that code in here
              .....  
      
      
           } while ( _______________________________________________ );