For this lab, you will use the example from the book and the AbstractCollection Class as the basis for writing your own ArrayList implementation.
Java library: java.util.AbstractCollection
Create a new Java project in Eclipse, and import your Task
class from the priority queues lab.
You should add a new field to the Task
class: ID
, which should be an integer. This will be used to identify a task by a unique number so that it can be found and removed from your TaskArrayList
.
If your Task
class does not have one yet, I strongly urge you to create a toString
method that will return a string comprised of the ID
, the task priority
, the amount of time for the task
, and the description
. You could just return a string comprised of the ID
and the description
(or make the two methods).
Using today’s reading example (instead of integers you will have Tasks), you should create a new class: TaskArrayList
. This should extend AbstractCollection<Task>
. Do not implement the List interface because that would require many additional methods that we do not need for this exercise.
Your TaskArrayList
will be implemented using an array of Task
objects. This detail is hidden from any programs that use the TaskArrayList
.
Your TaskArrayList
class must include:
Task
arrayTaskArrayList
Task
array with the default capacity and initializes the size to zeroadd
method that will add a Task
to the array. Note that this method must be able to expand the capacity of the array if you try to add an element to a full array. See BJP 15.3 for how to handle this. (When copying elements from one array to another, use Arrays Java methods)TaskArray
removebyID
method that will search for a Task
in the TaskArray
, using the ID
to find a match. If the ID
is found, the removebyID
method should return the Task
it found. if it is not found, return null
. This method must also move all Tasks located at higher indexes down to fill the hole left by the removed Task
. And it should reduce the size of the TaskArrayList
too.toString
method? Why or Why no?You should create a driver program (TaskArrayListTester
) that will create TWO TaskArrayLists. One is for your current task list, and one is for completed tasks.
Add enough tasks that you know the add
method has to increase the capacity of the underlying array at least once. So, if your default capacity is 10, add 11 or more tasks.
Print out the TaskArrayList
to confirm that you have been successful. This is where the toString
and printTaskArray
methods come in handy.
Then, remove several tasks from the current TaskArrayList
and add them to the completed TaskArrayList
.
Print both current and completed TaskArrayLists to confirm the programs are working correctly.
Instead of printing in the console, write the output to a file, see example Redirecting System.out.println() output to a file in Java
Submit:
Remember: Do not use magic numbers, write your code anticipating errors and print user-friendly error messages, all your public methods should be well documented (use Javadoc comments). Please include comments or organize your code and report in a way that will help the grader to find the answer to the exercises or posted questions.
Implement the iterator
method. You will need to create an inner class that implements the Iterator interface. See BJP 15.3 for how to handle this.