Question
Really struggling to even get started on this, can anyone help me complete this?? The requirement is to fully complete the ArrayList.java and ArrayUnorderedList.java file
Really struggling to even get started on this, can anyone help me complete this??
The requirement is to fully complete the ArrayList.java and ArrayUnorderedList.java file and complete at least five methods in the LinkedBinaryTree.java file as well.
You cannot use a package/import that isn't already provided in the code.
//ArrayList.java
import java.util.*;
/** * ArrayList represents an array implementation of a list. The front of * the list is kept at array index 0. This class will be extended * to create a specific kind of list. * * @author Lewis and Chase * @version 4.0 */ public abstract class ArrayList
/** * Creates an empty list using the default capacity. */ public ArrayList() { this(DEFAULT_CAPACITY); }
/** * Creates an empty list using the specified capacity. * * @param initialCapacity the integer value of the size of the array list */ public ArrayList(int initialCapacity) { rear = 0; list = (T[])(new Object[initialCapacity]); modCount = 0; }
/** * Creates a new array to store the contents of this list with * twice the capacity of the old one. Called by descendant classes * that add elements to the list. */ protected void expandCapacity() { list = Arrays.copyOf(list, list.length*2); } /** * Removes and returns the last element in this list. * * @return the last element in the list * @throws EmptyCollectionException if the element is not in the list */ @Override public T removeLast() throws EmptyCollectionException { //TODO: Implement this. }
/** * Removes and returns the first element in this list. * * @return the first element in the list * @throws EmptyCollectionException if the element is not in the list */ @Override public T removeFirst() throws EmptyCollectionException { //TODO: Implement this. }
/** * Removes and returns the specified element. * * @param element the element to be removed and returned from the list * @return the removed elememt * @throws ElementNotFoundException if the element is not in the list */ @Override public T remove(T element) { T result; int index = find(element);
if (index == NOT_FOUND) throw new ElementNotFoundException("ArrayList");
result = list[index]; rear--; // shift the appropriate elements for (int scan=index; scan
return result; } /** * Returns a reference to the element at the front of this list. * The element is not removed from the list. Throws an * EmptyCollectionException if the list is empty. * * @return a reference to the first element in the list * @throws EmptyCollectionException if the list is empty */ @Override public T first() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("ArrayList");
return list[0]; }
/** * Returns a reference to the element at the rear of this list. * The element is not removed from the list. Throws an * EmptyCollectionException if the list is empty. * * @return a reference to the last element of this list * @throws EmptyCollectionException if the list is empty */ @Override public T last() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("ArrayList");
return list[rear-1]; }
/** * Returns true if this list contains the specified element. * * @param target the target element * @return true if the target is in the list, false otherwise */ public boolean contains(T target) { return (find(target) != NOT_FOUND); }
/** * Returns the array index of the specified element, or the * constant NOT_FOUND if it is not found. * * @param target the target element * @return the index of the target element, or the * NOT_FOUND constant */ private int find(T target) { int scan = 0; int result = NOT_FOUND; if (!isEmpty()) while (result == NOT_FOUND && scan
return result; }
/** * Returns true if this list is empty and false otherwise. * * @return true if the list is empty, false otherwise */ @Override public boolean isEmpty() { return (rear == 0); } /** * Returns the number of elements currently in this list. * * @return the number of elements in the list */ @Override public int size() { return rear; }
/** * Returns a string representation of this list. * * @return the string representation of the list */ @Override public String toString() { String result = "";
for(T element : list) result += element + " ";
return result; } /** * Returns an iterator for the elements currently in this list. * * @return an iterator for the elements in the list */ @Override public Iterator
/** * ArrayListIterator iterator over the elements of an ArrayList. */ private class ArrayListIterator implements Iterator //ArrayUnorderedList.java /** * ArrayUnorderedList represents an array implementation of an unordered list. * * @author Lewis and Chase * @version 4.0 */ public class ArrayUnorderedList /** * Creates an empty list using the specified capacity. * * @param initialCapacity the intial size of the list */ public ArrayUnorderedList(int initialCapacity) { super(initialCapacity); } /** * Adds the specified element to the front of this list. * * @param element the element to be added to the front of the list */ @Override public void addToFront(T element) { // TODO: Implement this. } /** * Adds the specified element to the rear of this list. * * @param element the element to be added to the list */ @Override public void addToRear(T element) { // TODO: Implement this. } /** * Adds the specified element after the specified target element. * Throws an ElementNotFoundException if the target is not found. * * @param element the element to be added after the target element * @param target the target that the element is to be added after */ @Override public void addAfter(T element, T target) { if (size() == list.length) expandCapacity(); int scan = 0; // find the insertion point while (scan scan; shift--) list[shift] = list[shift-1]; // insert element list[scan] = element; rear++; modCount++; } } //LinkedBinaryTree.java import java.util.*; /** * LinkedBinaryTree implements the BinaryTreeADT interface * * @author Lewis and Chase * @version 4.0 */ public class LinkedBinaryTree /** * Creates a binary tree with the specified element as its root. * * @param element the element that will become the root of the binary tree */ public LinkedBinaryTree(T element) { root = new BinaryTreeNode(element); } /** * Creates a binary tree with the specified element as its root and the * given trees as its left child and right child * * @param element the element that will become the root of the binary tree * @param left the left subtree of this tree * @param right the right subtree of this tree */ public LinkedBinaryTree(T element, LinkedBinaryTree /** * Returns the integer size of this tree. * * @return the integer size of the tree */ @Override public int size() { // TODO: Implement this. } /** * Returns the height of this tree. * * @return the height of the tree */ public int getHeight() { // TODO: Implement this. } /** * Returns the height of the specified node. * * @param node the node from which to calculate the height * @return the height of the tree */ private int height(BinaryTreeNode /** * Returns a reference to the specified target element if it is * found in this binary tree. * * @param targetElement the element being sought in this tree * @param next the element to begin searching from */ private BinaryTreeNode /** * Returns an iterator over the elements in this tree using the * iteratorInOrder method * * @return an in order iterator over this binary tree */ @Override public Iterator /** * Performs a recursive inorder traversal. * * @param node the node to be used as the root for this traversal * @param tempList the temporary list for use in this traversal */ protected void inOrder(BinaryTreeNode /** * Performs an preorder traversal on this binary tree by calling * an overloaded, recursive preorder method that starts with * the root. * * @return a pre order iterator over this tree */ @Override public Iterator /** * Performs a recursive preorder traversal. * * @param node the node to be used as the root for this traversal * @param tempList the temporary list for use in this traversal */ protected void preOrder(BinaryTreeNode /** * Performs an postorder traversal on this binary tree by calling * an overloaded, recursive postorder method that starts * with the root. * * @return a post order iterator over this tree */ @Override public Iterator /** * Performs a recursive postorder traversal. * * @param node the node to be used as the root for this traversal * @param tempList the temporary list for use in this traversal */ protected void postOrder(BinaryTreeNode /** * Performs a levelorder traversal on this binary tree, using a * templist. * * @return a levelorder iterator over this binary tree */ @Override public Iterator nodes.addToRear(root); while (!nodes.isEmpty()) { current = nodes.removeFirst(); if (current != null) { tempList.addToRear(current.getElement()); if (current.getLeft() != null) nodes.addToRear(current.getLeft()); if (current.getRight() != null) nodes.addToRear(current.getRight()); } else tempList.addToRear(null); } return new TreeIterator(tempList.iterator()); } /** * Inner class to represent an iterator over the elements of this tree */ private class TreeIterator implements Iterator
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started