Question

In: Computer Science

Java the goal is to create a list class that uses an array to implement the...

Java the goal is to create a list class that uses an array to implement the interface below. I'm having trouble figuring out the remove(T element) and set(int index, T element). I haven't added any custom methods other than a simple expand method that doubles the size by 2. I would prefer it if you did not use any other custom methods. Please use Java Generics, Thank you.

import java.util.*;

/**
* Interface for an Iterable, Indexed, Unsorted List ADT.
* Iterators and ListIterators provided by the list are required
* to be "fail-fast" and throw ConcurrentModificationException if
* the iterator detects any change to the list from another source.
* Note: "Unsorted" only means that it is not inherently maintained
* in a sorted order. It may or may not be sorted.
*
* @author CS 221
*
* @param - class of objects stored in the list
*/
public interface IndexedUnsortedList extends Iterable
{
/**
* Adds the specified element to the front of this list.
*
* @param element the element to be added to the front of this list
*/
public void addToFront(T element);

/**
* Adds the specified element to the rear of this list.
*
* @param element the element to be added to the rear of this list
*/
public void addToRear(T element);

/**
* Adds the specified element to the rear of this list.
*
* @param element the element to be added to the rear of the list
*/
public void add(T element);

/**
* Adds the specified element after the specified target.
*
* @param element the element to be added after the target
* @param target the target is the item that the element will be added after
* @throws NoSuchElementException if target element is not in this list
*/
public void addAfter(T element, T target);
  
/**
* Inserts the specified element at the specified index.
*
* @param index the index into the array to which the element is to be inserted.
* @param element the element to be inserted into the array
* @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index > size)
*/
public void add(int index, T element);

/**
* Removes and returns the first element from this list.
*
* @return the first element from this list
* @throws NoSuchElementException if list contains no elements
*/
public T removeFirst();

/**
* Removes and returns the last element from this list.
*
* @return the last element from this list
* @throws NoSuchElementException if list contains no elements
*/
public T removeLast();

/**
* Removes and returns the first element from the list matching the specified element.
*
* @param element the element to be removed from the list
* @return removed element
* @throws NoSuchElementException if element is not in this list
*/
public T remove(T element);

/**
* Removes and returns the element at the specified index.
*
* @param index the index of the element to be retrieved
* @return the element at the given index
* @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size)
*/
public T remove(int index);
  
/**
* Replace the element at the specified index with the given element.
*
* @param index the index of the element to replace
* @param element the replacement element to be set into the list
* @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size)
*/
public void set(int index, T element);

/**
* Returns a reference to the element at the specified index.
*
* @param index the index to which the reference is to be retrieved from
* @return the element at the specified index
* @throws IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size)
*/
public T get(int index);

/**
* Returns the index of the first element from the list matching the specified element.
*
* @param element the element for the index is to be retrieved
* @return the integer index for this element or -1 if element is not in the list
*/
public int indexOf(T element);

/**
* Returns a reference to the first element in this list.
*
* @return a reference to the first element in this list
* @throws NoSuchElementException if list contains no elements
*/
public T first();

/**
* Returns a reference to the last element in this list.
*
* @return a reference to the last element in this list
* @throws NoSuchElementException if list contains no elements
*/
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, else false
*/
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 a string representation of this list.
*
* @return a string representation of this list
*/
public String toString();

/**
* Returns an Iterator for the elements in this list.
*
* @return an Iterator over the elements in this list
*/
public Iterator iterator();

/**
* Returns a ListIterator for the elements in this list.
*
* @return a ListIterator over the elements in this list
*
* @throws UnsupportedOperationException if not implemented
*/
public ListIterator listIterator();

/**
* Returns a ListIterator for the elements in this list, with
* the iterator positioned before the specified index.
*
* @return a ListIterator over the elements in this list
*
* @throws UnsupportedOperationException if not implemented
*/
public ListIterator listIterator(int startingIndex);
}

Solutions

Expert Solution

Code for IndexedUnsortedList.java

import java.util.*;

/**
* Interface for an Iterable, Indexed, Unsorted List ADT. Iterators and
* ListIterators provided by the list are required to be "fail-fast" and throw
* ConcurrentModificationException if the iterator detects any change to the
* list from another source. Note: "Unsorted" only means that it is not
* inherently maintained in a sorted order. It may or may not be sorted.
*
* @author CS 221
*
* @param <T>
* - class of objects stored in the list
*/
public interface IndexedUnsortedList<T> extends Iterable<T> {
   /**
   * Adds the specified element to the front of this list.
   *
   * @param element
   * the element to be added to the front of this list
   */
   public void addToFront(T element);

   /**
   * Adds the specified element to the rear of this list.
   *
   * @param element
   * the element to be added to the rear of this list
   */
   public void addToRear(T element);

   /**
   * Adds the specified element to the rear of this list.
   *
   * @param element
   * the element to be added to the rear of the list
   */
   public void add(T element);

   /**
   * Adds the specified element after the specified target.
   *
   * @param element
   * the element to be added after the target
   * @param target
   * the target is the item that the element will be added after
   * @throws NoSuchElementException
   * if target element is not in this list
   */
   public void addAfter(T element, T target);

   /**
   * Inserts the specified element at the specified index.
   *
   * @param index
   * the index into the array to which the element is to be inserted.
   * @param element
   * the element to be inserted into the array
   * @throws IndexOutOfBoundsException
   * if the index is out of range (index < 0 || index > size)
   */
   public void add(int index, T element);

