Lecture Summary for CSC 211 on 7/9/03 -- O'Hare
Review Questions
- 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.
- Explain the difference between the following:
0     
0.0     
'\0'     
""     
null     
false
Answers:
- 0 is an int (4 byte) constant.
- 0.0 is a double (8 byte) constant.
- '\0' is the null character constant with ascii code 0.
When '\0' is given to a printer to print, nothing will happen.
- "" is the String object of zero length.
- null is the value in a reference variable when it does not refer to
an object.
- false is the boolean constant false, represented internally by 0.
- What are the three steps to creating an object?
Answer:
- Declare a reference variable of the same type as the object.
- Call a constructor for the object using the new operator.
- Initialize the values of the object's instance variables.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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");
- 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());
- Design and implement a GroceryItem class.
- Draw the UML diagram.
- Write the code for the class. Only write the accessor and mutator
method of one of the instance variables.
- Write a main method to test the class.
- Predict the output of the main method in part c.
Here is the
answer.
 
Overloaded Methods
- The signature of a method is the ordered list of
the types of its parameters.
- Two methods are different if their names differ, or if their
names are the same but their signatures are different. For example
f(5, "a"); and
f("a", 5); are different methods
because the signature (int, String) is different than
(String, int).
- The Overloaded Example (Overloaded.zip or Overloaded.html).
 
More About Reference Variables
- A reference variable contains the address of an object.
- The reference variable is said to point to an object, as shown in
the following reference diagram. In this
case the reference variable r points to a Kid object.
- More than one reference variable can point to an object, as shown
in this diagram. The reference variables
r and s both refer to the same Kid object.
- A reference variable containing the null reference does not
point to any object. The null reference is shown by a slash in
in this example.
 
More Examples
- Kids3 Example in Classes1.
- Dots Example.
 
Arrays in Java