CPE
102
Winter
2008
Lab
6
Objectives
- To gain
experience developing several Comparator classes to use for sorting.
- To gain
experience using Java Standard Library sort methods.
Resources
Ground
Rules
Orientation
You will be developing a class that
represents a list of random Integer class objects. The
class will support sorting using the appropriate Java Standard Library sort
methods found in the Arrays or Collections classes. The class will support natural
ordering by using the compareTo method
supported by the Integer class and other ordering by implementing three
different classes that implement the Comparator interface.
The class will also have a method to determine whether or not the list is
sorted in a particular order and will implement the toString
method so the contents of the list can be examined easily.
Remember
to implement all functionality in a test-driven development fashion; write a
test case for each individual piece of functionality, followed by the
implementation.
Specification
- Implement a
class called IntegerList that
supports a specified number of randomly initialized Integer objects as
follows:
- The class
should have the minimally necessary instance variable(s) to support the
specified functionality.
- Implement a single
constructor that accepts an int-parameter specifying the number
of Integer objects the list should contain and a long-parameter
specifying the seed to use when generating random numbers to populate the
list with. The constructed object should contain the specified
number of Integer objects that have been initialized with random integer values.
- Implement a
method called sort with no parameters and a return type of
void. The method will sort the list of Integer objects using the
appropriate method from the Arrays or Collections class.
- Implement a
method called sort with one parameter of type
Comparator<Integer> and a return type of void. The method
will sort the list of Integer objects using the appropriate method from
the Arrays or Collections class.
- Implement a
method called isSorted with a
single parameter of type Comparator<Integer> and a return type of boolean. The method
will check to see if the list is in the order specified by the provided
Comparator. If the Comparator parameter is null the method will
check to see if the list is in the natural order specified by the compareTo method of the Integer class. The
method returns true if the list is in the specified order, otherwise
false.
- Override the toString method inherited from Object
so that it returns a String containing each integer value on its own
line. If the list is empty the method should return an empty
String.
- Implement a
class called Descending that implements the Comparator<Integer>
interface so that it results in Integers being ordered in descending
order.
- Implement a
class called OddEvenAscending that
implement the Comparator<Integer> interface so that it
results in Integers being ordered so that all odds come before all evens
and within odds or evens the values are ascending.
- Implement a
class called OddEvenDescending
that implement the Comparator<Integer> interface so
that it results in Integers being ordered so that all odds come before all
evens and within odds or evens the values are descending.
- Implement JUnit tests that demonstrates that your implementation of the four classes
above are complete and correct.