diff --git a/.gitignore b/.gitignore index 1023db7..d083cf2 100644 --- a/.gitignore +++ b/.gitignore @@ -51,3 +51,4 @@ /Semester 2/Assignments/lab_EL5_CalebFontenot/target/ /Semester 2/Assignments/labEL2_CalebFontenot/target/ /Semester 2/LabJuneauValidateUserInput/target/ +/Semester 2/MP4_CalebFontenot/target/ diff --git a/Semester 2/MP4_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/mp4_calebfontenot/ArrayListASDV.java b/Semester 2/MP4_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/mp4_calebfontenot/ArrayListASDV.java index 99ca80b..9342a43 100644 --- a/Semester 2/MP4_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/mp4_calebfontenot/ArrayListASDV.java +++ b/Semester 2/MP4_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/mp4_calebfontenot/ArrayListASDV.java @@ -16,6 +16,7 @@ import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.NoSuchElementException; +import java.util.Objects; import java.util.function.Consumer; /** @@ -23,8 +24,7 @@ import java.util.function.Consumer; * @author ASDV2 */ public class ArrayListASDV - implements Serializable, Cloneable, List -{ + implements Serializable, Cloneable, List { private E[] list; private int index;//the index to add at ( length of array) @@ -44,33 +44,32 @@ public class ArrayListASDV * Constructs an empty list with the specified initial capacity. * * @param initialCapacity - the initial capacity of the list - * @throws IllegalArgumentException - if the specified initial capacity is - * negative + * @throws IllegalArgumentException - if the specified initial capacity is negative */ public ArrayListASDV(int initialCapacity) { - if (initialCapacity < 0) - { + if (initialCapacity < 0) { throw new IllegalArgumentException("initialCapacity id negative: " + initialCapacity); - } + } list = (E[]) new Object[initialCapacity]; index = 0; } - /**Double the size of the current list and copies to it the old list - * + + /** + * Double the size of the current list and copies to it the old list + * * @return the new array. */ - private E[] doubleSizeOfList() - { - list = this.toArray( (E[] ) new Object[list.length+list.length]); - return this.list; + private E[] doubleSizeOfList() + { + list = this.toArray((E[]) new Object[list.length + list.length]); + return this.list; + + } - } - /** - * Constructs a list containing the elements of the specified collection, in - * the order they are returned by the collection's iterator. + * Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. * * @param c - the collection whose elements are to be placed into this * @throws NullPointerException - if the specified collection is null @@ -83,30 +82,27 @@ public class ArrayListASDV } /** - * Returns true if this collection changed as a result of the call. false if - * this collection does not permit duplicates and already contains the - * specified element. + * Returns true if this collection changed as a result of the call. false if this collection does not permit duplicates and already contains the specified element. * * @param e - element whose presence in this collection is to be ensured * * @return true if this collection changed as a result of the call - * @throws ClassCastException - if the class of the specified element - * prevents it from being added to this collection - * @throws NullPointerException - if the specified element is null and this - * collection does not permit null elements - * @throws IllegalArgumentException - if some property of the element - * prevents it from being added to this collection + * @throws ClassCastException - if the class of the specified element prevents it from being added to this collection + * @throws NullPointerException - if the specified element is null and this collection does not permit null elements + * @throws IllegalArgumentException - if some property of the element prevents it from being added to this collection */ @Override public boolean add(E e) { - if ( e == null) - throw new NullPointerException(); - - list[index++] = e; - if (index >= list.length * 0.75) - doubleSizeOfList(); - return true; + if (e == null) { + throw new NullPointerException("null parameter"); + } + + list[index++] = e; + if (index >= list.length * 0.75) { + doubleSizeOfList(); + } + return true; } /** @@ -125,10 +121,9 @@ public class ArrayListASDV { String s = "ArrayListASDV["; - for (int i = 0; i < index; ++i) - { + for (int i = 0; i < index; ++i) { s += list[i] + " "; - } + } s += "]"; return s; @@ -147,72 +142,75 @@ public class ArrayListASDV } /** - * Returns true if this list contains the specified element. More formally, - * returns true if and only if this list contains at least one element e - * such that (o==null ? e==null : o.equals(e)). + * Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)). * * @param o - element whose presence in this list is to be tested * * @return true if this list contains the specified element * */ - @Override public boolean contains(Object o) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + if (o == null) { + return false; + } + for (int i = 0; i < this.index; i++) { + if (o.equals(this.list[i])) { + return true; + } + } + return false; } /** - * Returns an array containing all of the elements in this list in proper - * sequence (from first to last element). The returned array will be "safe" - * in that no references to it are maintained by this list. (In other words, - * this method must allocate a new array). The caller is thus free to modify - * the returned array. This method acts as bridge between array-based and - * collection-based APIs. Returns: an array containing all of the elements - * in this list in proper sequence + * Returns an array containing all of the elements in this list in proper sequence (from first to last element). The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array. This method acts as bridge between array-based and collection-based APIs. Returns: an array containing all of the elements in this list in proper sequence * - * @return an array containing all of the elements in this list in proper - * sequence + * @return an array containing all of the elements in this list in proper sequence */ @Override public Object[] toArray() { - - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + Object[] returnArray = new Object[index]; + for (int i = 0; i < index; ++i) { + Object objCopy = list[i]; + returnArray[i] = objCopy; + } + return returnArray; } /** - * Removes the first occurrence of the specified element from this list, if - * it is present. If the list does not contain the element, it is unchanged. - * More formally, removes the element with the lowest index i such that - * (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). - * Returns true if this list contained the specified element (or - * equivalently, if this list changed as a result of the call). + * Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call). * * @param o - element to be removed from this list, if present * @return true if this list contained the specified element */ @Override - public boolean remove(Object o - ) + public boolean remove(Object o) { - - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - + if (o == null) { + return false; + } + for (int i = 0; i < this.index; i++) { + if (o.equals(this.list[i])) { + this.list[i] = null; + return true; + } + } + return false; } /** - * Removes all of the elements from this list. The list will be empty after - * this call returns. Note: Traverse the array and set all of its elements - * to null. Set its index to zero. + * Removes all of the elements from this list. The list will be empty after this call returns. Note: Traverse the array and set all of its elements to null. Set its index to zero. */ @Override public void clear() { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - + for (int i = 0; i < list.length; ++i) { + list[i] = null; + } + index = 0; } /** @@ -220,166 +218,161 @@ public class ArrayListASDV * * @param index of the element to return * @return the element at the specified position in this list - * @throws IndexOutOfBoundsException - if the index is out of range (index - * GE 0 || index GE size()) + * @throws IndexOutOfBoundsException - if the index is out of range (index GE 0 || index GE size()) */ @Override - public E get(int index - ) + public E get(int index) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - + return list[index]; } /** - * Replaces the element at the specified position in this list with the - * specified element. + * Replaces the element at the specified position in this list with the specified element. * * @param index - index of the element to replace * @param element - element to be stored at the specified position * @return the element previously at the specified position - * @throws IndexOutOfBoundsException - if the index is out of range (index - * GE 0 || index GE size()) + * @throws IndexOutOfBoundsException - if the index is out of range (index GE 0 || index GE size()) */ @Override - public E set(int index, E element - ) + public E set(int index, E element) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - + return list[index] = element; } /** - * Inserts the specified element at the specified position in this list. - * Shifts the element currently at that position (if any) and any subsequent - * elements to the right (adds one to their indices). + * Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). * - * @param index - index at which the specified element is to be inserted - * element + * @param index - index at which the specified element is to be inserted element * @param - element to be inserted - * @throws NullPointerException - if the specified element is null and this - * collection does not permit null elements - * @throws IndexOutOfBoundsException - if the index is out of range (index - * GE 0 || index GE size()) + * @throws NullPointerException - if the specified element is null and this collection does not permit null elements + * @throws IndexOutOfBoundsException - if the index is out of range (index GE 0 || index GE size()) */ - @Override - public void add(int index, E element - ) + public void add(int index, E element) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + if (element == null) { + throw new NullPointerException("cant add null"); + } + if (index > this.index || index < 0) { + throw new IndexOutOfBoundsException("cant add at this index"); + } + // Check if the list needs to be resized + if (this.index >= list.length) { + doubleSizeOfList(); + } + + // Shift elements to the right to make space for the new element + for (int i = this.index; i > index; i--) { + list[i] = list[i - 1]; + } + + // Insert the new element at the specified index + list[index] = element; + this.index++; } /** - * Removes the element at the specified position in this list. Shifts any - * subsequent elements to the left (subtracts one from their indices). + * Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). * * @param index - the index of the element to be removed * @return the element that was removed from the list - * @throws IndexOutOfBoundsException - if the index is out of range (index - * GE 0 || index GE size()) + * @throws IndexOutOfBoundsException - if the index is out of range (index GE 0 || index GE size()) */ - @Override - public E remove(int index - ) + public E remove(int index) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + if (index < 0 || index >= this.index) { + throw new IndexOutOfBoundsException("Index " + index + " out of bounds"); + } + E removedElement = list[index]; + + // Shift elements to the left to fill the gap + for (int i = index; i < this.index - 1; i++) { + list[i] = list[i + 1]; + } + + // Set the last element to null (optional) + list[this.index - 1] = null; + + this.index--; + + return removedElement; } /** - * Returns the index of the first occurrence of the specified element in - * this list, or -1 if this list does not contain the element. More - * formally, returns the lowest index i such that - * (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such - * index. Parameters: + * Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index. Parameters: * * @param o - element to search for - * @return the index of the first occurrence of the specified element in - * this list, or -1 if this list does not contain the element + * @return the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element */ @Override - public int indexOf(Object o - ) + public int indexOf(Object o) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + for (int i = 0; i < list.length - 1; i++) { + if (list[i].equals(o)) { + return i; + } else { + return -1; + } + + } + + return -1; } /** - * Returns the index of the last occurrence of the specified element in this - * list, or -1 if this list does not contain the element. More formally, - * returns the highest index i such that - * (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such - * index. Parameters: + * Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index. Parameters: * * @param o - element to search for - * @return the index of the last occurrence of the specified element in this - * list, or -1 if this list does not contain the element + * @return the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element */ @Override - public int lastIndexOf(Object o - ) + public int lastIndexOf(Object o) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - + ListIterator iterator = listIterator(size()); + while (iterator.hasPrevious()) { + int indexOf = iterator.previousIndex(); + Object current = iterator.previous(); + if (current.equals(o)) { + return indexOf; + } + } + return -1; } /** - * Returns a view of the portion of this list between the specified - * fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex - * are equal, the returned list is empty.) The returned list is backed by - * this list, so non-structural changes in the returned list are reflected - * in this list, and vice-versa. The returned list supports all of the - * optional list operations. This method eliminates the need for explicit - * range operations (of the sort that commonly exist for arrays). Any - * operation that expects a list can be used as a range operation by passing - * a subList view instead of a whole list. For example, the following idiom - * removes a range of elements from a list: list.subList(from, to).clear(); + * Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations. This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list: list.subList(from, to).clear(); * - * Similar idioms may be constructed for ArrayList.indexOf(Object) and - * ArrayList.lastIndexOf(Object), and all of the algorithms in the - * Collections class can be applied to a subList. The semantics of the list - * returned by this method become undefined if the backing list (i.e., this - * list) is structurally modified in any way other than via the returned - * list. (Structural modifications are those that change the size of this - * list, or otherwise perturb it in such a fashion that iterations in - * progress may yield incorrect results.) + * Similar idioms may be constructed for ArrayList.indexOf(Object) and ArrayList.lastIndexOf(Object), and all of the algorithms in the Collections class can be applied to a subList. The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.) * * @param fromIndex - low endpoint (inclusive) of the subList * @param toIndex - high endpoint (exclusive) of the subList * @return a view of the specified range within this list - * @throws IndexOutOfBoundsException - for an illegal endpoint index value - * (fromIndex LE 0 || toIndex > size || fromIndex > toIndex) - * @throws IllegalArgumentException - if the endpoint indices are out of - * order (fromIndex > toIndex) + * @throws IndexOutOfBoundsException - for an illegal endpoint index value (fromIndex LE 0 || toIndex > size || fromIndex > toIndex) + * @throws IllegalArgumentException - if the endpoint indices are out of order (fromIndex > toIndex) */ @Override - public List subList(int fromIndex, int toIndex - ) + public List subList(int fromIndex, int toIndex) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + if (fromIndex < 0 || toIndex > size() || fromIndex > toIndex) { + throw new IndexOutOfBoundsException(); + } + List sublist = new ArrayListASDV<>(); + for (int i = fromIndex; i < toIndex; i++) { + sublist.add(get(i)); + } + return sublist; } /** - * Returns an array containing all of the elements in this list in proper - * sequence (from first to last element); the runtime type of the returned - * array is that of the specified array. If the list fits in the specified - * array, it is returned therein. Otherwise, a new array is allocated with - * the runtime type of the specified array and the size of this list. If the - * list fits in the specified array with room to spare (i.e., the array has - * more elements than the list), the element in the array immediately - * following the end of the collection is set to null. (This is useful in - * determining the length of the list only if the caller knows that the list - * does not contain any null elements.) + * Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list. If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.) * - * @param a - the array into which the elements of the list are to be - * stored, if it is big enough; otherwise, a new array of the same runtime - * type is allocated for this purpose. + * @param a - the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose. * @return an array containing the elements of the list - * @throws ArrayStoreException - if the runtime type of the specified array - * is not a supertype of the runtime type of every element in this list + * @throws ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in this list * @throws NullPointerException - if the specified array is null */ @Override @@ -387,25 +380,22 @@ public class ArrayListASDV { Class clazz = a.getClass(); //>length of a is too small - if (a.length < index) - // Make a new array of a's runtime type - { + if (a.length < index) // Make a new array of a's runtime type + { return (T[]) Arrays.copyOf(this.list, index, a.getClass()); - } + } //>length of a is good System.arraycopy(this.list, 0, a, 0, index); //>length of a is greater than this list set nulls - if (a.length > index) - { - for (int i = index; i < a.length; ++i) - { + if (a.length > index) { + for (int i = index; i < a.length; ++i) { a[i] = null; - } - } + } + } return a; } @@ -413,17 +403,52 @@ public class ArrayListASDV @Override public Iterator iterator() { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + Iterator it = new Iterator() { + int index = 0; + E[] list = (E[]) new Object[3]; + @Override + public boolean hasNext() + { + if (index == this.list.length) { + return false; + } + return true; + } + + @Override + public E next() + { + return this.list[index++]; + } + @Override + public void remove() { + if (index < 1) { + Object[] newList = new Object[--index]; + for (int i = 0; i < index; i++) { + newList[i] = list[i]; + } + list = (E[]) newList; + } + } + + @Override + public void forEachRemaining(Consumer action) + { + Iterator.super.forEachRemaining(action); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/OverriddenMethodBody + } + + }; + return (Iterator) it; } + ; + /** - * Returns a list iterator over the elements in this list (in proper - * sequence). The returned list iterator is fail-fast. + * Returns a list iterator over the elements in this list (in proper sequence). The returned list iterator is fail-fast. * * - * @return a list iterator over the elements in this list (in proper - * sequence + * @return a list iterator over the elements in this list (in proper sequence */ @Override public ListIterator listIterator() @@ -436,95 +461,80 @@ public class ArrayListASDV public ListIterator listIterator(int index) { - ListIterator it = new ListIterator() - { - - int current = index; + ListIterator it = new ListIterator() { + E[] list = (E[]) new Object[3]; + int index = 0; /** - * Returns true if this list iterator has more elements when - * traversing the list in the forward direction. (In other words, - * returns true if ListIterator.next would return an element rather - * than throwing an exception.) + * Returns true if this list iterator has more elements when traversing the list in the forward direction. (In other words, returns true if ListIterator.next would return an element rather than throwing an exception.) * - * @return true if the list iterator has more elements when - * traversing the list in the forward direction + * @return true if the list iterator has more elements when traversing the list in the forward direction */ @Override public boolean hasNext() { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + return next() != null; } /** - * Returns the next element in the list and advances the cursor - * position. This method may be called repeatedly to iterate through - * the list, or intermixed with calls to ListIterator.previous to go - * back and forth. (Note that alternating calls to next and previous - * will return the same element repeatedly.) + * Returns the next element in the list and advances the cursor position. This method may be called repeatedly to iterate through the list, or intermixed with calls to ListIterator.previous to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.) * * @return the next element in the list - * @throws NoSuchElementException - if the iteration has no next - * element + * @throws NoSuchElementException - if the iteration has no next element */ @Override - public E next() + public E next() throws NoSuchElementException { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - + if (index > this.list.length) { + throw new NoSuchElementException(); + } + return this.list[index++]; } @Override public boolean hasPrevious() { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - + return index > 0; } @Override public E previous() { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + return this.list[--index]; } /** - * Returns the index of the element that would be returned by a - * subsequent call to next(). (Returns list size if the list - * iterator is at the end of the list.) + * Returns the index of the element that would be returned by a subsequent call to next(). (Returns list size if the list iterator is at the end of the list.) * - * @return the index of the element that would be returned by a - * subsequent call to next, or list size if the list iterator is at - * the end of the list + * @return the index of the element that would be returned by a subsequent call to next, or list size if the list iterator is at the end of the list */ - @Override - public int nextIndex() - { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - +@Override + public int nextIndex() { + return index + 1; } /** - * Returns the index of the element that would be returned by a - * subsequent call to previous(). (Returns -1 if the list iterator - * is at the beginning of the list.) + * Returns the index of the element that would be returned by a subsequent call to previous(). (Returns -1 if the list iterator is at the beginning of the list.) * - * @return the index of the element that would be returned by a - * subsequent call to previous, or -1 if the list iterator is at the - * beginning of the list + * @return the index of the element that would be returned by a subsequent call to previous, or -1 if the list iterator is at the beginning of the list */ @Override public int previousIndex() { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - + return index - 1; } @Override public void forEachRemaining(Consumer action) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - + while(hasNext()) + { + if (action == null) + throw new NullPointerException("Action is Null"); + else + action.accept(next()); + } } @Override @@ -552,93 +562,96 @@ public class ArrayListASDV /** * - * Returns true if this collection contains all of the elements in the - * specified collection. + * Returns true if this collection contains all of the elements in the specified collection. * - * Parameters: c - collection to be checked for containment in this - * collection Returns: true if this collection contains all of the elements - * in the specified collection Throws: ClassCastException - if the types of - * one or more elements in the specified collection are incompatible with - * this collection (optional) NullPointerException - if the specified - * collection contains one or more null elements and this collection does - * not permit null elements (optional), or if the specified collection is - * null. + * Parameters: c - collection to be checked for containment in this collection Returns: true if this collection contains all of the elements in the specified collection Throws: ClassCastException - if the types of one or more elements in the specified collection are incompatible with this collection (optional) NullPointerException - if the specified collection contains one or more null elements and this collection does not permit null elements (optional), or if the specified collection is null. * * @param c - collection to be checked for containment in this collection - * @return true if this collection contains all of the elements in the - * specified collection. - * @throws ClassCastException - if the types of one or more elements in the - * specified collection are incompatible with this collection + * @return true if this collection contains all of the elements in the specified collection. + * @throws ClassCastException - if the types of one or more elements in the specified collection are incompatible with this collection */ @Override - public boolean containsAll(Collection c) - { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + public boolean containsAll(Collection c) { + for (Object e : c) + { + if(!contains(e)) + { + return false; + } + } + return true; + } + @Override + public boolean addAll(Collection c) { + boolean changed = false; + for(E e : c) + { + if (add(e)) + { + changed = true; + } + } + + return changed; } @Override - public boolean addAll(Collection c) + public boolean addAll(int index, Collection c) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - + boolean changed = false; + for(E e : c) + { + add(index++, e); + changed = true; + } + return changed; } - + /** + * Removes all of this collection's elements that are also contained in the specified collection (optional operation). After this call returns, this collection will contain no elements in common with the specified collection. + * + * Parameters: c - collection containing elements to be removed from this collection Returns: true if this collection changed as a result of the call Throws: UnsupportedOperationException - if the removeAll method is not supported by this collection ClassCastException - if the types of one or more elements in this collection are incompatible with the specified collection (optional) NullPointerException - if this collection contains one or more null elements and the specified collection does not support null elements (optional), or if the specified collection is null + * + * @param c - collection containing elements to be removed from this collection + * @return true if this collection changed as a result of the call + * @throws ClassCastException - if the types of one or more elements in this collection are incompatible with the specified collection + */ @Override - public boolean addAll(int index, Collection c - ) - { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - + public boolean removeAll(Collection c) { + boolean changed = false; + for (Object e : c) { + if (contains(e)) { + remove(e); + changed = true; + } + } + return changed; } /** - * Removes all of this collection's elements that are also contained in the - * specified collection (optional operation). After this call returns, this - * collection will contain no elements in common with the specified - * collection. + * Retains only the elements in this collection that are contained in the specified collection (optional operation). In other words, removes from this collection all of its elements that are not contained in the specified collection. * - * Parameters: c - collection containing elements to be removed from this - * collection Returns: true if this collection changed as a result of the - * call Throws: UnsupportedOperationException - if the removeAll method is - * not supported by this collection ClassCastException - if the types of one - * or more elements in this collection are incompatible with the specified - * collection (optional) NullPointerException - if this collection contains - * one or more null elements and the specified collection does not support - * null elements (optional), or if the specified collection is null * - * @param c - collection containing elements to be removed from this - * collection + * @param c - collection containing elements to be retained in this collection * @return true if this collection changed as a result of the call - * @throws ClassCastException - if the types of one or more elements in this - * collection are incompatible with the specified collection + * @throws ClassCastException - if the types of one or more elements in this collection are incompatible with the specified collection (optional) */ @Override - public boolean removeAll(Collection c) - { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + public boolean retainAll(Collection c) { + Objects.requireNonNull(c); - } + boolean modified = false; + Iterator iterator = this.iterator(); - /** - * Retains only the elements in this collection that are contained in the - * specified collection (optional operation). In other words, removes from - * this collection all of its elements that are not contained in the - * specified collection. - * - * - * @param c - collection containing elements to be retained in this - * collection - * @return true if this collection changed as a result of the call - * @throws ClassCastException - if the types of one or more elements in this - * collection are incompatible with the specified collection (optional) - */ - @Override - public boolean retainAll(Collection c - ) - { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + while (iterator.hasNext()) { + E element = iterator.next(); + if (!c.contains(element)) { + iterator.remove(); + modified = true; + } + } + return modified; } public static void main(String[] args) @@ -650,7 +663,6 @@ public class ArrayListASDV aaa.add(3); aaa.add(4); - ArrayListASDV list1 = new ArrayListASDV(); ArrayListASDV list2 = new ArrayListASDV(4); ArrayListASDV list3 = new ArrayListASDV(4); @@ -661,22 +673,18 @@ public class ArrayListASDV list1.add(20); list3.add(new A1(-1)); list3.add(new A1(-2)); - Integer[] b = - { - 2, 3 - }; - /* + Integer[] b + = { + 2, 3 + }; list1.toArray(b); list2.add("a"); - try - { + try { list2.add(null); - } - catch (NullPointerException e) - { + } catch (NullPointerException e) { System.err.println(e); - }; + }; list2.add("b"); list2.add("c"); @@ -701,10 +709,9 @@ public class ArrayListASDV System.out.print("test toArray(): "); Object[] ar = list2.toArray(); System.out.print("[ "); - for (int i = 0; i < ar.length; ++i) - { + for (int i = 0; i < ar.length; ++i) { System.out.print(ar[i] + " "); - } + } System.out.println(" ] "); System.out.println("\n---------------------------------------"); @@ -718,10 +725,10 @@ public class ArrayListASDV System.out.println("\n---------------------------------------"); System.out.println("test add(index, element)"); - for (char a = 'Z'; a >= 'A'; --a) - { + for (char a = 'Z'; a >= 'A'; --a) { + System.out.println("array size: " + list2.size()); list2.add(0, "" + a); - } + } System.out.println(list2); list2.add(26, "z"); @@ -736,14 +743,11 @@ public class ArrayListASDV System.out.println(o); System.out.println(list2); - try - { + try { list2.remove(30); - } - catch (IndexOutOfBoundsException e) - { + } catch (IndexOutOfBoundsException e) { System.err.println(e); - } + } System.out.println("\n---------------------------------------"); System.out.println("test remove(Object)"); @@ -782,51 +786,45 @@ public class ArrayListASDV System.out.println(l2); List l3 = l2.subList(11, 11); System.out.println(l3); - try - { + try { l2.subList(12, 11); - } - catch (Exception e) - { + } catch (Exception e) { System.err.println(e); - } + } System.out.println("\n---------------------------------------"); System.out.println("test toArray()"); Object[] ar1 = l2.toArray(); - for (Object obj : ar1) - { + for (Object obj : ar1) { System.out.print(obj + " "); - } + } System.out.println("\n---------------------------------------"); System.out.println("test toArray(T[] a) small size a"); ArrayListASDV listX = new ArrayListASDV(); listX.add(10); listX.add(20); - Integer[] a1 = - { - 1 - }; + Integer[] a1 + = { + 1 + }; ar = listX.toArray(ar); - for (int i = 0; i < ar.length; ++i) - { + for (int i = 0; i < ar.length; ++i) { System.out.println(ar[i]); - } + } System.out.println("\n---------------------------------------"); System.out.println("test toArray(T[] a) Big size a"); ArrayListASDV listA1 = new ArrayListASDV(); listA1.add(new A1(100)); - A1[] a11 = - { - new A1(-1), new A1(-2), new A1(3) - }; + A1[] a11 + = { + new A1(-1), new A1(-2), new A1(3) + }; listA1.toArray(a11); - for (int i = 0; i < a11.length; ++i) - { + for (int i = 0; i < a11.length; ++i) { System.out.println(a11[i]); - } + } System.out.println(""); System.out.println("\n---------------------------------------"); @@ -836,10 +834,9 @@ public class ArrayListASDV Iterator it = list2.iterator(); - while (it.hasNext()) - { + while (it.hasNext()) { System.out.print(it.next() + " "); - } + } System.out.println(""); System.out.println("\n---------------------------------------"); @@ -851,19 +848,17 @@ public class ArrayListASDV li3.add(40); System.out.println(li3); ListIterator li = li3.listIterator(2); - while (li.hasNext()) - { + while (li.hasNext()) { System.out.print("\tnext index: " + li.nextIndex()); System.out.print("\tprevious index: " + li.previousIndex()); System.out.print("\t" + li.next()); - } + } System.out.println(""); - while (li.hasPrevious()) - { + while (li.hasPrevious()) { System.out.print("\tnext index: " + li.nextIndex()); System.out.print("\tprevious index: " + li.previousIndex()); System.out.print("\t" + li.previous()); - } + } System.out.println(""); System.out.println("next index: " + li.nextIndex()); System.out.println("previous index: " + li.previousIndex()); @@ -871,8 +866,7 @@ public class ArrayListASDV System.out.println("\n---------------------------------------"); System.out.println("test forEachRemaining()"); System.out.println(li.next()); - li.forEachRemaining(new Consumer() - { + li.forEachRemaining(new Consumer() { @Override public void accept(Integer t) { @@ -883,15 +877,13 @@ public class ArrayListASDV System.out.println("\n---------------------------------------"); System.out.println("test containsAll(Collection)"); - List ar33 = Arrays.asList(new Integer[] - { + List ar33 = Arrays.asList(new Integer[]{ 10, 20 - }); + }); System.out.println(li3.containsAll(ar33)); - ar33 = Arrays.asList(new Integer[] - { + ar33 = Arrays.asList(new Integer[]{ 10, -1 - }); + }); System.out.println(li3.containsAll(ar33)); System.out.println("\n---------------------------------------"); @@ -901,72 +893,58 @@ public class ArrayListASDV li3.add(11); li3.add(10); System.out.println(li3); - ar33 = Arrays.asList(new Integer[] - { + ar33 = Arrays.asList(new Integer[]{ 10 - }); + }); System.out.println(li3.removeAll(ar33)); System.out.println(li3); - List oar = Arrays.asList(new Object[] - { + List oar = Arrays.asList(new Object[]{ 3.3, 40, "abc" - }); - try - { + }); + try { li3.removeAll(oar); - } - catch (ClassCastException e) - { + } catch (ClassCastException e) { Thread.sleep(100); System.err.println(e); - } + } System.out.println(li3); - List sar = Arrays.asList(new A1[] - { + List sar = Arrays.asList(new A1[]{ new A1(999) - }); - try - { + }); + try { li3.removeAll(sar); - } - catch (ClassCastException e) - { + } catch (ClassCastException e) { Thread.sleep(100); System.err.println(e); - } + } System.out.println(li3); System.out.println("\n---------------------------------------"); System.out.println("test retainAll(Collection)"); - ar33 = Arrays.asList(new Integer[] - { + ar33 = Arrays.asList(new Integer[]{ 30 - }); + }); li3.retainAll(ar33); System.out.println(li3); System.out.println("\n---------------------------------------"); System.out.println("test addAll(Collection)"); - ar33 = Arrays.asList(new Integer[] - { + ar33 = Arrays.asList(new Integer[]{ 1, 2, 3, 4 - }); + }); li3.addAll(ar33); System.out.println(li3); System.out.println("\n---------------------------------------"); System.out.println("test addAll(index, Collection)"); - ar33 = Arrays.asList(new Integer[] - { + ar33 = Arrays.asList(new Integer[]{ 100, 200, 300 - }); + }); li3.addAll(2, ar33); System.out.println(li3); -*/ } } -class A1 implements Consumer -{ +class A1 implements Consumer { int x; @@ -984,23 +962,19 @@ class A1 implements Consumer @Override public boolean equals(Object obj) { - if (this == obj) - { + if (this == obj) { return true; - } - if (obj == null) - { + } + if (obj == null) { return false; - } - if (getClass() != obj.getClass()) - { + } + if (getClass() != obj.getClass()) { return false; - } + } final A1 other = (A1) obj; - if (this.x != other.x) - { + if (this.x != other.x) { return false; - } + } return true; } @@ -1012,5 +986,3 @@ class A1 implements Consumer } } - - diff --git a/Semester 2/Multicomponents_Lab_CalebFontenot/nb-configuration.xml b/Semester 2/Multicomponents_Lab_CalebFontenot/nb-configuration.xml new file mode 100644 index 0000000..5e1a2de --- /dev/null +++ b/Semester 2/Multicomponents_Lab_CalebFontenot/nb-configuration.xml @@ -0,0 +1,20 @@ + + + + + + 10-web + gfv700ee10 + Facelets + + diff --git a/Semester 2/Multicomponents_Lab_CalebFontenot/pom.xml b/Semester 2/Multicomponents_Lab_CalebFontenot/pom.xml new file mode 100644 index 0000000..49d5c3d --- /dev/null +++ b/Semester 2/Multicomponents_Lab_CalebFontenot/pom.xml @@ -0,0 +1,42 @@ + + 4.0.0 + edu.slcc.asdv.caleb + Multicomponents_Lab_CalebFontenot + 1.0-SNAPSHOT + war + Multicomponents_Lab_CalebFontenot-1.0-SNAPSHOT + + + UTF-8 + 10.0.0 + + + + + jakarta.platform + jakarta.jakartaee-api + ${jakartaee} + provided + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 11 + 11 + + + + org.apache.maven.plugins + maven-war-plugin + 3.3.2 + + + + \ No newline at end of file diff --git a/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/java/beans/Registration.java b/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/java/beans/Registration.java new file mode 100644 index 0000000..f33d2ca --- /dev/null +++ b/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/java/beans/Registration.java @@ -0,0 +1,112 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/JSF/JSFManagedBean.java to edit this template + */ + +package beans; + +import jakarta.inject.Named; +import jakarta.enterprise.context.RequestScoped; + +/** + * + * @author caleb + */ +@Named(value="registration") +@RequestScoped +public class Registration { + + private String lastName; + private String firstName; + private String mi; + private String gender; + + /** + * Get the value of gender + * + * @return the value of gender + */ + public String getGender() + { + return gender; + } + + /** + * Set the value of gender + * + * @param gender new value of gender + */ + public void setGender(String gender) + { + this.gender = gender; + } + + + public String getMi() + { + return mi; + } + + public void setMi(String mi) + { + this.mi = mi; + } + + + /** + * Get the value of firstName + * + * @return the value of firstName + */ + public String getFirstName() + { + return firstName; + } + + /** + * Set the value of firstName + * + * @param firstName new value of firstName + */ + public void setFirstName(String firstName) + { + this.firstName = firstName; + } + + + public String getLastName() + { + return lastName; + } + + public void setLastName(String lastName) + { + this.lastName = lastName; + } + public String getResponse() + { + if (lastName == null) { + return ""; // Request has not been made + } else { + String allMinor = ""; + for (String s : minor) { + allMinor += s + " "; + } + + String allHobby = ""; + for (String s : hobby) { + allHobby += s + " "; + } + + return "

You entered
" + + "Last Name: " + lastName + "
" + + "First Name: " + firstName + "
" + + "MI: " + mi + "
" + + "Gender: " + gender + "
" + + "Major: " + major + "
" + + "Minor: " + allMinor + "
" + + "Hobby: " + allHobby + "
" + + "Remarks: " + remarks + "

"; + } + } +} diff --git a/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/multicomponents_lab_calebfontenot/JakartaRestConfiguration.java b/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/multicomponents_lab_calebfontenot/JakartaRestConfiguration.java new file mode 100644 index 0000000..6f16197 --- /dev/null +++ b/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/multicomponents_lab_calebfontenot/JakartaRestConfiguration.java @@ -0,0 +1,13 @@ +package edu.slcc.asdv.caleb.multicomponents_lab_calebfontenot; + +import jakarta.ws.rs.ApplicationPath; +import jakarta.ws.rs.core.Application; + +/** + * Configures Jakarta RESTful Web Services for the application. + * @author Juneau + */ +@ApplicationPath("resources") +public class JakartaRestConfiguration extends Application { + +} diff --git a/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/multicomponents_lab_calebfontenot/resources/JakartaEE10Resource.java b/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/multicomponents_lab_calebfontenot/resources/JakartaEE10Resource.java new file mode 100644 index 0000000..5b62279 --- /dev/null +++ b/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/multicomponents_lab_calebfontenot/resources/JakartaEE10Resource.java @@ -0,0 +1,20 @@ +package edu.slcc.asdv.caleb.multicomponents_lab_calebfontenot.resources; + +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.core.Response; + +/** + * + * @author + */ +@Path("jakartaee10") +public class JakartaEE10Resource { + + @GET + public Response ping(){ + return Response + .ok("ping Jakarta EE") + .build(); + } +} diff --git a/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/resources/META-INF/persistence.xml b/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000..7582bf1 --- /dev/null +++ b/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/webapp/WEB-INF/beans.xml b/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/webapp/WEB-INF/beans.xml new file mode 100644 index 0000000..9dfae34 --- /dev/null +++ b/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/webapp/WEB-INF/beans.xml @@ -0,0 +1,6 @@ + + + \ No newline at end of file diff --git a/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/webapp/WEB-INF/glassfish-web.xml b/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/webapp/WEB-INF/glassfish-web.xml new file mode 100644 index 0000000..673cc06 --- /dev/null +++ b/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/webapp/WEB-INF/glassfish-web.xml @@ -0,0 +1,25 @@ + + + + + + + + Keep a copy of the generated servlet class' java code. + + + diff --git a/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/webapp/WEB-INF/web.xml b/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..fcfcd54 --- /dev/null +++ b/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,24 @@ + + + + jakarta.faces.PROJECT_STAGE + Development + + + Faces Servlet + jakarta.faces.webapp.FacesServlet + 1 + + + Faces Servlet + /faces/* + + + + 30 + + + + faces/index.xhtml + + diff --git a/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/webapp/index.html b/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/webapp/index.html new file mode 100644 index 0000000..3368e9c --- /dev/null +++ b/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/webapp/index.html @@ -0,0 +1,10 @@ + + + + Start Page + + + +

Hello World!

+ + diff --git a/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/webapp/index.xhtml b/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/webapp/index.xhtml new file mode 100644 index 0000000..ffcb20e --- /dev/null +++ b/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/webapp/index.xhtml @@ -0,0 +1,78 @@ + + + + + Facelet Title + + + + +

Student Registration Form + +

+ + + + + + + + + + + + + + + + Gender + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Remarks: + + + + + + +
+
+ diff --git a/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/webapp/resources/us.gif b/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/webapp/resources/us.gif new file mode 100644 index 0000000..2523dfc Binary files /dev/null and b/Semester 2/Multicomponents_Lab_CalebFontenot/src/main/webapp/resources/us.gif differ