|
1
|
- A Collection class is a data type that is capable of holding a group of
items.
- In Java, Collection classes can be implemented as a class, along with
methods to add, remove, and examine items.
|
|
2
|
- For the first example, think about a bag.
|
|
3
|
- For the first example, think about a bag.
- Inside the bag are some numbers.
|
|
4
|
- When you first begin to use a bag, the bag will be empty.
- We count on this to be the initial state of any bag that we use.
|
|
5
|
- Numbers may be added into a bag.
|
|
6
|
- Numbers may be added into a bag.
|
|
7
|
- Numbers may be added into a bag.
- The bag can hold many numbers.
|
|
8
|
- Numbers may be added into a bag.
- The bag can hold many numbers.
|
|
9
|
- Numbers may be added into a bag.
- The bag can hold many numbers.
- We can even place the same number more than once.
|
|
10
|
- Numbers may be added into a bag.
- The bag can hold many numbers.
- We can even place the same number more than once.
|
|
11
|
- We may ask about the contents of the bag.
|
|
12
|
- We may remove a number from a bag.
|
|
13
|
- We may remove a number from a bag.
- But we remove only one number at a time.
|
|
14
|
- Another operation is to determine how many numbers are in a bag.
|
|
15
|
- A bag can be put in its initial state, which is an empty bag.
- Numbers can be added into the bag.
- You may check how many occurrences of a certain number are in the bag.
- Numbers can be removed from the bag.
- You can check how many numbers are in the bag.
|
|
16
|
- Java classes (introduced in Chapter 2) can be used to implement a
Collection class such as a Bag.
- The class definition includes:
|
|
17
|
- Java classes (introduced in Chapter 2) can be used to implement a
Collection class such as a Bag.
- The class definition includes:
|
|
18
|
|
|
19
|
|
|
20
|
- Places a bag in the initial state (an empty bag)
|
|
21
|
- Adds a new number to the bag
|
|
22
|
- Counts how many integers are in the bag.
|
|
23
|
- Counts how many copies of a number occur
|
|
24
|
- Removes one copy of a number
|
|
25
|
- Here is typical code from a program that uses the new Bag class:
|
|
26
|
- The documentation gives specifications for the bag methods.
- Specifications are written as precondition/postcondition contracts.
- Everything needed to use the Bag class is included in this
documentation.
|
|
27
|
- Suppose that a Mysterious Benefactor provides you with the Bag class,
but you are only permitted to read the documentation. You cannot read the class
implementation or .java file. Can
you write a program that uses the Bag data type ?
- Yes I can.
- No. Not unless I see the class
implementation for the Bag.
|
|
28
|
- Suppose that a Mysterious Benefactor provides you with the Bag class,
but you are only permitted to read the documentation. You cannot read the class
implementation or .java file. Can
you write a program that uses the Bag data type ?
- Yes I can.
- You know the name of the new
data type, and you also know the headings and specifications of each of
the operations. This is enough for you to create and use Bags.
|
|
29
|
- The entries of a bag will be stored in the front part of an array, as
shown in this example.
|
|
30
|
- The entries may appear in any order. This represents the same bag as the
previous one. . .
|
|
31
|
- . . . and this also represents the same bag.
|
|
32
|
- We also need to keep track of how many numbers are in the bag.
|
|
33
|
- Use these ideas to write a list of private instance variables could
implement the Bag class. You
should have two instance variables.
|
|
34
|
|
|
35
|
|
|
36
|
|
|
37
|
|
|
38
|
- Make sure there is room for a new entry in the array.
- Place newEntry in the appropriate location of the data array.
- Add one to the instance variable manyItems.
|
|
39
|
- Make sure there is room for a new entry in the array.
- Place newEntry in the appropriate location of the data array.
- Add one to the instance variable manyItems.
|
|
40
|
- Make sure there is room for a new entry in the array.
- Place newEntry in the appropriate location of the data array.
- Add one to the instance variable manyItems.
|
|
41
|
- Read Section 3.2 for the implementations of the other bag methods.
- Remember: If you are just using the Bag class, then you don’t need to
know how the operations are implemented.
- Later we will reimplement the bag using more efficient algorithms.
- We’ll also have a few other operations to manipulate bags.
|
|
42
|
- In this example, we have implemented a bag containing integers.
- But we could have had a bag of float numbers, a bag of characters, a bag
of Strings . . .
|
|
43
|
- A Collection class is a class that can hold a group of items.
- Collection classes can be implemented with a Java class.
- The author of the class should provide documentation that another
programmer can read to use the class.
- Other details are given in Section 3.2, which you should read.
|
|
44
|
|