Collection of Objects 15-121 Fall 2020 Margaret Reid-Miller Today - - PowerPoint PPT Presentation
Collection of Objects 15-121 Fall 2020 Margaret Reid-Miller Today - - PowerPoint PPT Presentation
Collection of Objects 15-121 Fall 2020 Margaret Reid-Miller Today Quiz will be due Sunday 11:55pm Homework3: start early due Monday September 21 th at 11:55pm Review of how objects are stored Arrays of objects ContactList
Today
- Quiz will be due Sunday 11:55pm
- Homework3: start early
- due Monday September 21th at 11:55pm
- Review of how objects are stored
- Arrays of objects
- ContactList implementation
Fall 2020 15-121 (Reid-Miller) 2
Primitive type variables hold values
(e.g., int, double, char)
Fall 2020 15-121 (Reid-Miller) 3
- Variables of primitive types name a storage location
in memory in which we can store a value.
Primitive Types
balance1 1000.0 1000.0
double balance1 = 1000.0;
Fall 2020 15-121 (Reid-Miller) 4
- Simply declaring a local variable does not provide a
value for the storage location. You cannot use the variable until it is assigned a value.
Primitive Types
balance2 balance1 1000.0 1000.0
double balance1 = 1000.0; double balance2;
Fall 2020 15-121 (Reid-Miller) 5
- Assigning the value of the one variable to another
copies the value:
Primitive Types
balance2 1000.0 balance1 1000.0
double balance1 = 1000.0; double balance2; balance2 = balance1;
Fall 2020 15-121 (Reid-Miller) 6
- You can assign a new value to a variable. The
previous value is lost.
Primitive Types
balance2 1000.0 balance1 1000.0 500.0
double balance1 = 1000.0; double balance2; balance2 = balance1; balance1 = 500;
Fall 2020 15-121 (Reid-Miller) 7
Object type variables hold references to objects.
Fall 2020 15-121 (Reid-Miller) 8
- Alice gets a $100 gift card from Target.
- Object type variables also name a memory location.
But the memory is too small to hold an object. It can
- nly hold a reference (pointer) to the object.
GiftCard alice = new GiftCard("Macy's", 100.0); ; (60.0); ;
100.0 name balance GiftCard alice String "Macy's"
Object Types
Fall 2020 15-121 (Reid-Miller) 9
GiftCard alice = new GiftCard("Macy's", 100.0); GiftCard bob = alice; (60.0); ;
100.0 bob name balance GiftCard alice String "Macy's"
- Bob takes Alice’s gift card.
Object References
Assigning alice to bob copies the reference from alice to bob. We say bob is an alias for alice.
Fall 2020 15-121 (Reid-Miller) 10
GiftCard alice = new GiftCard("Macy's", 100.0); GiftCard bob = alice; bob.buyGoods(60.0); ;
40.0 bob name balance GiftCard alice String "Macy's"
- Bob spends $60. Alice can see that her card now
has only $40.
Object References
Fall 2020 15-121 (Reid-Miller) 11
GiftCard alice = new GiftCard("Macy's", 100.0); GiftCard bob = alice; bob.buyGoods(60.0); alice = new GiftCard("Target", 75.0);
40.0 bob name balance GiftCard alice 75.0 name balance GiftCard String "Macy's" String "Target"
- Alice buys a $75 gift card from Target.
Object References
Fall 2020 15-121 (Reid-Miller) 12
GiftCard alice = new GiftCard("Macy's", 100.0); GiftCard bob = alice; bob.buyGoods(60.0); alice = new GiftCard("Target", 75.0); bob = alice;
40.0 bob name balance GiftCard alice 75.0 name balance GiftCard String "Macy's" String "Target"
- Bob takes Alice’s Target card and loses Macy’s card.
Object References
Fall 2020 15-121 (Reid-Miller) 13
40.0 bob name balance GiftCard alice 75.0 name balance GiftCard String "Macy's" String "Target"
- But now the program cannot access the Macy’s gift
card any more.
- Such objects are considered “garbage” because they
still take up memory space.
Garbage
Fall 2020 15-121 (Reid-Miller) 14
String "Macy's" 40.0 GiftCard bob alice 75.0 store balance GiftCard String "Target"
- To reclaim the memory space, Java has a garbage
collector that periodically “cleans up” memory so that it can be reused.
- (C doesn’t have garbage collection and programs can easily
have a “memory leak” if not programmed with extreme care.)
Garbage Collector
name balance
Compliments of the garbage collector!
Fall 2020 15-121 (Reid-Miller) 15
Object Types as Parameters
- An object type parameter is an alias of the argument.
GiftCard alice = new GiftCard("Macy's", 50.0); goShopping(alice); public static void goShopping(Giftcard card) { while (card.getBalance > 0) { card.buyGoods(10.0) } }
50.0 "Macy's"
alice card copies the reference
Fall 2020 15-121 (Reid-Miller) 16
The null Pointer
null sue If we do not instantiate an object, the variable holds a special value null that represents a nonexisting
- bject.
GiftCard sue; sue.addMoney(30);
If we try to use the variable as an object, we get a NullPointerException at runtime.
Fall 2020 15-121 (Reid-Miller) 17
The equals Method Revisited
- The == operator tests whether two variables have the
same references (identity);
- Whereas the equals method tests whether two
variables refer to objects that have the same state (content). store balance GiftCard
Fall 2020 15-121 (Reid-Miller) 18
Arrays of Objects
Array of Objects
- Arrays can store references to objects in addition to
primitive values.
- E.g.,
GiftCard[] cards = new GiftCard[4];
- Creating an array does not create the objects.
- Instead each element of the array is initialized to
null. [0] [1] [2] [3]
null null null null
cards
Fall 2020 15-121 (Reid-Miller) 20
Creating the Objects in the Array
- To fill the array, you need to create the objects in
addition to creating the array.
cards[0] = new GiftCard("Target", 25.0); cards[1] = new GiftCard("Macy’s”, 50.0);
[0] [1] [2] [3]
null null
cards 25.0 "Target" 50.0 "Macy's" store balance
Fall 2020 15-121 (Reid-Miller) 21
Using an Array of Objects
- You can use an an array element (array name with
an index) in the same way you use a variable that is a reference to an object.
- E.g., Add $20 to the gift card at index 1.
cards[1].addMoney(20.0);
That is, cards[1] is a reference to a GiftCard
- bject on which to invoke the addMoney() method.
Fall 2020 15-121 (Reid-Miller) 22
- If program attempts to use null when an object is
required, Java throws a NullPointerException:
- Invoke a method of a null object:
cards[3].buyGoods(30.0); (Error)
- Access or modify a field of a null object:
b = cards[2].balance; (Error)
- Access length or element of a null as if it were
an array:
int[] data; if (data.length > 0) (Error)
NullPointerException
null null
Fall 2020 15-121 (Reid-Miller) 23
Arrays as a Field of a Class
- When a class has a field that is an array, do not create
the array when you declare the field.
- The constructor should create the array.
public class Wallet { GiftCard[] cards; // declare the field … public Wallet(int numCards, String store) { cards = new GiftCard[numCards]; for (i = 0; i < numCards; i++) { cards[i] = new GiftCard(store); } …
Fall 2020 15-121 (Reid-Miller) 24
ContactList
A class to maintain a list of contacts.
Suppose we want to manage a list of contacts.
What is a contact?
- Let's start with a Person class that holds (name-
number) pairs How should we store a list of contacts?
- In an array of Person objects
How long should the array be?
- As long as the number contacts we have
What if we want to be able to add and remove contacts?
- Make the array bigger than the biggest expected
size
Fall 2020 15-121 (Reid-Miller) 26
Use a partially filled array to give the allusion the array can grow and shrink
What does a ContactList class have to remember (fields)?
- an array of contacts
- the number of contacts in the array
What will be the ContactList class invariants?
- All the contacts are at the beginning of the array.
- There are no gaps between contacts.
(If we remove a contact we must make sure the beginning of the array stays full.)
- The number of contacts matches the number of
entries in the contacts array.
Fall 2020 15-121 (Reid-Miller) 27
Let's implement a ContactList class
What operations might we want?
- read a file of contact data
- add a contact
- look up phone number given a name
- edit a contact
- remove a contact
For each public method we need to be sure that the class invariants are maintained by the end of the method.
Fall 2020 15-121 (Reid-Miller) 28