Assignment 1: The List is Life

Due by: Friday, September 6, 2024 at 11:59 p.m.

Your mission, should you choose to accept it, is to implement a version of the ArrayList class from the Java Collections Framework. Your class will be called NewList. This assignment is intended to get you used to implementing Java data structures.

Specification

The NewList class uses a dynamic array to hold an arbitrary number of String values. It has a size which is the current number of elements being stored in the list.

You are not required to implement the entire ArrayList class. First of all, your list data structure will not be generic, and it will hold only String values. Also, you are only required to implement the following methods.

  • NewList()
    Implement the default constructor to create an empty NewList. A new NewList should create an array with the capacity to hold 10 items.

  • void add(String element)
    Add element to the last position in the list. This method may require your array to be resized.

  • void add(int index, String element)
    Add element element to the list at position index, moving elements at index and higher to the following locations. This method may require your array to be resized.

  • void addAll(NewList list)
    Add all the elements from list to the end of your list. This method may require your array to be resized.

  • void clear()
    Remove all elements from your list.

  • boolean contains(String element)
    Return true if your list contains element and false otherwise. Remember to compare String values using the equals() method.

  • String get(int index)
    Return the element at position index.

  • int indexOf(String element)
    Return the index of the first occurrence of element or -1 if your list does not contain it.

  • boolean isEmpty()
    Return true if your list contains no items and false otherwise.

  • int lastIndexOf(String element)
    Return the index of the last occurrence of element or -1 if your list does not contain it.

  • String remove(int index)
    Remove the element at position index from your list and return it. All the elements after index will each move to the location before.

  • void set(int index, String element)
    Change the element at position index to element.

  • int size()
    Return the size of the list.

For more information, refer to the documentation for ArrayList here. Whenever possible, adopt the approach used by the designers of the Java Collections Framework.

Restrictions

You may not import any packages or classes.

Your class must be named NewList and be submitted in NewList.java, without any package.

Add your methods to the following class skeleton.

public class NewList {

	private String[] array;	
	private int size;

	...
}

Hints

Test your code thoroughly. You are encouraged to write JUnit test cases to test the functionality of NewList, but you should not turn them in. I have provided an example of testing code here. If you use it, be sure to add JUnit 5 libraries, not JUnit 4. These tests test some but not all of the functionality of NewList.

All indexes are zero-based.

If any method tries to access an index that is not legal, you should throw an IndexOutOfBoundsException exception. Users are allowed to add to the index immediately after the last valid index with the add(int index, String element) method, but no higher.

The methods listed above are the only public methods you should have. However, it may be convenient to write private helper methods to accomplish your goals with maximum code reuse. It is also possible to call some of these public methods from others, if that improves your design.

When increasing the size of your internal array, create a new array that's twice as long as the current one. Then, copy the contents of the current array into the new array and point the array member at the new array.

You should not have a main() method anywhere in the NewList class. It is a utility class that programmers will use whenever they want to store a list of String values.

All member variables must be private. You only need the two member variables above for a reasonably efficient implementation, but you are permitted to add more if you can justify them.

Turn In

Upload NewList.java to Brightspace. Your assignment must be submitted by Friday, September 6, 2024 at 11:59 p.m. Grace days are not available for assignments.

All work must be done individually. You may discuss general concepts with your classmates. There are many array-based list implementations available on the Internet, but you should avoid using them as a reference until after you have turned in your assignment. Please refer to the course policies if you have any questions about academic integrity. If you have trouble with the assignment, I am always available for assistance.

Under no circumstances should any student look at the code written by another student. Tools will be used to detect code similarity automatically.

Grading

There is a constructor and 12 methods specified above. The constructor is worth 6%. Each correctly completed method will earn you 7% of the total. Be sure that methods that are used repeatedly (e.g., constructors) are well written so that testing of other methods is never hampered. A final 10% will be awarded based on good commenting and style, as discussed here. Be sure to put your name, date, course number, and assignment number in comments at the top of your files. Failure to compile will result in a zero for the assignment.