In: Computer Science
Write a class to implement HeadTailListInterface. Instead of using an array, use a List object as your instance data variable. (List (Links to an external site.) from the Java standard library- not ListInterface!). Instantiate the List object to type ArrayList.
Inside the methods of this class, invoke methods on the List object to accomplish the task. Note: some methods might look very simple... this does not mean they are wrong!
There is one difference in how this class will work compared to the other: in this extra credit class, you do not have control over the capacity, so you should not print the capacity in display and the capacity does not have to be exactly doubled in the two add methods.
For full credit:
The class header and instance data will be:
public class ListHeadTailList<T> implements HeadTailListInterface<T> List<T> list; // initialize to type ArrayList<T> in the ListHeadTailList constructor
------------------------------------------------------------------------------------------------------------------------------------------------
/**
* An interface for a list. Entries in a list have positions that
begin with 0.
* Entries can only be removed or added to the beginning and end of
the list.
* Entries can be accessed from any position.
*
* @author Jessica Masters
*/
public interface HeadTailListInterface<T> {
/**
* Adds a new entry to the beginning of the list.
* Entries currently in the list are shifted
down.
* The list size is increased by 1.
*
* @param newEntry The object to be added as a new
entry.
*/
public void addFront(T newEntry);
/**
* Adds a new entry to the end of the list.
* Entries currently in the list are unaffected.
* The list size is increased by 1.
*
* @param newEntry The object to be added as a new
entry.
*/
public void addBack(T newEntry);
/**
* Removes an entry from the beginning of the
list.
* Entries currently in the list are shifted up.
* The list size is decreased by 1.
*
* @return A reference to the removed entry or null if
the list is empty.
*/
public T removeFront();
/**
* Removes an entry from the end of the list.
* Entries currently in the list are unaffected.
* The list size is decreased by 1.
*
* @return A reference to the removed entry or null if
the list is empty.
*/
public T removeBack();
/** Removes all entries from this list. */
public void clear();
/**
* Retrieves the entry at a given position in this
list.
*
* @param givenPosition An integer that indicates the
position of the desired entry.
* @return A reference to the indicated entry or null
if the index is out of bounds.
*/
public T getEntry(int givenPosition);
/**
* Displays the contents of the list to the console, in
order.
*/
public void display();
/**
* Determines the position in the list of a given
entry.
* If the entry appears more than once, the first index
is returned.
*
* @param anEntry the object to search for in the
list.
* @return the first position the entry that was found
or -1 if the object is not found.
*/
public int indexOf(T anEntry);
/**
* Determines the position in the list of a given
entry.
* If the entry appears more than once, the last index
is returned.
*
* @param anEntry the object to search for in the
list.
* @return the last position the entry that was found
or -1 if the object is not found.
*/
public int lastIndexOf(T anEntry);
/**
* Determines whether an entry is in the list.
*
* @param anEntry the object to search for in the
list.
* @return true if the list contains the entry, false
otherwise
*/
public boolean contains(T anEntry);
/**
* Gets the length of this list.
*
* @return The integer number of entries currently in
the list.
*/
public int size();
/**
* Checks whether this list is empty.
*
* @return True if the list is empty, or false if the
list contains one or more elements.
*/
public boolean isEmpty();
}
------------------------------------------------------------------------------------------------------------------------
********TESTING ISEMPTY AND EMPTY DISPLAY Empty is true: true Should display: 0 elements; capacity = 10 0 elements; capacity N/A ********TESTING ADD TO FRONT Should display: 1 elements; capacity = 10 [2] 1 elements; capacity N/A [2] Should display: 3 elements; capacity = 10 [3, 4, 2] 3 elements; capacity N/A [3, 4, 2] Empty is false: false ********TESTING CLEAR Should display: 0 elements; capacity = 10 0 elements; capacity N/A ********TESTING ADD TO BACK Should display: 1 elements; capacity = 10 [7] 1 elements; capacity N/A [7] Should display: 3 elements; capacity = 10 [7, 10, 5] 3 elements; capacity N/A [7, 10, 5] ********TESTING CONTAINS Contains 5 true: true Contains 7 true: true Contains 4 false: false ********TESTING ADD WITH EXPANSION Should display: 32 elements; capacity = 40 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31] 32 elements; capacity N/A [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31] ********TESTING INDEX OF Index of 0 is 0: 0 Index of 31 is 31: 31 Index of -5 is -1: -1 Index of 32 is -1: -1 Index of 3 is 0: 0 Index of 5 is 6: 6 ********TESTING LAST INDEX OF Last index of 0 is 1: 1 Last index of 31 is 32: 32 Last index of -5 is -1: -1 Last index of 35 is -1: -1 Last index of 3 is 4: 4 Last index of 5 is 33: 33 ********TESTING SIZE Size is 34: 34 ********TESTING GET ENTRY Element in position 15 is 14: 14 Element in position 0 is 3: 3 Element in position 39 is null: null Element in position -1 is null: null ********TESTING REMOVES Remove front element 3: 3 Remove back element 5 :5 Remove front element 0: 0 Remove back element 31: 31 Should display: 30 elements; capacity = 40 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30] 30 elements; capacity N/A [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30] Remove element null: null Remove element null: null Remove element 1: 1 Should display: 0 elements; capacity = 40 0 elements; capacity N/A Remove element 1: 1 Should display: 0 elements; capacity = 40 0 elements; capacity N/A Remove element 1: 1 Should display: 0 elements; capacity = 40 0 elements; capacity N/A Remove element 1: 1 Should display: 0 elements; capacity = 40 0 elements; capacity N/A ********TESTING MIX OF ADDS AND REMOVES Should display: 7 elements; capacity = 40 [5, 4, 3, 2, 3, 8, 9] 7 elements; capacity N/A [5, 4, 3, 2, 3, 8, 9] Should display: 5 elements; capacity = 40 [4, 3, 2, 3, 8] 5 elements; capacity N/A [4, 3, 2, 3, 8] ********TESTING WITH STRINGS Should display: 5 elements; capacity = 5 [You, did, it!, Nice, job!] 5 elements; capacity = 5 [You, did, it!, Nice, job!] Contains "Nice" is true: true Contains "You" is true: true Contains "you" is false: false Index of "it!" is 2: 2 Last index of "it!" is 2: 2
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: Since you have not attached the test program, I couldn’t check if the program gives the expected output. But I think everything should work. If you encounter any troubles, share your test code.
// ListHeadTailList.java
import java.util.ArrayList;
import java.util.List;
public class ListHeadTailList<T> implements HeadTailListInterface<T> {
List<T> list;
// constructor initializing empty list
public ListHeadTailList() {
list = new ArrayList<T>();
}
@Override
public void addFront(T newEntry) {
// adding to index 0
list.add(0, newEntry);
}
@Override
public void addBack(T newEntry) {
// adding to the end
list.add(newEntry);
}
@Override
public T removeFront() {
if (isEmpty()) {
// empty
return null;
}
// removing and returning element at index 0
return list.remove(0);
}
@Override
public T removeBack() {
if (isEmpty()) {
return null;
}
// removing and returning last element
return list.remove(list.size() - 1);
}
@Override
public void clear() {
// clearing all elements
list.clear();
}
@Override
public T getEntry(int givenPosition) {
if (givenPosition < 0 || givenPosition >= size()) {
// invalid position
return null;
}
// returning element at given position
return list.get(givenPosition);
}
@Override
public void display() {
// simply printing the list
System.out.println(list);
}
@Override
public int indexOf(T anEntry) {
// returning the first index of anEntry, or -1 if not found
return list.indexOf(anEntry);
}
@Override
public int lastIndexOf(T anEntry) {
// returning the last index of anEntry, or -1 if not found
return list.lastIndexOf(anEntry);
}
@Override
public boolean contains(T anEntry) {
//returns true if list contains anEntry, else false
return list.contains(anEntry);
}
@Override
public int size() {
return list.size();
}
@Override
public boolean isEmpty() {
return list.isEmpty();
}
@Override
public String toString() {
//returning string representation of list
return list.toString();
}
}