comp 110 objects and references luv kohli october 8, 2008 mwf 2-2:50 pm sitterson 014
TRANSCRIPT
![Page 1: COMP 110 Objects and references Luv Kohli October 8, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/1.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/2.jpg)
AnnouncementsAnnouncements
2
![Page 3: COMP 110 Objects and references Luv Kohli October 8, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/3.jpg)
Questions?Questions?
3
![Page 4: COMP 110 Objects and references Luv Kohli October 8, 2008 MWF 2-2:50 pm Sitterson 014](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/7.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/17.jpg)
== 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](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/18.jpg)
== 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](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/20.jpg)
.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](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/22.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062518/5697bf7b1a28abf838c83615/html5/thumbnails/24.jpg)
FridayFridaySome more information on objects and
references
DecimalFormat
Help with Lab 5
24