Java Reference
In-Depth Information
remove(anEntry)
+remove(anEntry: T): boolean
Task: Removes an occurrence of a particular
object from the bag, if possible.
Input: anEntry is an object.
Output: True or false according to whether
the removal succeeds.
clear()
+clear(): void
Task: Removes all objects from the bag.
Input: None.
Output: None.
getFrequencyOf(anEntry) +getFrequencyOf(anEntry: T):
Task: Counts the number of times an object
occurs in the bag.
Input: anEntry is an object.
Output: The integer number of times
anEntry occurs in the bag.
integer
contains(anEntry)
+contains(anEntry: T):
boolean
Task: Tests whether the bag contains a
particular object.
Input: anEntry is an object.
Output: True or false according to whether
anEntry occurs in the bag.
toArray()
+toArray(): T[]
Task: Looks at all objects in the bag.
Input: None.
Output: A new array of entries currently in
the bag.
Design Decision: What should happen when an unusual condition occurs?
You as class designer need to make decisions about how to treat unusual conditions and include
these decisions in your specifications. The documentation for the ADT bag should reflect both
these decisions and the details in the previous discussion.
In general, you can address unusual situations in several ways. Your method could
Assume that the invalid situations will not occur. This assumption is not as naive as it
might sound. A method could state as an assumption—that is, a precondition—restrictions
to which a client must adhere. It is then up to the client to check that the precondition is
satisfied before invoking the method. For example, a precondition for the method remove
might be that the bag is not empty. Notice that the client can use other methods of the
ADT bag, such as isEmpty and getCurrentSize , to help with this task. As long as the cli-
ent obeys the restriction, the invalid situation will not occur.
 
 
Search WWH ::




Custom Search