Summary for 7/9/03

Lecture Summary for CSC 211 on 7/9/03 -- O'Hare

Review Questions

  1. Explain what the following terms mean when applied to variables.
    a. Instance        b. Local        c. Reference
    Answer: An instance variable can be used anywhere in the class where it is declared. A local variable can only be used within the method where it is declared. A reference contains the address of an object.

  2. Explain the difference between the following:
    0      0.0      '\0'      ""      null      false
    Answers:
    1. 0 is an int (4 byte) constant.
    2. 0.0 is a double (8 byte) constant.
    3. '\0' is the null character constant with ascii code 0.
      When '\0' is given to a printer to print, nothing will happen.
    4. "" is the String object of zero length.
    5. null is the value in a reference variable when it does not refer to an object.
    6. false is the boolean constant false, represented internally by 0.

  3. What are the three steps to creating an object?
    Answer:
    1. Declare a reference variable of the same type as the object.
    2. Call a constructor for the object using the new operator.
    3. Initialize the values of the object's instance variables.

  4. Why should instance variables be declared private?
    Ans: So they are not subject to change by the outside world. Making an instance variable private is safer.

  5. How do you make an instance variable read only?
    Ans: Make the instance variable private, but supply a public accessor method to get its value.

  6. Explain what the following terms mean when applied to constructors.
    a. Default        b. Noarg        c. Parameterized
    Ans: A default constructor is a constructor that Java calls if you do not supply one. It initializes numeric instance variables to zero, boolean variables to false, char variables to the null character, and reference variables to null. See also the answer to Question 3. A noarg constructor has not no parameters that initializes instance variables to default values. A parameterized constructor does contain parameters corresponding to arguments that are passed in when the constructor is called with the new operator.

  7. If a class has more than one constructor, how does Java decide which one to use?
    Ans: It checks the signature of the input arguments to the constructor and chooses a constructor with the same signature.

  8. True or False? A toString method should be provided for every class.
    Ans: Ideally, true. A toString method is very useful for debugging. Every class in the Java Class Library has a toString method.

  9. What is wrong? Assume that the instance variables name, gender, and age are public.
    Kid x;
    x.name = "Mary";
    x.gender = 'F';
    x.age = 12;
    System.out.println(x);
    Ans: No object has been created so there will be a null pointer exception.

  10. Explain the difference between an accessor method and a mutator method.
    Ans: An accessor method returns the value of an instance variable.
    A mutator method changes the value of an instance variable, preferably after doing some data validation.

  11. Match the method types in Column 1 with the items in Column 2.
    Column 1 Column 2
    Mutator One return value
    Accessor No return value
      One parameter
      No parameter

    Ans: A mutator method has one parameter and no return value.
    An accessor method has no parameter and one return value.

  12. Which type of method does not specify a return type in its header.
    Ans: A constructor. It is the only the only type of method that does not specify a return type.

  13. What is wrong with this constructor definition?
    public MyClass()
    {
        MyClass x = MyClass();
    }
    Ans: When an object is created, the MyClass constructor is called. This constructor then creates another object x that calls the MyClass constructor, creating another object x, calling another constructor, etc. Hence an endless cycle of constructor calls ensues.

  14. Assume that x is a Kid object. Write lines of Java code that print "yes" if x is female and "no" otherwise. Assume the class definition Kids2.   Answer:
    if (x.getGender() == 'F')
        System.out.println("yes");
    else
        System.out.println("no");

  15. Assume that x is a Kid object. Write lines of Java code that print the length of the name for y.   Answer:
    System.out.println(x.getName().length());

  16. Design and implement a GroceryItem class.
    1. Draw the UML diagram.
    2. Write the code for the class. Only write the accessor and mutator method of one of the instance variables.
    3. Write a main method to test the class.
    4. Predict the output of the main method in part c.
    Here is the answer.

 

Overloaded Methods

 

More About Reference Variables

 

More Examples

 

Arrays in Java

 

The LasVegas Project