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
CourseList
class that has two private fields (list
&size
) and a constant for a default capacitylist
will be an array that holds references toCourse
objectssize
will be the number ofCourse
objects currently inlist
Remember, the capacity of the array and
size
are not the same thing
DEFAULT_CAPACITY
is a constant that will be used for creating thelist
array if no capacity is provided
Write two constructors
One takes no parameter and makes
list
based onDEFAULT_CAPACITY
The other takes a parameter
initialCapacity
for the starting capacity oflist
Write an
add
method that takes aCourse
object and adds it to the collectionThis will require a mechanism to “expand the capacity” of the array to work properly
Write a
contains
method that takes aCourse
as a parameter and returnstrue
if it exists in the collection andfalse
otherwiseWrite a
indexOf
method that returns the index of a specifiedCourse
objectIf no such
Course
exists, this method should throw an exception
Write a
remove
method that takes aCourse
to remove from the collection as a parameterIf no such
Course
exists, this method should throw an exceptionMake use of the
Course
object’sequals
method
Write a
get
method that returns theCourse
object at the specified index in theCourseList
If the provided index is out of bounds, this method should throw an exception
Write a
size
method to return the number ofCourse
objects in the collectionWrite a
toString
for the classTest the
CourseList
class by creating an instance of it and using the methodsBe sure to check all methods