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 Write a class to implement a list with the ability to insert a new item...
JAVA Write a class to implement a list with the ability to insert a new item at any location within the list, remove an item, and search for an item. The list should use a linked list implementation. This implementation should make use of a dummy node at the head of the list and should have explict references head and previous. Add a method to the list that inserts elements in acsending order assuming the list is already sorted before...
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...
Create in Java Create a stack class to store integers and implement following methods: 1- void...
Create in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
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...
Using the Java programming language: Create and implement a class Car that models a car. A...
Using the Java programming language: Create and implement a class Car that models a car. A Car is invented to have a gasoline tank that can hold a constant max of 12.5 gallons, and an odometer that is set to 0 for a new car. All cars have an original fuel economy of 23.4 miles per gallon, but the value is not constant. Provide methods for constructing an instance of a Car (one should be zero parameter, and the other...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT