Question: File: baginterface.py Author: Ken Lambert class BagInterface(object): Interface for all bag types. # Constructor def __init__(self, sourceCollection = None): Sets the initial state of
File: baginterface.py Author: Ken Lambert """
class BagInterface(object): """Interface for all bag types."""
# Constructor def __init__(self, sourceCollection = None):
"""Sets the initial state of self, which includes the contents of sourceCollection, if its present.""" pass
# Accessor methods def isEmpty(self):
"""Returns True if len(self) == 0, or False otherwise.""" return True
def __len__(self): """Returns the number of items in self.""" return 0
def __str__(self): """Returns the string representation of self.""" return ""
def __iter__(self): """Supports iteration over a view of self.""" return None
Developing an Interface 139
def __add__(self, other): """Returns a new bag containing the contents of self and other.""" return None
def __eq__(self, other): """Returns True if self equals other, or False otherwise.""" return False
# Mutator methods def clear(self):
"""Makes self become empty.""" pass
def add(self, item): """Adds item to self.""" pass
def remove(self, item): """Precondition: item is in self. Raises: KeyError if item in not in self. Postcondition: item is removed from self.""" pass
Now that you have a handy blueprint for all bags, you are ready to consider some bag implementations. In the next two sections, you develop an array-based bag collection and a link-based bag collection.
1. Do the items in a bag have a position, or are they unordered? 2. Which operations appear in the interface of any collection? 3. Which method is responsible for creating a collection object?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
