3. Collections
Feel free to use your laptop
You are strongly encourage to work with others
When you get stuck, ask those sitting around you for help
Get used to working together in the labs
Peer teaching and peer learning has been empirically shown to be very effective
3.1. Creating Objects
The goal is to make a program that will keep track of a suite of courses. To do this, two classes will be written —
one for keeping track of the information about a single course, and another for keeping track of multiple courses. In
this lab, the CourseList class will be created as the Course class was written in the previous lab.
3.1.1. Course List Class
A CourseList will be a collection of Course objects.
Refer to the data structures review topic for guidance on
creating this class. It is recommended to go through the topic and refactor the relevant code to fit the requirements of
this lab.
Make a
CourseListclass that has two private fields (list&size) and a constant for a default capacitylistwill be an array that holds references toCourseobjectssizewill be the number ofCourseobjects currently inlistRemember, the capacity of the array and
sizeare not the same thing
DEFAULT_CAPACITYis a constant that will be used for creating thelistarray if no capacity is provided
Write two constructors
One takes no parameter and makes
listbased onDEFAULT_CAPACITYThe other takes a parameter
initialCapacityfor the starting capacity oflist
Write an
addmethod that takes aCourseobject and adds it to the collectionThis will require a mechanism to “expand the capacity” of the array to work properly
Write a
containsmethod that takes aCourseas a parameter and returnstrueif it exists in the collection andfalseotherwiseWrite a
indexOfmethod that returns the index of a specifiedCourseobjectIf no such
Courseexists, this method should throw an exception
Write a
removemethod that takes aCourseto remove from the collection as a parameterIf no such
Courseexists, this method should throw an exceptionMake use of the
Courseobject’sequalsmethod
Write a
getmethod that returns theCourseobject at the specified index in theCourseListIf the provided index is out of bounds, this method should throw an exception
Write a
sizemethod to return the number ofCourseobjects in the collectionWrite a
toStringfor the classTest the
CourseListclass by creating an instance of it and using the methodsBe sure to check all methods