public class ArrayHeadTailList<T> implements HeadTailListInterface<T>
You are given an interface for a type of list. The list works like this:
Write a class that implements this interface. The class uses arrays to implement the list.
Your class header and instance data variables will be:
public class ArrayHeadTailList<T> implements HeadTailListInterface<T> private T[] listArray; private int numberOfElements;
Your class must compile and have the following implemented methods. Follow the API descriptions from the interface file and the additional characteristics listed below.
/**
* 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();
}
==================================================================
public class ProjectBTester {
public static void main(String[] args) {
HeadTailListInterface<Integer> list = new ArrayHeadTailList<Integer>(10);
// comment the line above and un-comment the line below to test the extra credit
// NOTE: for the extra credit, all lines should match except for the capacity print out-
// the capacity of an ArrayList is private, so this cannot be shown
// HeadTailListInterface<Integer> list = new ListHeadTailList<Integer>(10);
System.out.println("********TESTING ISEMPTY AND EMPTY DISPLAY");
System.out.println("Empty is true: " + list.isEmpty());
System.out.println();
System.out.println("Should display:\n0 elements; capacity = 10");
list.display();
System.out.println();
System.out.println("\n\n********TESTING ADD TO FRONT");
// test addFront to emtpy
list.addFront(2);
System.out.println("Should display:\n1 elements; capacity = 10 [2]");
list.display();
System.out.println();
// test addFront to not empty
list.addFront(4);
list.addFront(3);
System.out.println("Should display:\n3 elements; capacity = 10 [3, 4, 2]");
list.display();
System.out.println();
System.out.println("Empty is false: " + list.isEmpty());
System.out.println("\n\n********TESTING CLEAR");
list.clear();
System.out.println("Should display:\n0 elements; capacity = 10");
list.display();
System.out.println("\n\n********TESTING ADD TO BACK");
// test addBack to empty
list.addBack(7);
System.out.println("Should display:\n1 elements; capacity = 10 [7]");
list.display();
System.out.println();
// test addBack to non empty
list.addBack(10);
list.addBack(5);
System.out.println("Should display:\n3 elements; capacity = 10 [7, 10, 5]");
list.display();
System.out.println("\n\n********TESTING CONTAINS");
System.out.println("Contains 5 true: "+list.contains(5));
System.out.println("Contains 7 true: "+list.contains(7));
System.out.println("Contains 4 false: "+list.contains(4));
System.out.println("\n\n********TESTING ADD WITH EXPANSION");
list.clear();
for(int i=0; i<32; i++) {
list.addBack(i);
}
System.out.println("Should display:\n32 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]");
list.display();
System.out.println("\n\n********TESTING INDEX OF");
System.out.println("Index of 0 is 0: " + list.indexOf(0));
System.out.println("Index of 31 is 31: " + list.indexOf(31));
System.out.println("Index of -5 is -1: " + list.indexOf(-5));
System.out.println("Index of 32 is -1: " + list.indexOf(32));
list.addFront(3);
list.addBack(5);
System.out.println("Index of 3 is 0: " + list.indexOf(3));
System.out.println("Index of 5 is 6: " + list.indexOf(5));
System.out.println("\n\n********TESTING LAST INDEX OF");
System.out.println("Last index of 0 is 1: " + list.lastIndexOf(0));
System.out.println("Last index of 31 is 32: " + list.lastIndexOf(31));
System.out.println("Last index of -5 is -1: " + list.lastIndexOf(-5));
System.out.println("Last index of 35 is -1: " + list.lastIndexOf(35));
System.out.println("Last index of 3 is 4: " + list.lastIndexOf(3));
System.out.println("Last index of 5 is 33: " + list.lastIndexOf(5));
System.out.println("\n\n********TESTING SIZE");
System.out.println("Size is 34: " + list.size());
System.out.println();
System.out.println("\n********TESTING GET ENTRY");
System.out.println("Element in position 15 is 14: "+list.getEntry(15));
System.out.println("Element in position 0 is 3: "+list.getEntry(0));
System.out.println("Element in position 39 is null: "+list.getEntry(39));
System.out.println("Element in position -1 is null: "+list.getEntry(-1));
System.out.println("\n\n********TESTING REMOVES");
// test removes from nonEmpty
System.out.println("Remove front element 3: "+list.removeFront());
System.out.println("Remove back element 5 :"+list.removeBack());
System.out.println("Remove front element 0: "+list.removeFront());
System.out.println("Remove back element 31: "+list.removeBack());
System.out.println();
System.out.println("Should display:\n30 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]");
list.display();
System.out.println();
// test removes from empty
list.clear();
System.out.println("Remove element null: "+list.removeFront());
System.out.println("Remove element null: "+list.removeBack());
System.out.println();
// test removes from singleton
list.clear();
list.addFront(1);
System.out.println("Remove element 1: "+list.removeFront());
System.out.println("Should display:\n0 elements; capacity = 40");
list.display();
System.out.println();
System.out.println();
list.addBack(1);
System.out.println("Remove element 1: "+list.removeFront());
System.out.println("Should display:\n0 elements; capacity = 40");
list.display();
System.out.println();
System.out.println();
list.addFront(1);
System.out.println("Remove element 1: "+list.removeBack());
System.out.println("Should display:\n0 elements; capacity = 40");
list.display();
System.out.println();
System.out.println();
list.addBack(1);
System.out.println("Remove element 1: "+list.removeBack());
System.out.println("Should display:\n0 elements; capacity = 40");
list.display();
System.out.println();
System.out.println();
System.out.println("\n\n********TESTING MIX OF ADDS AND REMOVES");
list.addFront(3);
list.addBack(2);
list.addFront(4);
list.addFront(5);
list.addBack(3);
list.addBack(8);
list.addBack(9);
System.out.println("Should display:\n7 elements; capacity = 40 [5, 4, 3, 2, 3, 8, 9]");
list.display();
System.out.println();
list.removeFront();
list.removeBack();
System.out.println("Should display:\n5 elements; capacity = 40 [4, 3, 2, 3, 8]");
list.display();
System.out.println();
System.out.println("********TESTING WITH STRINGS");
HeadTailListInterface<String> wordList = new ArrayHeadTailList<String>(4);
wordList.addFront("job!");
wordList.addFront("Nice");
wordList.addFront("it!");
wordList.addFront("did");
wordList.addFront("You");
System.out.println("Should display:\n5 elements; capacity = 8 [You, did, it!, Nice, job!]");
wordList.display();
System.out.println();
System.out.println("Contains \"Nice\" is true: "+ wordList.contains(new String("Nice")));
System.out.println("Contains \"You\" is true: "+ wordList.contains(new String("You")));
System.out.println("Contains \"you\" is false: "+ wordList.contains(new String("you")));
System.out.println();
System.out.println("Index of \"it!\" is 2: "+ wordList.indexOf(new String("it!")));
System.out.println("Last index of \"it!\" is 2: "+ wordList.lastIndexOf(new String("it!")));
}
}
Program Files:
HeadTailListInterface.java
/**
* 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.
*/
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();
}

