Lab 11 Pre-Lab Assignment     Name ____________________________________________

  1. Often a class has more than one constructor - that is, the constructor method is overloaded. The Coin class in the textbook (pages 221-222) 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 a string "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 the string "heads" or the string "tails")
    
    
    
    
    
    
    
    
    
    
    
    
    
    
       }
    
  2. Write a statement that declares a Coin object named penny and initializes it to "tails".

     

     

     

  3. When a method (a constructor or a regular method) is overloaded how does the compiler know which method should be executed?

     

     

     

     

  4. Recall that in Assignment 3 you wrote a Critter class with a constructor that created a Critter object with random characteristics (for example width, eye size, and color). In the main panel class you instantiated 3 Critter objects but then immediately changed one of them by using getter methods to get the attributes of the parents and then setter methods to set the attributes of the child. A better way of doing that would have been to get the attributes of the parents as you did before but then have a second constructor that could create the child to have the given attributes (rather than random ones). This exercise involves a different (and simpler) Critter class that has three constructors. 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
     
       
    
    
  5. The Circle class defined in Chapter 4 (pages 188-190) has a constructor that takes 4 parameters - the diameter of the circle, the color, and the x and y coordinates of the upper left corner. Note that the names of the instance variables are diameter, color, x, and y. Suppose the following static constants and variable are added to the class:
          public static final int WIDTH = 600;   // width of the graphics object
                                                 // a circle will be drawn on
          public static final int HEIGHT = 400;  // height of the graphics object
    
          private static Random generator = new Random();
    
    
    1. What is the difference between a static variable or constant and an instance variable or constant?

       

       

       

    2. Show how a client program can use the constant WIDTH to print out the applet width by filling in the following print statement.
      System.out.println ("Assumed applet width: " + __________________________________);
      
    3. The following is a parameterless constructor that generates a circle with random diameter in the range 75 up to 150, a random x coordinate between 0 and 600, a random y coordinate between 0 and 400, and a random color (using the Color constructor with one parameter).
      
           public Circle ()
           {
              final int MAX_COLOR = (int)Math.pow(2,24) - 1;
              x = generator.nextInt(WIDTH);
              y = generator.nextInt(HEIGHT);
              diameter = generator.nextInt(75) + 75;
      	color = new Color (generator.nextInt(MAX_COLOR));
            }
      
      
      Clearly the circle generated may not fit completely within the applet window which may be ok in some applications but not others. It would be nice for the class to provide a method to determine whether the circle fits or not. 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.
      
      
            _______________________________________________________ //header
            {
               // return a boolean indicating whether or not the circle
               // is completely inside the applet window
      
       
      
      
      
      
      
      
            }
      
      
    4. Fill in the blanks below to use this method in a client program so that 20 circles completely in the applet window are drawn. (Note that the Circle class has a draw method to draw a circle object.)
      
           Circle myCircle;
           int count = 0;
      
           do
           {
              // instantiate a new random circle
      
      
      
      
      
              // if it is in the applet window draw it and count it
      
      
      
      
      
      
      
      
      
      
      
      
           } while ( _________________________________________________ );
      
      
    5. 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 - these are the helper (or support) methods. Suppose the constructor for the Circle class is changed to guarantee the circle constructed is inbounds -- it generates the x, y, and diameter 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 emphasize the difference between invoking a method from within the class and invoking it from a client program). Fill in the blank below to complete the loop (use the inBounds method in your condition!) that would be in the constructor.
           do
           {
              // Randomly generate the x, y, and diameter (as in the constructor
              // above -- you don't need to fill that code in here)
              .....  
      
      
           } while ( _______________________________________________ );