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 = 1;
            }
    
    	// 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 ("Sam", 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. An exception to the above is methods that are used internally in the class to help other methods do their job. These methods are called support methods and are private. The Account class defined on pages 227-229 of the textbook is similar to the one from the last lab BUT both the deposit and withdraw methods check to make sure the parameter passed in for the amount is not negative. Because this test to see if the amount is negative is similar in both methods the test could be put in a separate support method. Both deposit and withdraw would call this method rather than doing their own test and error message. For example, suppose the support method is named validAmount and takes one double argument representing the amount and returns a boolean (false if the argument is negative; true otherwise). Then the body of the withdraw method (on page 228) could be modified to use the support method as follows:
              amount += fee;
              if (validAmount(amount))
              {
                 if (amount > balance)
                 {
                    Same as now -- print insufficient funds messages
                 }
                 else
                    balance = balance - amount;
              }
              return balance;
    
    1. Fill in the code for the private method validAmount - it has one parameter of type double and returns a boolean. If the amount is negative the method prints an error message indicating it is not legal to withdraw or deposit a negative amount.
          _________________________________________________________ // header
          {
              //print an error message if the amount is negative
      
      
      
      
      
      
              // return false if the amount is negative; true otherwise
              // DO NOT use an if -- just return the value of a boolean 
              // expression
      
      
      
           }
      
      
    2. Write the body of the deposit method so it calls the validAmount method rather than checking for a negative amount itself.