Java Reference
In-Depth Information
Imagine that 30 students in room A occupy the desks numbered sequentially from 0 to 29, and
a new student wants to join those students. Since 40 desks are in the room, the desk numbered 30 is
available. We can simply assign the new student to desk 30. When all 40 desks are occupied, we
can no longer accommodate more students. The room is full.
2.3
Removing a particular student. Now imagine that the student in desk 5 of room A drops the
course. Desk 5 stays in its fixed location within the room and will be vacant. If we still want stu-
dents to sit in consecutively numbered desks, however, one student will need to move to desk 5.
Since the students are not in any particular order, if the student in the highest-numbered desk moves
to desk 5, no one else need move. For example, if 30 students are seated in the room in desks 0 to
29, the student in desk 29 would move to desk 5. Desks 29 and above would be vacant.
Question 1 What is an advantage of moving a student as just described so that the vacated
desk does not remain vacant?
Question 2 What is an advantage of leaving the vacated desk vacant?
Question 3 If a student were to drop the course, which one could do so without forcing
another to change desks?
A Group of Core Methods
2.4
The Java array-based implementation for the ADT bag incorporates some of the ideas that our
classroom example illustrates. The result is the class ArrayBag , which implements the interface
BagInterface that you saw in Listing 1-1 of Chapter 1. Each public method within the interface
corresponds to an ADT bag operation. Recall that the interface defines a generic type T for the
objects in a bag. We use this same generic type in the definition of ArrayBag .
The definition for the class ArrayBag could be fairly involved. The class certainly will have
quite a few methods. For such classes, you should not define the entire class and then attempt to test
it. Instead, you should identify a group of core methods to both implement and test before continu-
ing with the rest of the class definition. By leaving the definitions of the other methods for later,
you can focus your attention and simplify your task. But what methods should be part of this
group? In general, such methods should be central to the purpose of the class and allow reasonable
testing. We sometimes will call a group of core methods a core group .
When dealing with a collection such as a bag, you cannot test most methods until you have cre-
ated the collection. Thus, adding objects to the collection is a fundamental operation. If the method
add does not work correctly, testing other methods such as remove would be pointless. Thus, the
bag's add method is part of the group of core methods that we implement first.
To test whether add works correctly, we need a method that allows us to see the bag's contents.
The method toArray serves this purpose, and so it is a core method. The constructors are also fun-
damental and are in the core group. Similarly, any methods that a core method might call are part of
the core group as well. For example, since we cannot add an entry to a full bag, the method add will
need to call isFull .
VideoNote
An array-based bag
2.5
The core methods. We have identified the following core methods to be a part of the first draft of the
class ArrayBag :
Constructors
public boolean add(T newEntry)
public T[] toArray()
public boolean isFull()
 
 
Search WWH ::




Custom Search