I\'m really struggling to get this program working in Java. Please help!!! The f
ID: 3684115 • Letter: I
Question
I'm really struggling to get this program working in Java. Please help!!!
The files below are required with the program:
Driver.java
/**
* DoubleOrderedList testing area.
*
*/
public class Driver {
public static void main(String [] args) {
DoubleOrderedList list = new DoubleOrderedList<>();
list.add(23);
list.add(24);
list.add(16);
list.add(3);
list.add(7);
list.add(17);
list.add(9);
list.add(13);
list.add(14);
list.add(1);
System.out.println(list);
list.remove(7);
list.removeFirst();
list.remove(17);
list.removeLast();
list.remove(14);
list.removeLast();
System.out.println(list);
/* Test Results:
1 3 7 9 13 14 16 17 23 24
3 9 13 16
*/
}
}
ElementNotFoundException.java
/**
* ElementNotFoundException represents the situation in which a target element
* is not present in a collection
*/
public class ElementNotFoundException extends RuntimeException
{
/**
* Sets up this exception with an appropriate message.
*/
public ElementNotFoundException (String collection)
{
super ("The target element is not in this " + collection);
}
}
EmptyCollectionException.java
/**
* Represents the situation in which a collection is empty.
*/
public class EmptyCollectionException extends RuntimeException
{
/**
* Sets up this exception with an appropriate message.
* @param collection the name of the collection
*/
public EmptyCollectionException (String collection)
{
super ("The " + collection + " is empty.");
}
}
ListADT.java
import java.util.Iterator;
/**
* ListADT defines the interface to a general list collection. Specific
* types of lists will extend this interface to complete the
* set of necessary operations.
*/
public interface ListADT extends Iterable
{
/**
* Removes and returns the first element from this list.
*
* @return the first element from this list
*/
public T removeFirst();
/**
* Removes and returns the last element from this list.
*
* @return the last element from this list
*/
public T removeLast();
/**
* Removes and returns the specified element from this list.
*
* @param element the element to be removed from the list
*/
public T remove(T element);
/**
* Returns a reference to the first element in this list.
*
* @return a reference to the first element in this list
*/
public T first();
/**
* Returns a reference to the last element in this list.
*
* @return a reference to the last element in this list
*/
public T last();
/**
* Returns true if this list contains the specified target element.
*
* @param target the target that is being sought in the list
* @return true if the list contains this element
*/
public boolean contains(T target);
/**
* Returns true if this list contains no elements.
*
* @return true if this list contains no elements
*/
public boolean isEmpty();
/**
* Returns the number of elements in this list.
*
* @return the integer representation of number of elements in this list
*/
public int size();
/**
* Returns an iterator for the elements in this list.
*
* @return an iterator over the elements in this list
*/
public Iterator iterator();
/**
* Returns a string representation of this list.
*
* @return a string representation of this list
*/
public String toString();
}
NonComparableElementException.java
/**
* NonComparableElementException represents the situation in which an attempt
* is made to add an element that is not comparable to an ordered collection
*/
public class NonComparableElementException extends RuntimeException
{
/**
* Sets up this exception with an appropriate message.
*
* @param collection the exception message to deliver
*/
public NonComparableElementException (String collection)
{
super ("The " + collection + " requires comparable elements.");
}
}
OrderedListADT.java
/**
* OrderedListADT defines the interface to an ordered list collection. Only
* Comparable elements are stored, kept in the order determined by
* the inherent relationship among the elements.
*/
public interface OrderedListADT extends ListADT
{
/**
* Adds the specified element to this list at the proper location
*
* @param element the element to be added to this list
*/
public void add(T element);
}
Explanation / Answer
import java.util.Iterator;
public class ArrayList<T> implements ListADT<T>
{
protected final int DEFAULT_CAPACITY = 100;
private final int NOT_FOUND = -1;
protected int rear;
protected T[] list;
public ArrayList()
{
rear = 0;
list = (T[])(new Object[DEFAULT_CAPACITY]);
}
public ArrayList (int initialCapacity)
{
rear = 0;
list = (T[])(new Object[initialCapacity]);
}
public T removeLast () throws EmptyCollectionException
{
T result = null;
if (isEmpty())
throw new EmptyCollectionException ("list");
rear--;
result = list[rear];
list[rear] = null;
return result;
}
public void add (T element)
{
rear++;
}
public T removeFirst() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException ("list");
T result = null;
list[rear] = null;
return result;
}
public T remove (T element)
{
T result = null;
int index = -1;
rear--;
return result;
}
public T first() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException ("list");
return list[0];
}
public T last() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException ("list");
return list[rear-1];
}
public boolean contains (T target)
{
return (find(target) != NOT_FOUND);
}
private int find (T target)
{
int scan = 0, result = NOT_FOUND;
boolean found = false;
if (found)
result = scan;
return result;
}
public boolean isEmpty()
{
return (rear == 0);
}
public int size()
{
return rear;
}
public Iterator<T> iterator()
{
return new ArrayIterator<T> (list, rear);
}
public String toString()
{
String result = "";
for (int scan=0; scan < rear; scan++)
result = result + list[scan].toString() + " ";
return result;
}
protected void expandCapacity()
{
T[] larger = (T[])(new Object[list.length*2]);
for (int scan=0; scan < list.length; scan++)
larger[scan] = list[scan];
list = larger;
}
}
class ArrayOrderedList<T> extends ArrayList<T> implements OrderedListADT<T>
{
public ArrayOrderedList()
{
super();
}
public ArrayOrderedList (int initialCapacity)
{
super(initialCapacity);
}
public void add (T element)
{
if (size() == list.length)
expandCapacity();
Comparable<T> temp = (Comparable<T>)element;
int scan = 0;
while (scan < rear && temp.compareTo(list[scan]) > 0)
scan++;
for (int scan2=rear; scan2 > scan; scan2--)
list[scan2] = list[scan2-1];
list[scan] = element;
rear++;
}
}
class ArrayIterator<T> implements Iterator
{
private int count; // the number of elements in the collection
private int current; // the current position in the iteration
private T[] items;
public ArrayIterator (T[] collection, int size)
{
items = collection;
count = size;
current = 0;
}
public boolean hasNext()
{
return (current < count);
}
public T next()
{
if (! hasNext())
current++;
return items[current - 1];
}
public void remove() throws UnsupportedOperationException
{
throw new UnsupportedOperationException();
}
}
interface OrderedListADT<T> extends ListADT<T>
{
// Adds the specified element to this list at the proper location
public void add (T element);
}
interface ListADT<T>
{
// Removes and returns the first element from this list
public T removeFirst ();
// Removes and returns the specified element from this list
public T remove (T element);
// Returns a reference to the first element on this list
public T first ();
// Returns true if this list contains the specified target element
public boolean contains (T target);
// Returns true if this list contains no elements
public boolean isEmpty();
// Returns the number of elements in this list
public int size();
// Returns an iterator for the elements in this list
public Iterator<T> iterator();
// Returns a string representation of this list
public String toString();
}
class EmptyCollectionException extends RuntimeException
{
public EmptyCollectionException (String collection)
{
super ("The " + collection + " is empty.");
}
}
class ElementNotFoundException extends RuntimeException
{
public ElementNotFoundException (String collection)
{
super ("The target element is not in this " + collection);
}
}
class NonComparableElementException extends RuntimeException
{
public NonComparableElementException (String collection)
{
super ("The " + collection + " requires comparable elements.");
}
}