Java Reference
In-Depth Information
1.10
The remaining behaviors do not change the contents of the bag. One of these behaviors counts the
number of times a given object occurs within the bag. We specify it first in pseudocode and then in
UML, as follows:
// Counts the number of times a given entry appears in the bag.
getFrequencyOf(anEntry)
+getFrequencyOf(anEntry: T): integer
Another method tests whether the bag contains a given object. Its specifications in pseudocode
and UML are
// Tests whether the bag contains a given entry.
contains(anEntry)
+contains(anEntry: T): boolean
Question 5 Given the full bag aBag that you created in Question 1, write some pseudocode
statements to find the number of times, if any, that the string " Hello " occurs in aBag .
1.11
Finally, we want to look at the contents of the bag. Rather than providing a method that displays the
entries in the bag, we will define one that returns an array of these entries. The client is then free to
display any or all of them in any way desired. Here are the specifications for our last method:
// Looks at all entries in the bag.
toArray()
+toArray(): T[]
When a method returns an array, it usually should define a new one to return. We will note that
detail for this method.
1.12
As we developed the previous specifications for the bag's methods, we represented them using
UML notation. Figure 1-2 shows the result of doing so.
FIGURE 1-2
UML notation for the class Bag
Bag
+getCurrentSize(): integer
+isFull(): boolean
+isEmpty(): boolean
+add(newEntry: T): boolean
+remove(): T
+remove(anEntry: T): boolean
+clear(): void
+getFrequencyOf(anEntry: T): integer
+contains(anEntry: T): boolean
+toArray(): T[]
Notice that the CRC card and the UML do not reflect all of the details, such as assumptions
and unusual circumstances, that we mentioned in our previous discussion. However, after you have
 
 
Search WWH ::




Custom Search