Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PP 15.8 Implement an OrderedList using an ArrayList. Side note: Please keep the driver as close to the original as possible. Please change what each

PP 15.8 Implement an OrderedList using an ArrayList.

Side note: Please keep the driver as close to the original as possible. Please change what each case does to replicate the output.

ArrayList.java

package jsjf;

import jsjf.exceptions.*; import java.util.*;

public abstract class ArrayList implements ListADT, Iterable {private final static int DEFAULT_CAPACITY = 100; private final static int NOT_FOUND = -1;

protected int rear; protected T[] list; protected int modCount;

public ArrayList() {this(DEFAULT_CAPACITY); }

public ArrayList(int initialCapacity) {rear = 0; list = (T[])(new Object[initialCapacity]); modCount = 0; }

protected void expandCapacity() { // To be completed as a Programming Project }

public T removeLast() throws EmptyCollectionException {// To be completed as a Programming Project return null; // temp }

public T removeFirst() throws EmptyCollectionException {// To be completed as a Programming Project return null; // temp }

public T remove(T element) { T result; int index = find(element);

if (index == NOT_FOUND) throw new ElementNotFoundException("ArrayList");

result = list[index]; rear--;

for (int scan = index; scan

list[scan] = list[scan+1];

list[rear] = null; modCount++;

return result; }

public T first() throws EmptyCollectionException {// To be completed as a Programming Project return null; // temp }

public T last() throws EmptyCollectionException {// To be completed as a Programming Project return null; // temp }

public boolean contains(T target)

{return (find(target) != NOT_FOUND); }

private int find(T target)

{int scan = 0; int result = NOT_FOUND;

if (!isEmpty()) while (result == NOT_FOUND && scan

return result; }

public boolean isEmpty() {// To be completed as a Programming Project return true; // temp }

public int size() {// To be completed as a Programming Project return 0; // temp }

public String toString() {// To be completed as a Programming Project return ""; // temp }

public Iterator iterator() {return new ArrayListIterator(); }

private class ArrayListIterator implements Iterator {int iteratorModCount; int current;

public ArrayListIterator() {iteratorModCount = modCount; current = 0; }

public boolean hasNext() throws ConcurrentModificationException {if (iteratorModCount != modCount) throw new ConcurrentModificationException();

return (current

public T next() throws ConcurrentModificationException {if (!hasNext()) throw new NoSuchElementException();

current++;

return list[current - 1]; }

public void remove() throws UnsupportedOperationException {throw new UnsupportedOperationException(); }

} }

ArrayListDriver

package jsjf;

import java.util.Scanner;

public class ArrayListDriver {public static void main(String[] args) {Scanner input=new Scanner(System.in); ArrayListqueue=new ArrayList(); int menu = 0; do {System.out.println(" Queue menu selection 1.Add element|2.Remove element|3.First element|4.Last element|5.Display|6.Exit");

System.out.print("Enter your choice: ");

menu=Integer.parseInt(input.next()); switch(menu) {case 1: System.out.print("Enter element: "); String element=input.next(); ArrayList.add(element); break; case 2: System.out.print("Enter element: "); String element=input.next(); ArrayList.remove(element); break; case 3: System.out.println("First element is: "+ArrayList.first()); break; case 4: System.out.println("Last elements is: "); ArrayList.last(); break; case 5: System.out.println("Listed elements are: "); ArrayList.display(); break; } } while(menu!=6); //iterates until 6 (Exit) is entered input.close(); }

}

ArrayOrderedList.java

package jsjf;

import jsjf.exceptions.*;

public class ArrayOrderedList extends ArrayList implements OrderedListADT {public ArrayOrderedList() {super(); }

public ArrayOrderedList(int initialCapacity) {super(initialCapacity); }

public void add(T element) {if (!(element instanceof Comparable)) throw new NonComparableElementException("OrderedList");

Comparable comparableElement = (Comparable)element;

if (size() == list.length) expandCapacity();

int scan = 0;

while (scan 0) scan++;

for (int shift = rear; shift > scan; shift--) list[shift] = list[shift - 1];

list[scan] = element; rear++; modCount++; } }

ArrayUnorderedList.java

package jsjf;

import jsjf.exceptions.*;

public class ArrayUnorderedList extends ArrayList implements UnorderedListADT {public ArrayUnorderedList() {

super(); }

public ArrayUnorderedList(int initialCapacity) { super(initialCapacity); }

public void addToFront(T element) { // To be completed as a Programming Project }

public void addToRear(T element) { // To be completed as a Programming Project }

public void addAfter(T element, T target) {if (size() == list.length) expandCapacity();

int scan = 0;

while (scan

if (scan == rear) throw new ElementNotFoundException("UnorderedList");

scan++;

for (int shift = rear; shift > scan; shift--) list[shift] = list[shift - 1];

list[scan] = element; rear++; modCount++; }

}

ListADT.java

package jsjf;

import java.util.Iterator;

public interface ListADT extends Iterable {public T removeFirst();

public T removeLast();

public T remove(T element);

public T first();

public T last();

public boolean contains(T target);

public boolean isEmpty();

public int size();

public Iterator iterator();

public String toString();

}

OrderedListADT.java

package jsjf;

public interface OrderedListADT extends ListADT {public void add(T element); }

UnorderedListADT.java

package jsjf;

public interface UnorderedListADT extends ListADT {public void addToFront(T element);

public void addToRear(T element);

public void addAfter(T element, T target); }

ElementNotFoundException.java

package jsjf.exceptions;

public class ElementNotFoundException extends RuntimeException {public ElementNotFoundException (String collection) {super ("The target element is not in this " + collection); } }

EmptyCollectionException.java

package jsjf.exceptions;

public class EmptyCollectionException extends RuntimeException {public EmptyCollectionException(String collection) {super("The " + collection + " is empty."); } }

NonCompareableElementException.java

package jsjf.exceptions;

public class NonComparableElementException extends RuntimeException {public NonComparableElementException (String collection) {super ("The " + collection + " requires Comparable elements."); } }

Sample Output

image text in transcribed

Rubric

image text in transcribed

List Menu Selections 1.add elementi 2.remove element 3.first element 1 4.last element I 5.display I 6.Exit Enter your choice: 1 Enter element: Henry List Menu Selections 1.add element 1 2. remove element 1 3. first element 4.last element 1 5.display I 6.Exis Enter your choice: 1 Enter element: Earl List Menu Selections 1. add element 2.remove element 3. first element 4.last element i 5.display I 6.Exit Enter your choice: 1 Enter element: Jane List Menu Selections 1.add element 2.remove element 13.11zat element 4.last element 5.display 6.Exit Enter your choice: 1 Enter element: Aaron Liat Menu Selections 1.add element 2.remove element 3. first element 4.last elements display 6.Exit Enter your choice: 2 Enter element: Aaron Aaron removed. List Menu Selections 1. add element 2.remove element 1 3. first element 4. last elements.display I 6.Exit Enter your choice: 3 Tirst element: Earl List Menu Selections 1. add element 2.remove element 3. first element 4.last element 5.display 6.Exit Enter your choice: 5 1. Earl 2. Henry 3. Jane Documentation 10 pts Driver program Implement the driver program featured in this module's lecture. 20 pts Implement ArrayList class All methods and data for this class are listed in the lecture. 30 pts Implement add/T element) method This method adds the string] element to the list. The placement of the element is based on the order of the list, which is ascending. 30 pts Successful running program 10 pts List Menu Selections 1.add elementi 2.remove element 3.first element 1 4.last element I 5.display I 6.Exit Enter your choice: 1 Enter element: Henry List Menu Selections 1.add element 1 2. remove element 1 3. first element 4.last element 1 5.display I 6.Exis Enter your choice: 1 Enter element: Earl List Menu Selections 1. add element 2.remove element 3. first element 4.last element i 5.display I 6.Exit Enter your choice: 1 Enter element: Jane List Menu Selections 1.add element 2.remove element 13.11zat element 4.last element 5.display 6.Exit Enter your choice: 1 Enter element: Aaron Liat Menu Selections 1.add element 2.remove element 3. first element 4.last elements display 6.Exit Enter your choice: 2 Enter element: Aaron Aaron removed. List Menu Selections 1. add element 2.remove element 1 3. first element 4. last elements.display I 6.Exit Enter your choice: 3 Tirst element: Earl List Menu Selections 1. add element 2.remove element 3. first element 4.last element 5.display 6.Exit Enter your choice: 5 1. Earl 2. Henry 3. Jane Documentation 10 pts Driver program Implement the driver program featured in this module's lecture. 20 pts Implement ArrayList class All methods and data for this class are listed in the lecture. 30 pts Implement add/T element) method This method adds the string] element to the list. The placement of the element is based on the order of the list, which is ascending. 30 pts Successful running program 10 pts

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions