collection of objects
play

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


  1. Collection of Objects 15-121 Fall 2020 Margaret Reid-Miller

  2. 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 implementation Fall 2020 15-121 (Reid-Miller) 2

  3. Primitive type variables hold values (e.g., int, double, char) Fall 2020 15-121 (Reid-Miller) 3

  4. Primitive Types • Variables of primitive types name a storage location in memory in which we can store a value. double balance1 = 1000.0; 1000.0 1000.0 balance1 Fall 2020 15-121 (Reid-Miller) 4

  5. Primitive Types • 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. double balance1 = 1000.0; double balance2; 1000.0 1000.0 balance1 balance2 Fall 2020 15-121 (Reid-Miller) 5

  6. Primitive Types • Assigning the value of the one variable to another copies the value: double balance1 = 1000.0; double balance2; balance2 = balance1; 1000.0 balance1 balance2 1000.0 Fall 2020 15-121 (Reid-Miller) 6

  7. Primitive Types • You can assign a new value to a variable. The previous value is lost. double balance1 = 1000.0; double balance2; balance2 = balance1; balance1 = 500; 1000.0 500.0 balance1 balance2 1000.0 Fall 2020 15-121 (Reid-Miller) 7

  8. Object type variables hold references to objects. Fall 2020 15-121 (Reid-Miller) 8

  9. Object Types • Alice gets a $100 gift card from Target. GiftCard alice = new GiftCard("Macy ' s", 100.0); ; (60.0); ; alice GiftCard String name " Macy ' s " 100.0 balance • Object type variables also name a memory location. But the memory is too small to hold an object. It can only hold a reference (pointer) to the object. Fall 2020 15-121 (Reid-Miller) 9

  10. Object References • Bob takes Alice’s gift card. GiftCard alice = new GiftCard("Macy's", 100.0); GiftCard bob = alice; (60.0); ; alice GiftCard String name "Macy's" bob 100.0 balance 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

  11. Object References • Bob spends $60. Alice can see that her card now has only $40. GiftCard alice = new GiftCard("Macy's", 100.0); GiftCard bob = alice; bob.buyGoods(60.0); ; alice GiftCard String name "Macy's" bob 40.0 balance Fall 2020 15-121 (Reid-Miller) 11

  12. Object References • Alice buys a $75 gift card from Target. GiftCard alice = new GiftCard("Macy's", 100.0); GiftCard bob = alice; bob.buyGoods(60.0); alice = new GiftCard("Target", 75.0); alice GiftCard String name "Macy's" bob 40.0 balance GiftCard String name "Target" balance 75.0 Fall 2020 15-121 (Reid-Miller) 12

  13. Object References • Bob takes Alice’s Target card and loses Macy’s card. GiftCard alice = new GiftCard("Macy's", 100.0); GiftCard bob = alice; bob.buyGoods(60.0); alice = new GiftCard("Target", 75.0); bob = alice; alice GiftCard String name "Macy's" bob 40.0 balance GiftCard String name "Target" balance 75.0 Fall 2020 15-121 (Reid-Miller) 13

  14. Garbage • 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. alice GiftCard String name "Macy's" bob 40.0 balance GiftCard String name "Target" balance 75.0 Fall 2020 15-121 (Reid-Miller) 14

  15. Garbage Collector • 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.) alice Compliments of GiftCard String the garbage name "Macy's" collector! bob balance 40.0 GiftCard String store "Target" balance 75.0 Fall 2020 15-121 (Reid-Miller) 15

  16. Object Types as Parameters • An object type parameter is an alias of the argument. GiftCard alice = new GiftCard("Macy's", 50.0); goShopping(alice); "Macy's" alice 50.0 copies the reference card public static void goShopping(Giftcard card) { while (card.getBalance > 0) { card.buyGoods(10.0) } } Fall 2020 15-121 (Reid-Miller) 16

  17. The null Pointer If we do not instantiate an object, the variable holds a special value null that represents a nonexisting object. sue null GiftCard sue; If we try to use the variable as an object, we get a NullPointerException at runtime. sue.addMoney(30); Fall 2020 15-121 (Reid-Miller) 17

  18. 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). GiftCard store balance Fall 2020 15-121 (Reid-Miller) 18

  19. Arrays of Objects

  20. 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

  21. 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 "Target" "Macy's" store balance 25.0 50.0 Fall 2020 15-121 (Reid-Miller) 21

  22. 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 object on which to invoke the addMoney() method. Fall 2020 15-121 (Reid-Miller) 22

  23. NullPointerException • 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: null (Error) b = cards[2].balance; • Access length or element of a null as if it were an array: null int[] data; (Error) if (data.length > 0) Fall 2020 15-121 (Reid-Miller) 23

  24. 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

  25. ContactList A class to maintain a list of contacts.

  26. 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

  27. 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

  28. 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

Recommend


More recommend