Lab: 27 - Task ArrayList

Assigned
Wednesday, 1 November 2023

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

Getting Started

Create a new Java project in Eclipse, and import your Task class from the priority queues lab.

Modify the Task Class

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).

Create a TaskArrayList Class

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:

  • a private field for the default capacity of the underlying Task array
  • a private array of Tasks
  • a private field for the size, which is the actual number of tasks added to the TaskArrayList
  • a constructor, that creates the Task array with the default capacity and initializes the size to zero
  • an add 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)
  • a size method that returns the current number of elements in the TaskArray
  • a 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.
  • Do you need a toString method? Why or Why no?

Create a Tester/Client Program

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

Lab Submission

Submit:

  • (3 points) TaskArrayList.java
  • (1 point) The output of your client program, which must include:
    • Printing the current TaskArrayList after adding 10+ tasks
    • Printing both the current and completed TaskArrayLists after you have removed several tasks from current and added them to completed.

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.

For those with extra time

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.