   /**
   * Removes and returns the first element from this list.
   *
   * @return the first element from this list
   * @throws NoSuchElementException
   * if list contains no elements
   */
   public T removeFirst();

   /**
   * Removes and returns the last element from this list.
   *
   * @return the last element from this list
   * @throws NoSuchElementException
   * if list contains no elements
   */
   public T removeLast();

   /**
   * Removes and returns the first element from the list matching the specified
   * element.
   *
   * @param element
   * the element to be removed from the list
   * @return removed element
   * @throws NoSuchElementException
   * if element is not in this list
   */
   public T remove(T element);

   /**
   * Removes and returns the element at the specified index.
   *
   * @param index
   * the index of the element to be retrieved
   * @return the element at the given index
   * @throws IndexOutOfBoundsException
   * if the index is out of range (index < 0 || index >= size)
   */
   public T remove(int index);

   /**
   * Replace the element at the specified index with the given element.
   *
   * @param index
   * the index of the element to replace
   * @param element
   * the replacement element to be set into the list
   * @throws IndexOutOfBoundsException
   * if the index is out of range (index < 0 || index >= size)
   */
   public void set(int index, T element);

   /**
   * Returns a reference to the element at the specified index.
   *
   * @param index
   * the index to which the reference is to be retrieved from
   * @return the element at the specified index
   * @throws IndexOutOfBoundsException
   * if the index is out of range (index < 0 || index >= size)
   */
   public T get(int index);

   /**
   * Returns the index of the first element from the list matching the specified
   * element.
   *
   * @param element
   * the element for the index is to be retrieved
   * @return the integer index for this element or -1 if element is not in the
   * list
   */
   public int indexOf(T element);

   /**
   * Returns a reference to the first element in this list.
   *
   * @return a reference to the first element in this list
   * @throws NoSuchElementException
   * if list contains no elements
   */
   public T first();

   /**
   * Returns a reference to the last element in this list.
   *
   * @return a reference to the last element in this list
   * @throws NoSuchElementException
   * if list contains no elements
   */
   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, else false
   */
   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 a string representation of this list.
   *
   * @return a string representation of this list
   */
   public String toString();

   /**
   * Returns an Iterator for the elements in this list.
   *
   * @return an Iterator over the elements in this list
   */
   public Iterator<T> iterator();

   /**
   * Returns a ListIterator for the elements in this list.
   *
   * @return a ListIterator over the elements in this list
   *
   * @throws UnsupportedOperationException
   * if not implemented
   */
   public ListIterator<T> listIterator();

   /**
   * Returns a ListIterator for the elements in this list, with the iterator
   * positioned before the specified index.
   *
   * @return a ListIterator over the elements in this list
   *
   * @throws UnsupportedOperationException
   * if not implemented
   */
   public ListIterator<T> listIterator(int startingIndex);
}

Code Screenshots:

Hope it helps, if you like the answer give it thumbs up. Thank you.


Related Solutions

JAVA * create Q3 class to implement comparator for a TreeSet. The purpose of       ...
JAVA * create Q3 class to implement comparator for a TreeSet. The purpose of        * the comparator is to compare the alphabetic order of integers. With Q3,        * the order of Integer elements will be sort according to the order of        * digit Unicode.        * For example, the value of {11,3,31,2} will return {11,2,3,31}        * NOTE: You don't need to compare each digit one by one. Just compare them System.out.println("Q3...
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for...
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for a polynomial. Much of this work can be modelled on the C++ dynamic array of ints List that we discussed in class. This class does not need the method the overloaded += operator. Your class should have the following methods: Write one constructor that takes an integer n for the degree of a term and a double coefficient c for the coefficient of the...
Create a program in java with the following information: Design a program that uses an array...
Create a program in java with the following information: Design a program that uses an array with specified values to display the following: The lowest number in the array The highest number in the array The total of the numbers in the array The average of the numbers in the array Initialize an array with these specific 20 numbers: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51 example...
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside...
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside of main declare two static final variables and integer for number of days in the week and a double for the revenue per pizza (which is $8.50). Create a method called main Inside main: Declare an integer array that can hold the number of pizzas purchased each day for one week. Declare two additional variables one to hold the total sum of pizzas sold...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items in the...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Create the following java program with class list that outputs: //output List Empty List Empty List...
Create the following java program with class list that outputs: //output List Empty List Empty List Empty Item not found Item not found Item not found Original list Do or do not. There is no try. Sorted Original List Do There do is no not. or try. Front is Do Rear is try. Count is 8 Is There present? true Is Dog present? false List with junk junk Do or moremorejunk do not. There is no try. morejunk Count is...
JAVA How to make a graph class that uses a linked list class to store nodes...
JAVA How to make a graph class that uses a linked list class to store nodes and linked list within each node to store adjacency list The linked list class has been made already.   import java.util.*; public class LinkedList implements Iterable { private int size = 0; private Node head; private Node tail; private class Node { private T data; private Node prev; private Node next; public Node(T data) { this.data = data; } }    public Iterator iterator() {...
Language: Java Question:Using your Circle class (or the one provided below), create a Circle array of...
Language: Java Question:Using your Circle class (or the one provided below), create a Circle array of size 4 in a driver class using the following statement: Circle circleArr[] = new Circle[4]; Populate the array with four different radiuses and then, using a for loop from 0 to one less then the length of the array, print the area and the diameter of each of the four circles Circle Class: import java.text.DecimalFormat; public class Circle {    DecimalFormat dec = new...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT