1 / 21

References as Method Arguments toString() equals()

B Smith: Fall07: Rate: 2. Poorly organized, but the extemporaneous lecture and review of Copy constructors went well (3/4). References as Method Arguments toString() equals(). Math 130 Introduction to Programming Lecture # 17 Friday, October 5, 2007. Learning Outcomes.

glenda
Download Presentation

References as Method Arguments toString() equals()

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. B Smith: Fall07: Rate: 2. Poorly organized, but the extemporaneous lecture and review of Copy constructors went well (3/4) . References as Method Arguments toString()equals() Math 130Introduction to Programming Lecture # 17 Friday, October 5, 2007

  2. Learning Outcomes

  3. Passing References as Arguments • Objects can be passed to methods as parameters. • Java passes all parameters by value. • When an object is passed as a parameter, the value of the reference variable is passed. • The value of the reference variable is a reference to the object in memory. • A copy of the object is not passed, just a pointer to the object.

  4. An InventoryItem object description: units: “Wrench” 20 displayItem(item); Address public static void displayItem(InventoryItem i){ System.out.println("Description: " + i.getDescription()); System.out.println("Units: " + i.getUnits()); } Passing References as Arguments Example:PassObject.javaPassObject2.java

  5. B Smith: Stopped here on 3/7 with detailed examples. Also had 15 min discussion on workshop. Returning References From Methods • Methods are not limited to returning the primitive data types. • Methods can return references to objects as well. • Just as with passing parameters, a copy of the object is not returned, only its address. • Example: ReturnObject.java • Method return type: public static InventoryItem getData(){ … return new InventoryItem(d, u); }

  6. B Smith: Started here on 3/12. Was absent Friday 3/9 attended SIGCSE. Gave/built up examples using Circle class from BlueJ Same Class Operations • The String class has several methods, like equals, that accept String objects as parameters. • When a method accepts a parameter that is an instance of its own class, it is known as a same-class operation.

  7. Stock • symbol : String • sharePrice : double + Stock(sym: String, price: double) : + getSymbol() : String + getSharePrice() : double + toString() : String + equals(Object2: Stock) : boolean Same Class Operations Example:Stock.javaStockDemo1.java StockCompare.java

  8. Same Class Operations • The equals method of the Stock class takes an instance of the Stock class as a parameter. • Returns true if both “Stock objects” are equal or false if they are not. • Example: StockCompare.java public boolean equals(Stock object2) { boolean status; if( symbol.equals( Object2.symbol ) && sharePrice == object2.sharePrice) { status = true; } else { status = false; } return status; }

  9. The toString Method • The toString() method of a class can be called explicitly: System.out.println("The distance you entered is " + company1.toString()); • However, the toString() method does not have to be called explicitly but can be called implicitly. System.out.println("The distance you entered is " + company1); • All objects have a toString() method that returns the class name and a hash of the memory address of the object. • We can override the default method with our own to print out more useful information.

  10. Administrivia • Quiz Monday • Comment your code • Format your code • Your name(s) at the top of each file

  11. B Smith: implement the equals method for points The equals Method • When the == operator is used with reference variables, the memory address of the objects are compared. • The contents of the objects are not compared. • All objects have an equals method. • The equals method performs a same class operation. • The default operation of the equals method is to compare memory addresses of the objects.

  12. The equals Method • The Stock class has an equals method. • If we try the following: Stock stock1 = new Stock(“GMX”, 55.3); Stock stock2 = new Stock(“GMX”, 55.3); if (stock1 == stock2) // This is a mistake. System.out.println("The objects are the same."); else System.out.println("The objects are not the same."); • only the addresses of the objects are compared.

  13. The equals Method • Instead of using the == operator to compare two FeetInches objects, we should use the equals method. public boolean equals(Stock object2){ boolean status; if(symbol.equals(Object2.symbol) && sharePrice == Object2.sharePrice) status = true; else status = false; return status; } • Now, objects can be compared by their contents rather than by their memory addresses. • Example: StockCompare.java

  14. Copying Objects • There are two ways to copy an object. • Shallow copy • This is simply copying the address of an object into another reference variable. • Deep copy • This involves creating a new instance of the class and copying the values from one object into the new object. • Example: Deepcopy.java

  15. Copy Constructors • A copy constructor is a constructor that takes an existing object of the same class as its argument • The copy constructor makes the new object an exact copy of the argument. • A Stock class copy constructor public Stock(Stock object2){ symbol = object2.symbol; sharePrice = object2.sharePrice; }

  16. Copy Constructors • Using the copy constructor // Create a Stock object. Stock company1 = new Stock(“XYZ”, 9.62); // Create another Stock object that is a copy of the company1 object. Stock company2 = new Stock(company1);

  17. Aggregation • Creating an instance of one class as a reference in another class is called object aggregation. • Aggregation creates a “has a” relationship between objects. • Example: • Instructor.java, Textbook.java, Course.java, CourseDemo.java courseDemo

  18. Course TextBook - courseName : String - Instructor : Instructor - textBook : TextBook • title : String • author : String • publisher : String + Course(name : String, instr : Instructor, text : TextBook): + getName() : String + getInstructor() : Instructor + getTextBook() : TextBook + toString() : String + TextBook(title : String, author : String, publisher : String): + TextBook(object2 : TextBook): + set(title : String, author : String, publisher : String) : void + toString() : String Instructor • lastName : String • firstName : String • officeNumber : String + Instructor(lname : String, fname : String, office : String): +Instructor(object2 : Instructor): +set(lname : String, fname : String, office : String): void + toString() : String B Smith: Stopped here on 3/12 Aggregation in UML Diagrams

  19. Returning References to Private Fields • Avoid returning references to private data elements. • Returning references to private variables will allow any object that receives the reference to modify the variable.

  20. Null Pointers • A null pointer is when a reference variable points to nothing. • If a reference is null, then no operations can be performed on it. • References can be tested to see if they point to null prior to being used. if(lastName != null) System.out.println(“Last name is: “ + lastName.toUpperCase()); • Example: FullName.java, NameTester.java

  21. Summary

More Related