Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3 . Write a class Collection that represents a collection of items. The class supports six methods: a . _ _ init _ _ (

3. Write a class Collection that represents a collection of items. The class supports six methods:
a.__init__() which takes an optional parameter representing the maximum size of the collection. It sets the maximum size of the collection to the provided value or to the value 10 if no number is given as a parameter. It also creates an empty list to store the items in the
if no number is given as a parameter. It also creates an empty list to store the items in the collection.
b.__contains__() which takes an item as a parameter and returns True if the item is already in the collection and False otherwise. The function must return a Boolean and not a string.
c. add() which adds a new item to the collection. It does this by first checking that the maximum capacity for the collection hasn't been reached. If the maximum capacity has been reached, then the method returns False without modifying the collection. If the maximum capacity has not yet been reached, the method checks that the item is not already in the collection using the __contains__ method described above. If the item is already there, the method returns False and does not modify the collection. If the item is not present and the maximum capacity has not yet been reached, the method adds the item to the collection and returns True. The function must return a Boolean and not a string.
d. size() which returns the number of items currently in the collection.
e.__str__() which returns a string representing the object as seen in the sample output below.
f.__repr__() which returns a string representing the object as seen in the sample output below.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Foundations Technology Fundamentals For IT Success

Authors: Bob Bryla

1st Edition

0782143725, 9780782143720

More Books

Students also viewed these Databases questions