Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 110 Objects and references Luv Kohli October 8, 2008 MWF 2-2:50 pm Sitterson 014.

Similar presentations


Presentation on theme: "COMP 110 Objects and references Luv Kohli October 8, 2008 MWF 2-2:50 pm Sitterson 014."— Presentation transcript:

1 COMP 110 Objects and references Luv Kohli October 8, 2008 MWF 2-2:50 pm Sitterson 014

2 Announcements 2

3 Questions? 3

4 Today in COMP 110 Discuss Lab 4 Objects and references 4

5 Lab 4 Averages should be represented as floating-point numbers Variables such as totalGames, gamesOver90 should be integers else does not work for min/max unless you do it a certain way 5

6 Lab 4 Took off a point if loop did not handle the case of 0 games properly ◦ Took off another point if the sentinel value was used in your calculations Lenient this time around on comments if code was written clearly Took off a point for incorrect submission Next time, be careful with indentation/whitespace 6

7 Assignment statements as values int totalGames; int gamesOver90; int scoreSum; totalGames = gamesOver90 = scoreSum = 0; The value of the expression scoreSum = 12; is 12. 7

8 Review Classes Objects Instance variables Methods ◦ Return types ◦ Parameters and arguments Information hiding and encapsulation ◦ public/private ◦ accessors/mutators 8

9 Variables of a class type Behave differently from variables of a primitive type 9

10 Variables of a primitive type When declaring a variable, a certain amount of memory is assigned based on the declared primitive type What goes in this memory? 10 int age ; double length ; char letter ; memory

11 Variables of a primitive type A data value is stored in the location assigned to a variable of a primitive type 11

12 Variables of a class type What goes in these variables? 12 Student jack ; String inputString ; memory

13 Variables of a class type Contain the memory address of the object named by the variable ◦ NOT the object itself What is an address? Object is stored in some other location in memory The address to this other location is called a reference to the object Class types are also called reference types 13

14 Example: Books Assume we have a class named Book Book jacksBook = new Book(“Java”); Book apusBook = new Book(“Java”); vs. Book jacksBook = new Book(“Java”); Book apusBook = jacksBook; 14

15 Objects in memory 15 jacksBook apusBook ???? Memory Book jacksBook; Book apusBook; jacksBook = new Book(“Java”); apusBook = new Book(“Java”); jacksBook.setPage(137); apusBook.setPage(253); apusBook = jacksBook; apusBook.setPage(509); jacksBook is now on p. 509! ???????? ? Java ? Java ? Java ? Java ? Java 137 Java 253 Java 137 Java 253 Java 509 2078 ? 2078 1056 ???? ? 2078 1056 2078

16 Remember Variables of a class type contain memory addresses ◦ NOT objects themselves 16

17 == vs. equals() for Strings explained String is a class type What happens when you have String s1 = new String(“Hello”); String s2 = new String(“Hello”); boolean strEqual = (s1 == s2); strEqual is false! Why? s1 and s2 store different addresses! 17

18 == vs. equals() for Strings explained What happens when you have String s1 = new String(“Hello”); String s2 = new String(“Hello”); boolean strEqual = (s1.equals(s2)); strEqual is true! Why? String’s.equals() method checks if all the characters in the two Strings are the same 18

19 Writing the.equals() method public class Book { private String name; private int page; public boolean equals(Book book) { return (this.name.equals(book.name) && this.page == book.page); } 19

20 .equals() Every class has a default.equals() method if it is not explicitly written ◦ Does not necessarily do what you want You decide what it means for two objects of a specific class type to be considered equal ◦ Perhaps books are equal if the names and page numbers are equal ◦ Perhaps only if the names are equal ◦ Put this logic inside.equals() method 20

21 Parameters of a primitive type public void increaseNum(int num) { num++; } public void doStuff() { int x = 5; increaseNum(x); System.out.println(x); } Prints 5. Why? num is local to increaseNum method; does not change x 21

22 Parameters of a class type public void changeBook(Book book) { book = new Book(“Biology”); } public void doStuff() { Book jacksBook = new Book(“Java”); changeBook(jacksBook); System.out.println(jacksBook.getName()); } Prints Java. Why? book is local to changeBook, does not change jacksBook 22

23 Parameters of a class type public void changeBook(Book book) { book.setName(“Biology”); } public void doStuff() { Book jacksBook = new Book(“Java”); changeBook(jacksBook); System.out.println(jacksBook.getName()); } Prints Biology. Why? book contains the same address as jacksBook! 23

24 Friday Some more information on objects and references DecimalFormat Help with Lab 5 24


Download ppt "COMP 110 Objects and references Luv Kohli October 8, 2008 MWF 2-2:50 pm Sitterson 014."

Similar presentations


Ads by Google