Darrell Bethea May 24, 2011 3 Objects and references More on - - PowerPoint PPT Presentation

darrell bethea may 24 2011 3 objects and references more
SMART_READER_LITE
LIVE PREVIEW

Darrell Bethea May 24, 2011 3 Objects and references More on - - PowerPoint PPT Presentation

Darrell Bethea May 24, 2011 3 Objects and references More on Classes 4 Classes Objects Instance variables Methods Return types Parameters and arguments Information hiding and encapsulation public/private


slide-1
SLIDE 1

Darrell Bethea May 24, 2011

slide-2
SLIDE 2

3

slide-3
SLIDE 3

 Objects and references  More on Classes

4

slide-4
SLIDE 4

 Classes  Objects  Instance variables  Methods

  • Return types
  • Parameters and arguments

 Information hiding and encapsulation

  • public/private
  • accessors/mutators

5

slide-5
SLIDE 5

 Behave difgerently from variables of a

primitive type

6

slide-6
SLIDE 6

 When declaring a variable, a certain amount

  • f memory is assigned based on the declared

primitive type

 What goes in this memory?

7

int age; double length; char letter; memory

slide-7
SLIDE 7

 A data value is stored in the location assigned

to a variable of a primitive type

8

slide-8
SLIDE 8

 What goes in these variables?

9

Student jack; String inputString; memory

slide-9
SLIDE 9

 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

10

slide-10
SLIDE 10

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;

11

slide-11
SLIDE 11

12

Memory

? ?

... ... ...

slide-12
SLIDE 12

12

Memory

Book jacksBook; Book apusBook;

jacksBook apusBook ? ?

... ... ...

slide-13
SLIDE 13

12

Memory

Book jacksBook; Book apusBook; jacksBook = new Book(“Java”);

jacksBook apusBook ? “Java Book” ?

Book object ... ... ...

1056 1056

slide-14
SLIDE 14

12

Memory

Book jacksBook; Book apusBook; jacksBook = new Book(“Java”); apusBook = new Book(“C++ Book”);

jacksBook apusBook “Java Book” “C++ Book” ? ?

Book object Book object ... ... ...

1056 2078 1056 2078

slide-15
SLIDE 15

12

Memory

Book jacksBook; Book apusBook; jacksBook = new Book(“Java”); apusBook = new Book(“C++ Book”); jacksBook.setPage(68);

jacksBook apusBook “Java Book” “C++ Book” ? 68

Book object Book object ... ... ...

1056 2078 1056 2078

slide-16
SLIDE 16

12

Memory

Book jacksBook; Book apusBook; jacksBook = new Book(“Java”); apusBook = new Book(“C++ Book”); jacksBook.setPage(68); apusBook.setPage(254);

jacksBook apusBook “Java Book” “C++ Book” 254 68

Book object Book object ... ... ...

1056 2078 1056 2078

slide-17
SLIDE 17

12

Memory

Book jacksBook; Book apusBook; jacksBook = new Book(“Java”); apusBook = new Book(“C++ Book”); jacksBook.setPage(68); apusBook.setPage(254); apusBook = jacksBook;

jacksBook apusBook “Java Book” “C++ Book” 254 68

Book object Book object ... ... ...

1056 2078 1056 1056

slide-18
SLIDE 18

12

Memory

Book jacksBook; Book apusBook; jacksBook = new Book(“Java”); apusBook = new Book(“C++ Book”); jacksBook.setPage(68); apusBook.setPage(254); apusBook = jacksBook; apusBook.setPage(316);

jacksBook apusBook “Java Book” “C++ Book” 254 316

Book object Book object ... ... ...

1056 2078 1056 1056

slide-19
SLIDE 19

12

Memory

Book jacksBook; Book apusBook; jacksBook = new Book(“Java”); apusBook = new Book(“C++ Book”); jacksBook.setPage(68); apusBook.setPage(254); apusBook = jacksBook; apusBook.setPage(316); jacksBook is now on p. 316!

jacksBook apusBook “Java Book” “C++ Book” 254 316

Book object Book object ... ... ...

1056 2078 1056 1056

slide-20
SLIDE 20

 Variables of a class type contain memory

addresses

  • NOT objects themselves

13

slide-21
SLIDE 21

 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 difgerent addresses!

14

slide-22
SLIDE 22

 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

15

slide-23
SLIDE 23

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

16

slide-24
SLIDE 24

 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

17

slide-25
SLIDE 25

public void increaseNum(int num) { num++; } public void doStufg() { int x = 5; increaseNum(x); System.out.println(x); }  Prints 5. Why?

