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

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

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

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

COMP 110COMP 110Objects and referencesObjects and references

Luv KohliOctober 8, 2008

MWF 2-2:50 pmSitterson 014

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

AnnouncementsAnnouncements

2

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

Questions?Questions?

3

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

Today in COMP 110Today in COMP 110Discuss Lab 4

Objects and references

4

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

Lab 4Lab 4Averages should be represented as

floating-point numbersVariables such as totalGames,

gamesOver90 should be integerselse does not work for min/max unless

you do it a certain way

5

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

Lab 4Lab 4Took 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 calculationsLenient this time around on comments if

code was written clearlyTook off a point for incorrect submissionNext time, be careful with

indentation/whitespace

6

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

Assignment statements as Assignment statements as valuesvaluesint totalGames;

int gamesOver90;

int scoreSum;

totalGames = gamesOver90 = scoreSum = 0;

The value of the expression scoreSum = 12;

is 12.

7

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

ReviewReviewClassesObjectsInstance variablesMethods◦Return types◦Parameters and arguments

Information hiding and encapsulation◦public/private◦accessors/mutators

8

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

Variables of a class typeVariables of a class typeBehave differently from variables of a

primitive type

9

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

Variables of a primitive typeVariables of a primitive typeWhen 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

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

Variables of a primitive typeVariables of a primitive typeA data value is stored in the location

assigned to a variable of a primitive type

11

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

Variables of a class typeVariables of a class typeWhat goes in these variables?

12

Student jack;

String inputString;

memory

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

Variables of a class typeVariables of a class typeContain 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

memoryThe address to this other location is

called a reference to the objectClass types are also called reference types

13

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

Example: BooksExample: 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

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

Objects in memoryObjects 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?

Java137

Java253

Java137

Java253

Java509

2078

?

2078

1056

?

?

?

2078

1056

2078

2078

2078

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

RememberRememberVariables of a class type contain memory

addresses◦NOT objects themselves

16

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

== vs. equals() for Strings explained== vs. equals() for Strings explained

String is a class typeWhat 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

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

== vs. equals() for Strings explained== 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

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

Writing the .equals() methodWriting the .equals() methodpublic class Book

{

private String name;

private int page;

public boolean equals(Book book)

{

return (this.name.equals(book.name) &&

this.page == book.page);

}

}

19

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

.equals().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

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

Parameters of a primitive typeParameters of a primitive typepublic 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

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

Parameters of a class typeParameters of a class typepublic 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

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

Parameters of a class typeParameters of a class typepublic 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

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

FridayFridaySome more information on objects and

references

DecimalFormat

Help with Lab 5

24