Reading: Gaddis Chap(9.1-9.8 Also Selected Sections in 6)

Reading: Gaddis Chap(9.1-9.8 Also Selected Sections in 6)

<p>NAMES: ______</p><p>Exploring Reference Types Reading: Gaddis Chap(9.1-9.8 also selected sections in 6)</p><p>Objectives</p><p>At the end of this exercise, students will: </p><p> 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</p><p>Getting ready</p><p>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. </p><p>Exploring Person.java (Review of overloading)</p><p>1. What are the attributes (instance fields) of the Person class? G(addis):6.1</p><p>2. Why are these fields private? (G:6.1)</p><p>3. Find the first constructor for this class. What are the formal parameters (parameters) for this constructor? (G:5.2)</p><p>4. What is the line “this.first = fName;” doing? (G:6.3)</p><p>5. Find the second constructor for this class. How is it different than the first constructor?</p><p>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)</p><p>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. </p><p>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)</p><p>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”);</p><p>10. Which of the methods in the Person class are mutator methods? Which are accessor methods? (G:6.2)</p><p>11. Is this an immutable class? Why or why not?</p><p>Understanding Reference Types</p><p>For this section, look at the code for PersonDriver.java in addition to Person.java. Assume that the code will compile and execute properly.</p><p>BOARD 1. What is output by lines 17 – 20?</p><p>2. What is output by lines 23 – 26?</p><p>BOARD 3. What is output by lines 30 – 34?</p><p>4. What is output by lines 37 – 40?</p><p>BOARD 5. What is output by lines 46 – 49?</p><p>6. What is output by lines 52 – 55?</p><p>7. What is output by line 59?</p><p>8. What is output by line 60?</p><p>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;</p><p>/******************************************************************* * 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; }</p><p>/******************************************************************* * 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;</p><p> 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;</p><p> 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;</p><p> return builder; }</p><p>/***************************************************************** * 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;</p><p> if (middle != null) builder = builder + " " + middle;</p><p> 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 }</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    5 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us