18

slide-26
SLIDE 26

public void increaseNum(int num) { num++; } public void doStufg() { int x = 5; increaseNum(x); System.out.println(x); }  Prints 5. Why?

18

 num is local to increaseNum method  does not change x

slide-27
SLIDE 27

public void changeBook(Book book) { book = new Book(“Biology”); } public void doStufg() { Book jacksBook = new Book(“Java”); changeBook(jacksBook); System.out.println(jacksBook.getName()); }

 Prints Java. Why?

19

slide-28
SLIDE 28

public void changeBook(Book book) { book = new Book(“Biology”); } public void doStufg() { Book jacksBook = new Book(“Java”); changeBook(jacksBook); System.out.println(jacksBook.getName()); }

 Prints Java. Why?

19

 book is local to changeBook, does not change jacksBook

slide-29
SLIDE 29

public void changeBook(Book book) { book.setName(“Biology”); } public void doStufg() { Book jacksBook = new Book(“Java”); changeBook(jacksBook); System.out.println(jacksBook.getName()); }

 Prints Biology. Why?

20

slide-30
SLIDE 30

public void changeBook(Book book) { book.setName(“Biology”); } public void doStufg() { Book jacksBook = new Book(“Java”); changeBook(jacksBook); System.out.println(jacksBook.getName()); }

 Prints Biology. Why?

20

 book contains the same address as jacksBook!

slide-31
SLIDE 31

 Write an inventory class for pet store  It needs to have the ability to track

  • # of frogs
  • # of birds
  • # of turtles
  • 3 transaction types

 Add  Remove  Inquiry

21

slide-32
SLIDE 32

Would you like to make a transaction? y/n y Would you like to make a add (a), remove (r), or inquire (i)? a How many frogs, birds, and turtles would you like to add? Frogs: 7 Birds: 6 Turtles: 4 Would you like to make a transaction? y/n y Would you like to make a add (a), remove (r), or inquire (i)? r How many frogs, birds, and turtles would you like to remove? Frogs: 4 Birds: 0 Turtles: 0 Would you like to make a transaction? y/n y Would you like to make a add (a), remove (r), or inquire (i)? i You now have: 3 frogs 6 birds 4 turtles

22

slide-33
SLIDE 33

public class PetInventory { private int frogs = 0; private int birds = 0; private int turtles = 0;

24

Beginning of PetInventory Class

slide-34
SLIDE 34

 getFrogs: int  getBirds: int  getTurtles: int  setFrogs(int)  setBirds(int)  setTurtles(int)

25

slide-35
SLIDE 35

public static void main(String[] args) { PetInventory petInventory = new PetInventory(); . . .

petInventory.add(5, 7, 9);

26

Add

slide-36
SLIDE 36

public static void main(String[] args) { PetInventory petInventory = new PetInventory(); . . .

petInventory.add(5, 7, 9);

public void add(int f, int b, int t) { frogs += f; birds += b; turtles += t; } 26

Add

slide-37
SLIDE 37

public static void main(String[] args) { PetInventory petInventory = new PetInventory(); . . .

petInventory.add(5, 7, 9);

public void add(int f, int b, int t) { frogs += f; birds += b; turtles += t; } 26

Add

slide-38
SLIDE 38

public static void main(String[] args) { PetInventory petInventory = new PetInventory(); . . .

petInventory.add(5, 7, 9);

public void add(int f, int b, int t) { frogs += f; birds += b; turtles += t; } 26

Add

slide-39
SLIDE 39

public static void main(String[] args) { PetInventory petInventory = new PetInventory(); . . . petInventory.remove(5, 7, 9); 27

slide-40
SLIDE 40

public static void main(String[] args) { PetInventory petInventory = new PetInventory(); . . . petInventory.remove(5, 7, 9); public void remove(int f, int b, int t) { if ((f > frogs) || (b > birds) || (t > turtles)) { System.out.println("You do not have enough pets for that!"); } else { frogs -= f; birds -= b; turtles -= t; } } 27

slide-41
SLIDE 41

public static void main(String[] args) { PetInventory petInventory = new PetInventory(); . . .

petInventory.inquire();

28

Inquire

slide-42
SLIDE 42

public void inquire() { System.out.println("You now have: " + frogs + " frogs, " + birds + " birds, and " + turtles + " turtles."); } public static void main(String[] args) { PetInventory petInventory = new PetInventory(); . . .

petInventory.inquire();

28

Inquire

slide-43
SLIDE 43

 Midterm Review  Bring questions about the Sample Midterm

29

Tomorrow