NAMES: ______

Exploring Reference Types Reading: Gaddis Chap(9.1-9.8 also selected sections in 6)

Objectives

At the end of this exercise, students will:

 Articulate what it means to be a reference variable  Trace reference objects in a program  Define and recognize overloading with respect to methods  Explain why the instance fields of a class should be defined as private  Create a new method using instance fields

Getting ready

1. All team members - record your notes on your own response sheet. 2. Recorder – Record your team answers on the board. Also turn in your group’s diagram at the end of class. 3. Manager – You will keep the group on track. Each person should do the exercise independently, then have the team compare notes.

Exploring Person.java (Review of overloading)

1. What are the attributes (instance fields) of the Person class? G(addis):6.1

2. Why are these fields private? (G:6.1)

3. Find the first constructor for this class. What are the formal parameters (parameters) for this constructor? (G:5.2)

4. What is the line “this.first = fName;” doing? (G:6.3)

5. Find the second constructor for this class. How is it different than the first constructor?

6. This feature (where we have two methods with the same name) is called overloading. In your own words, describe what overloading a method means. (G:6.4)

7. Any method may be overloaded. Java determines which one to use based on the call to the method. Write two different calls to the constructor of Person which will result in using each of the two constructors. Assume that two Person variables, herman and candace, have been declared.

8. The toString method of a class is automatically called when we print an object of the class. Write a System.out.println statement that will print herman. (G:9.4)

9. In the following method call using two objects, herman and candace, identify which is this in the equals method and which is other. if(herman.equals(candace) System.out.println(“Equal”);

10. Which of the methods in the Person class are mutator methods? Which are accessor methods? (G:6.2)

11. Is this an immutable class? Why or why not?

Understanding Reference Types

For this section, look at the code for PersonDriver.java in addition to Person.java. Assume that the code will compile and execute properly.

BOARD 1. What is output by lines 17 – 20?

2. What is output by lines 23 – 26?

BOARD 3. What is output by lines 30 – 34?

4. What is output by lines 37 – 40?

BOARD 5. What is output by lines 46 – 49?

6. What is output by lines 52 – 55?

7. What is output by line 59?

8. What is output by line 60?

On a separate piece of paper, draw a diagram (like the one we explored at the beginning of class) showing the state of memory at the conclusion of this program. That is the only thing that you need to turn in today. /*********************************************************************** * Person class represents a person's name * * @author Nancy Harris, JMU * @version V1 Nov 15,2010 **********************************************************************/ public class Person { private String first; private String middle; private String last;

/******************************************************************* * Person explicit value constructor * * @param fName The first name * @param mName The middle name * @param lName The last name ******************************************************************/ public Person(String fName, String mName, String lName) { this.first = fName; this.middle = mName; this.last = lName; }

/******************************************************************* * Person explicit value constructor - used when no middle name available * * @param fName The first name * @param lName The last name ******************************************************************/ public Person(String fName,String lName) { this.first = fName; this.last = lName; } /***************************************************************** * equals compares two names. First and last must match. * If one or the other middle is missing, ignore. * * @return true if the two names match, false otherwise ****************************************************************/ public boolean equals(Person other) { boolean result; result = true;

if (!this.first.equals(other.first)) result = false; // if middles are different but if one or the other is missing don't // compare if (this.middle != null && other.middle != null && !this.middle.equals(other.middle)) result = false; if (!this.last.equals(other.last)) result = false;

return result; } /***************************************************************** * getName returns the person's name in first name, middle initial * last name order * * @return The person's name ****************************************************************/ public String getName() { String builder; builder = first; if(middle != null) builder = builder + " " + middle.charAt(0); builder = builder + " " + last;

return builder; }

/***************************************************************** * toString method returns last, first middle name format * * @return The name in last, first middle name format ****************************************************************/ public String toString() { String builder; builder = last + ", " + first;

if (middle != null) builder = builder + " " + middle;

return builder; } /******************************************************************* * Update name updates the person name * * @param fName The first name (if null, don't change) * @param mName The middle name (if null, don't change) * @param lName The last name (if null, don't change) ******************************************************************/ public void updateName(String fName, String mName, String lName) { if (fName != null) this.first = fName; if (mName != null) this.middle = mName; if (lName != null) this.last = lName; } } 1 /********************************************************** 2 * PersonDriver exercises the Person class 3 * 4 * @author Nancy Harris 5 * @version V1 11/15/2010 6 *********************************************************/ 7 public class PersonDriver 8 { 9 public static void main(String args[]) 10 { 11 Person mary, sue, harvey, blanche, gene; 12 13 mary = new Person("Mary", "Smith"); 14 sue = new Person("Susan", "Elizabeth", "Jones"); 15 16 // Are mary and sue the same object 17 if(mary == sue) 18 System.out.println("The two variables are aliases."); 19 else 20 System.out.println("The two variables are not aliases."); 21 22 // Do mary and sue have the same value 23 if(mary.equals(sue)) 24 System.out.println("The values of the objects are the same."); 25 else 26 System.out.println("The values of the objects are different."); 27 28 blanche = new Person("Mary", "Smith"); 29 30 // Are mary and blanche the same object 31 if(mary == blanche) 32 System.out.println("The two variables are aliases."); 33 else 34 System.out.println("The two variables are not aliases."); 35 36 // Do mary and blanche the same value 37 if(mary.equals(blanche)) 38 System.out.println("The values of the objects are the same."); 39 else 40 System.out.println("The values of the objects are different."); 41 42 harvey = new Person("Harvey", "James", "Mudd"); 43 gene = harvey; 44 45 // Are harvey and gene the same object 46 if(harvey == gene) 47 System.out.println("The two variables are aliases."); 48 else 49 System.out.println("The two variables are not aliases."); 50 51 // Is the value of harvey the same as the value in gene 52 if(harvey.equals(gene)) 53 System.out.println("The values of the objects are the same."); 54 else 55 System.out.println("The values of the objects are different."); 56 57 // How many objects do we have now 58 gene.updateName(null, "Genius", null); 59 System.out.println(gene); 60 System.out.println(harvey); 61 62 63 } 64 }