ArrayHeadTailList.java
/**
* A class that implements the HeadTailListInterface
interface.
* The class uses Arrays to implement the list.
*
*/
import java.util.Arrays;
public class ArrayHeadTailList<T> implements
HeadTailListInterface<T> {
private T[] listArray;
private int numberOfElements;
private boolean integrityOK;
private static final int MAX_CAPACITY = 10000;
//constructor:
public ArrayHeadTailList(int initialCapacity) {
if(initialCapacity < MAX_CAPACITY) {
@SuppressWarnings("unchecked")
T[] tempList = (T[])new Object[initialCapacity];
listArray = tempList;
numberOfElements = 0;
integrityOK = true;
}else {
integrityOK = false;
checkCapacity(initialCapacity);
}
}
//private helper methods:
private void checkCapacity(int capacity) {
if(capacity > MAX_CAPACITY) {
throw new IllegalStateException("Max capacity is 10000.");
}
}
private void checkIntegrity() {
if(!integrityOK) {
throw new SecurityException("ArrayHeadTailList has not been
instantiated.");
}
}
private void doubleCapacity() {
int newLength = listArray.length * 2;
checkCapacity(newLength);
listArray = Arrays.copyOf(listArray, newLength);
}
private boolean isArrayFull() {
return numberOfElements >= listArray.length;
}
//inherited methods:
@Override
public void addFront(T newEntry) {
checkIntegrity();
if(isArrayFull()) {
doubleCapacity();
}
for(int i = numberOfElements - 1; i >= 0; i--) {
listArray[i +1] = listArray[i];
}
listArray[0] = newEntry;
numberOfElements++;
}
@Override
public void addBack(T newEntry) {
checkIntegrity();
if(isArrayFull()) {
doubleCapacity();
}
listArray[numberOfElements] = newEntry;
numberOfElements++;
}
@Override
public T removeFront() {
checkIntegrity();
if(!isEmpty()) {
T elementToRemove = listArray[0];
listArray[0] = null;
for(int i = 0; i < numberOfElements -1; i++) {
listArray[i] = listArray[i+1];
}
listArray[numberOfElements -1] = null;
numberOfElements--;
return elementToRemove;
}
return null;
}
@Override
public T removeBack() {
checkIntegrity();
if(!isEmpty()) {
T elementToRemove = listArray[numberOfElements - 1];
listArray[numberOfElements - 1] = null;
numberOfElements --;
return elementToRemove;
}
return null;
}
@Override
public void clear() {
checkIntegrity();
for(int i = 0 ; i < numberOfElements ; i ++) {
listArray[i] = null;
}
numberOfElements = 0;
}
@Override
public T getEntry(int givenPosition) {
checkIntegrity();
if(givenPosition >= 0 && givenPosition <
numberOfElements) {
return listArray[givenPosition];
}
return null;
}
@Override
public void display() {
//trimming array to size for printing
@SuppressWarnings("unchecked")
T[] trimmedArray = (T[]) new Object[numberOfElements];
int trimmedLength = numberOfElements;
trimmedArray = Arrays.copyOf(listArray, trimmedLength);
String s = numberOfElements + " elements; capacity = " +
listArray.length + "\t" + Arrays.toString(trimmedArray);
System.out.println(s);
}
@Override
public int indexOf(T anEntry) {
checkIntegrity();
int index = -1;
if(!isEmpty()){
for(int i = 0; i< numberOfElements; i++) {
if(listArray[i].equals(anEntry)) {
index = i;
return index;
}
}
}
return index;
}
@Override
public int lastIndexOf(T anEntry) {
checkIntegrity();
int index = -1;
if(!isEmpty()) {
for(int i = numberOfElements - 1; i >= 0; i--) {
if(listArray[i].equals(anEntry)) {
index = i;
return index;
}
}
}
return index;
}
@Override
public boolean contains(T anEntry) {
checkIntegrity();
return indexOf(anEntry) > -1;
}
@Override
public int size() {
return numberOfElements;
}
@Override
public boolean isEmpty() {
return numberOfElements == 0;
}
}



LinkedHeadTailList.java
public class LinkedHeadTailList<T extends Comparable<? super T>> implements HeadTailListInterface<T>, Comparable<LinkedHeadTailList<T>> {
private Node head, tail;
public LinkedHeadTailList() {
head = null;
tail = null;
}
@Override
public void addFront(T newEntry) {
Node newNode = new Node(newEntry);
if(isEmpty()) {
addingToEmpty(newNode);
}else {
newNode.next = head;
head = newNode;
}
}
@Override
public void addBack(T newEntry) {
Node newNode = new Node(newEntry);
if(isEmpty()) {
addingToEmpty(newNode);
}else {
tail.next = newNode;
tail = newNode;
}
}
private void addingToEmpty(Node newNode) {
head = newNode;
tail = newNode;
}
@Override
public T removeFront() {
T toRemove = null;
if(!isEmpty()) {
if(size() == 1) {
tail = tail.next;
}
toRemove = head.data;
head = head.next;
}
return toRemove;
}
@Override
public T removeBack() {
T toRemove = null;
if(!isEmpty()) {
toRemove = tail.data;
if(size() == 1) {
head = head.next;
}else {
Node newTail = head;
while(newTail.next != tail) {
newTail = newTail.next;
}
newTail.next = null;
tail = newTail;
}
}
return toRemove;
}
@Override
public void clear() {
head = null;
tail = null;
}
@Override
public T getEntry(int givenPosition) {
T entry = null;
if(givenPosition > -1 && givenPosition < size())
{
Node currentNode = head;
int index = -1;
while(currentNode != null) {
index++;
if(index == givenPosition) {
entry = currentNode.data;
}
currentNode = currentNode.next;
}
}
return entry;
}
@Override
public void display() {
if(!isEmpty()) {
String data = "";
String headAndTail = "head=" + head.data + " tail=" +
tail.data;
Node current = head;
while(current != null) {
data = data + current.data + ", ";
current = current.next;
}
data = data.substring(0, data.length() - 2);
System.out.println("[" + data + "] \t" + headAndTail);
}else { //isEmpty()
System.out.println("[]");
}
}
@Override
public int indexOf(T anEntry) {
int indexFound = -1;
int index = -1;
Node currentNode = head;
while(currentNode != null) {
index++;
if(currentNode.data.equals(anEntry)){
indexFound = index;
return indexFound;
}
currentNode = currentNode.next;
}
return indexFound;
}
@Override
public int lastIndexOf(T anEntry) {
int indexFound = -1;
int index = -1;
Node currentNode = head;
while(currentNode != null) {
index++;
if(currentNode.data.equals(anEntry)){
indexFound = index;
}
currentNode = currentNode.next;
}
return indexFound;
}
@Override
public boolean contains(T anEntry) {
Node currentNode = head;
while(currentNode != null) {
if(currentNode.data.equals(anEntry)) {
return true;
}
currentNode = currentNode.next;
}
return false;
}
@Override
public int size() {
int counter = 0;
Node currentNode = head;
while(currentNode != null) {
counter++;
currentNode = currentNode.next;
}
return counter;
}
@Override
public boolean isEmpty() {
return head==null;
}
public int compareTo(LinkedHeadTailList<T> otherList) {
Node thisCurrent = head;
Node otherCurrent = otherList.head;
Integer thisSize = size();
Integer otherListSize = otherList.size();
while(thisCurrent != null && otherCurrent != null) {
int elementComparison =
thisCurrent.data.compareTo(otherCurrent.data);
if(elementComparison > 0) {
return 1;
}else if(elementComparison < 0) {
return -1;
}else {// elementComparison == 0;
thisCurrent = thisCurrent.next;
otherCurrent = otherCurrent.next;
}
}
return Integer.compare(thisSize, otherListSize);
}
private class Node {
private T data;
private Node next;
private Node(T dataPortion) {
data = dataPortion;
next = null;
}
private Node(T dataPortion, Node nextNode) {
data = dataPortion;
next = nextNode;
}
private T getData() {
return data;
}
private void setData(T newData) {
data = newData;
}
private Node getNextNode() {
return next;
}
private void setNextNode(Node nextNode) {
next = nextNode;
}
}
}



ListHeadTailList.java
/**
* A class that implements the HeadTailListInterface
interface.
* The class uses ArrayList to implement the list.
*/
import java.util.*;
public class ListHeadTailList<T> implements
HeadTailListInterface<T> {
private List<T> list;
private boolean integrityOK;
private static final int MAX_CAPACITY = 10000;
//constructor:
public ListHeadTailList(int initialCapacity) {
if(initialCapacity < MAX_CAPACITY) {
list = new ArrayList<>(initialCapacity);
integrityOK = true;
}else {
integrityOK = false;
checkCapacity(initialCapacity);
}
}
//private helper methods:
private void checkCapacity(int capacity) {
if(capacity > MAX_CAPACITY) {
throw new IllegalStateException("Max capacity is 10000.");
}
}
private void checkIntegrity() {
if(!integrityOK) {
throw new SecurityException("ArrayHeadTailList has not been
instantiated.");
}
}
@Override
public void addFront(T newEntry) {
checkIntegrity();
list.add(0, newEntry);
}
@Override
public void addBack(T newEntry) {
checkIntegrity();
list.add(newEntry);
}
@Override
public T removeFront() {
checkIntegrity();
if(!isEmpty()) {
return list.remove(0);
}
return null;
}
@Override
public T removeBack() {
checkIntegrity();
if(!isEmpty()) {
return list.remove(list.size() - 1);
}
return null;
}
@Override
public void clear() {
checkIntegrity();
list.clear();
}
@Override
public T getEntry(int givenPosition) {
checkIntegrity();
if(givenPosition >= 0 && givenPosition < list.size())
{
return list.get(givenPosition);
}
return null;
}
@Override
public void display() {
String s = list.size() + " elements; \t " + list;
System.out.println(s);
}
@Override
public int indexOf(T anEntry) {
checkIntegrity();
return list.indexOf(anEntry);
}
@Override
public int lastIndexOf(T anEntry) {
checkIntegrity();
return list.lastIndexOf(anEntry);
}
@Override
public boolean contains(T anEntry) {
checkIntegrity();
return list.contains(anEntry);
}
@Override
public int size() {
return list.size();
}
@Override
public boolean isEmpty() {
return list.size() == 0;
}
}


ProjectBTester.java
public class ProjectBTester {
public static void main(String[] args) {
// HeadTailListInterface<Integer> list = new ArrayHeadTailList<Integer>(10);
// comment the line above and un-comment the line below to test the extra credit
// NOTE: for the extra credit, all lines should match except for the capacity print out-
// the capacity of an ArrayList is private, so this cannot be shown
HeadTailListInterface<Integer> list = new ListHeadTailList<Integer>(10);
System.out.println("********TESTING ISEMPTY AND EMPTY DISPLAY");
System.out.println("Empty is true: " + list.isEmpty());
System.out.println();
System.out.println("Should display:\n0 elements; capacity = 10");
list.display();
System.out.println();
System.out.println("\n\n********TESTING ADD TO FRONT");
// test addFront to emtpy
list.addFront(2);
System.out.println("Should display:\n1 elements; capacity = 10 [2]");
list.display();
System.out.println();
// test addFront to not empty
list.addFront(4);
list.addFront(3);
System.out.println("Should display:\n3 elements; capacity = 10 [3, 4, 2]");
list.display();
System.out.println();
System.out.println("Empty is false: " + list.isEmpty());
System.out.println("\n\n********TESTING CLEAR");
list.clear();
System.out.println("Should display:\n0 elements; capacity = 10");
list.display();
System.out.println("\n\n********TESTING ADD TO BACK");
// test addBack to empty
list.addBack(7);
System.out.println("Should display:\n1 elements; capacity = 10 [7]");
list.display();
System.out.println();
// test addBack to non empty
list.addBack(10);
list.addBack(5);
System.out.println("Should display:\n3 elements; capacity = 10 [7, 10, 5]");
list.display();
System.out.println("\n\n********TESTING CONTAINS");
System.out.println("Contains 5 true: "+list.contains(5));
System.out.println("Contains 7 true: "+list.contains(7));
System.out.println("Contains 4 false: "+list.contains(4));
System.out.println("\n\n********TESTING ADD WITH EXPANSION");
list.clear();
for(int i=0; i<32; i++) {
list.addBack(i);
}
System.out.println("Should display:\n32 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]");
list.display();
System.out.println("\n\n********TESTING INDEX OF");
System.out.println("Index of 0 is 0: " + list.indexOf(0));
System.out.println("Index of 31 is 31: " + list.indexOf(31));
System.out.println("Index of -5 is -1: " + list.indexOf(-5));
System.out.println("Index of 32 is -1: " + list.indexOf(32));
list.addFront(3);
list.addBack(5);
System.out.println("Index of 3 is 0: " + list.indexOf(3));
System.out.println("Index of 5 is 6: " + list.indexOf(5));
System.out.println("\n\n********TESTING LAST INDEX OF");
System.out.println("Last index of 0 is 1: " + list.lastIndexOf(0));
System.out.println("Last index of 31 is 32: " + list.lastIndexOf(31));
System.out.println("Last index of -5 is -1: " + list.lastIndexOf(-5));
System.out.println("Last index of 35 is -1: " + list.lastIndexOf(35));
System.out.println("Last index of 3 is 4: " + list.lastIndexOf(3));
System.out.println("Last index of 5 is 33: " + list.lastIndexOf(5));
System.out.println("\n\n********TESTING SIZE");
System.out.println("Size is 34: " + list.size());
System.out.println();
System.out.println("\n********TESTING GET ENTRY");
System.out.println("Element in position 15 is 14: "+list.getEntry(15));
System.out.println("Element in position 0 is 3: "+list.getEntry(0));
System.out.println("Element in position 39 is null: "+list.getEntry(39));
System.out.println("Element in position -1 is null: "+list.getEntry(-1));
System.out.println("\n\n********TESTING REMOVES");
// test removes from nonEmpty
System.out.println("Remove front element 3: "+list.removeFront());
System.out.println("Remove back element 5 :"+list.removeBack());
System.out.println("Remove front element 0: "+list.removeFront());
System.out.println("Remove back element 31: "+list.removeBack());
System.out.println();
System.out.println("Should display:\n30 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]");
list.display();
System.out.println();
// test removes from empty
list.clear();
System.out.println("Remove element null: "+list.removeFront());
System.out.println("Remove element null: "+list.removeBack());
System.out.println();
// test removes from singleton
list.clear();
list.addFront(1);
System.out.println("Remove element 1: "+list.removeFront());
System.out.println("Should display:\n0 elements; capacity = 40");
list.display();
System.out.println();
System.out.println();
list.addBack(1);
System.out.println("Remove element 1: "+list.removeFront());
System.out.println("Should display:\n0 elements; capacity = 40");
list.display();
System.out.println();
System.out.println();
list.addFront(1);
System.out.println("Remove element 1: "+list.removeBack());
System.out.println("Should display:\n0 elements; capacity = 40");
list.display();
System.out.println();
System.out.println();
list.addBack(1);
System.out.println("Remove element 1: "+list.removeBack());
System.out.println("Should display:\n0 elements; capacity = 40");
list.display();
System.out.println();
System.out.println();
System.out.println("\n\n********TESTING MIX OF ADDS AND REMOVES");
list.addFront(3);
list.addBack(2);
list.addFront(4);
list.addFront(5);
list.addBack(3);
list.addBack(8);
list.addBack(9);
System.out.println("Should display:\n7 elements; capacity = 40 [5, 4, 3, 2, 3, 8, 9]");
list.display();
System.out.println();
list.removeFront();
list.removeBack();
System.out.println("Should display:\n5 elements; capacity = 40 [4, 3, 2, 3, 8]");
list.display();
System.out.println();
System.out.println("********TESTING WITH STRINGS");
HeadTailListInterface<String> wordList = new ArrayHeadTailList<String>(4);
wordList.addFront("job!");
wordList.addFront("Nice");
wordList.addFront("it!");
wordList.addFront("did");
wordList.addFront("You");
System.out.println("Should display:\n5 elements; capacity = 8 [You, did, it!, Nice, job!]");
wordList.display();
System.out.println();
System.out.println("Contains \"Nice\" is true: "+ wordList.contains(new String("Nice")));
System.out.println("Contains \"You\" is true: "+ wordList.contains(new String("You")));
System.out.println("Contains \"you\" is false: "+ wordList.contains(new String("you")));
System.out.println();
System.out.println("Index of \"it!\" is 2: "+ wordList.indexOf(new String("it!")));
System.out.println("Last index of \"it!\" is 2: "+ wordList.lastIndexOf(new String("it!")));
}
}





output:
********TESTING ISEMPTY AND EMPTY DISPLAY
Empty is true: true
Should display:
0 elements; capacity = 10
0 elements; []
********TESTING ADD TO FRONT
Should display:
1 elements; capacity = 10 [2]
1 elements; [2]
Should display:
3 elements; capacity = 10 [3, 4, 2]
3 elements; [3, 4, 2]
Empty is false: false
********TESTING CLEAR
Should display:
0 elements; capacity = 10
0 elements; []
********TESTING ADD TO BACK
Should display:
1 elements; capacity = 10 [7]
1 elements; [7]
Should display:
3 elements; capacity = 10 [7, 10, 5]
3 elements; [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; [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; [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; []
Remove element 1: 1
Should display:
0 elements; capacity = 40
0 elements; []
Remove element 1: 1
Should display:
0 elements; capacity = 40
0 elements; []
Remove element 1: 1
Should display:
0 elements; capacity = 40
0 elements; []
********TESTING MIX OF ADDS AND REMOVES
Should display:
7 elements; capacity = 40 [5, 4, 3, 2, 3, 8, 9]
7 elements; [5, 4, 3, 2, 3, 8, 9]
Should display:
5 elements; capacity = 40 [4, 3, 2, 3, 8]
5 elements; [4, 3, 2, 3, 8]
********TESTING WITH STRINGS
Should display:
5 elements; capacity = 8 [You, did, it!, Nice, job!]
5 elements; capacity = 8 [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




Hope this helps!
Please let me know if any changes needed.
Thank you!
public class ArrayHeadTailList<T> implements HeadTailListInterface<T> You are given an interface for a type of list. The...
import java.util.*; /** * A class that implements the ADT set by using a linked bag. * The set is never full. * * */ public class LinkedSetWithLinkedBag> implements SetInterface { private LinkedBag setOfEntries; /** * Creates a set from a new, empty linked bag. */ public LinkedSetWithLinkedBag() { //TODO Project1 } // end default constructor public boolean add(T newEntry) { //TODO Project1 // new node is at beginning of chain if(this.setOfEntries.isEmpty()) { if (!this.setOfEntries.contains(newEntry)) this.setOfEntries.add(newEntry); } return true; //...
Develop a Generic String List (GSL). NOTE: I have done this lab but someting is wrong here is what i was told that was needed. Ill provide my code at the very end. Here is what is missing : Here is my code: public class GSL { private String arr[]; private int size; public GSL() { arr = new String[10]; size = 0; } public int size() { return size; } public void add(String value) { ...
Suppose that you have several numbered billiard balls on a pool table. The smallest possible number on the ball is “1”. At each step, you remove a billiard ball from the table. If the ball removed is numbered n, you replace it with n balls randomly numbered less than n. For example, if you remove the “5” ball, you replace it with balls numbered “2”, “1”, “1”, “4”, and “3”, where numbers 2, 1, 1, 4, and 3 were randomly...
Java Write an intersection method for the ResizableArrayBag class. The intersection of two bags is the overlapping content of the bags. Intersections are explained in more detail in Chapter 1, #6. An intersecion might contain duplicates. The method should not alter either bag. The current bag and the bag sent in as a parameter should be the same when the method ends. The method header is: public BagInterface<T> intersection(ResizableArrayBag <T> anotherBag) Example: bag1 contains (1, 2, 2, 3) bag2 contains...
Load to the IDEA the remaining classes from the provided Lab02.zip file. Repeat the previous project inside the ArraySetWithArray class. As shown in the UML diagram below ArraySetWithArray class does not utilize ResizableArrayBag object as its instance variable, it has setOfEntries defined as an array which should be dynamically resized if more room needed (double the size). displaySet method should check if the set is empty and display appropriate message; if the set is not empty should display the number...
Write a program that thoroughly tests the class LinkedBag. Write the "LinkedBag" class, and a main program named "Project1.java" testing the methods defined for the LinkedBag object. The interface file: BagInterface.java. /** An interface that describes the operations of a bag of objects. @author Frank M. Carrano @author Timothy M. Henry @version 4.0*/public interface BagInterface<T>{ /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. */ public int getCurrentSize(); /**...
Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...
Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the first item containing a given value from a doubly linked list. The header of the method is as follows: public boolean removeValue(T aValue) where T is the general type of the objects in the list and the methods returns true if such an item is found and deleted. Include testing of the method in a main method of the DoublyLList class. ------------------------------------------------------------------------------------- /** A...
NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...
JAVA // TO DO: add your implementation and JavaDoc public class SmartArray<T>{ private static final int DEFAULT_CAPACITY = 2; //default initial capacity / minimum capacity private T[] data; //underlying array // ADD MORE PRIVATE MEMBERS HERE IF NEEDED! @SuppressWarnings("unchecked") public SmartArray(){ //constructor //initial capacity of the array should be DEFAULT_CAPACITY } @SuppressWarnings("unchecked") public SmartArray(int initialCapacity){ // constructor // set the initial capacity of...