Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the ListType class by adding the following methods: . Copy Constructor ad

ID: 3634830 • Letter: M

Question

Modify the ListType class by adding the following methods:

. Copy Constructor

add a copy constructor to the class that accepts a reference to another GeneralList Object.

. public int lastIndexOf (E element)

this method should return the last index of the specified element.

demonstrate the new methods in a simple program.

/** The ListType class is a concrete generic
class for storing a list of objects.
*/

public class ListType<E> implements GeneralList<E>
{
  
// Constants for the default capacity and
   // the resizing factor.
   private final int DEFAULT_CAPACITY = 10;
  
private final int RESIZE_FACTOR = 2;
  
  
// Private Fields
   private E[] list; // The list
   private int elements; // Number of elements stored
  
  
/** This constructor creates an empty list of the
   default capacity.
   */
   public ListType()
   {
// The following statement causes a compiler warning.
// It is a necessary work-around because we cannot
// instantiate an array of a generic type.
list = (E[])(new Object[DEFAULT_CAPACITY]);
elements = 0;
   }

  
/** This constructor creates an empty list of the
   specified capacity.
   @param capacity The initial capacity.
   @exception IllegalArgumentException if the
specified capacity is less than one.
   */
   public ListType(int capacity)
   {
if (capacity < 1)
  
throw new IllegalArgumentException();

// The following statement causes a compiler warning.
// It is a necessary work-around because we cannot
// instantiate an array of a generic type.
list = (E[])(new Object[capacity]);
elements = 0;
   }
  
  
/** Add an element to the end of the list.
@param element The element to add.
   */
   public void add(E element)
   {
// If the list is full, resize it.
if (elements == list.length)
   resize();

// Add element to the end of the list.
list[elements] = element;

// Adjust the number of elements.
elements++;
   }

  
/** Add an element at a specific index.
   @param index The added element's position.
   @param element The element to add.
   @exception IndexOutOtBoundsException When index
is out of bounds.
   */
  
public void add(int index, E element)
   {
// First make sure the index is valid.
if (index >= elements || index < 0)
throw new IndexOutOfBoundsException();

// If the list is full, resize it.
if (elements == list.length)
   resize();
  
// Shift the elements starting at index
// to the right one position.
for (int index2 = elements; index2 > index; index2--)
   list[index2] = list[index2 - 1];

// Add the new element at index.
list[index] = element;

// Adjust the number of elements.
elements++;
   }
  
  
/** Clear the list. */
   public void clear()
   {
for (int index = 0; index < list.length; index++)
   list[index] =
null;
elements = 0;
   }
  
  
/** Search the list for a specified element.
   @param element The element to search for.
   @return true if the list contains the element;
   false otherwise.
   */
   public boolean contains(E element)
   {
int index = 0; // Index counter
boolean found = false; // Search flag

// Step through the list. When the element
// is found, set found to true and stop.
while (!found && index < elements)
{
if (list[index].equals(element))
found =
true;
   index++;
}

// Return the status of the search.
return found;
   }
  
  
/** Get an element at a specific position.
   @param index The specified index.
   @return The element at index.
   @exception IndexOutOtBoundsException When index
   is out of bounds.
   */
   public E get(int index)
   {
if (index >= elements || index < 0)
  
throw new IndexOutOfBoundsException();
return list[index];
   }

  
/** Gets the index of the first occurrence of the
   specified element.
   @param element The element to search for.
   @return The index of the first occurrence of element
   if it exists; -1 if element is not in the list.
   */
   public int indexOf(E element)
   {
int index = 0; // Index counter
boolean found = false; // Search flag

// Step through the list. When the element
// is found, set found to true and stop.
while (!found && index < elements)
{
  
if (list[index].equals(element))
found =
true;
  
else
index++;
}

// Return the index of element or -1.
if (!found)
   index = -1;
  
return index;
   }
  
  
/** Determines whether the list is empty.
   @return true if the list is empty; false otherwise.
   */
   public boolean isEmpty()
   {
return (elements == 0);
   }
  
  
/** Remove a specific element from the list.
   @param element The element to remove.
   @return true if the element was found; false otherwise.
   */
   public boolean remove(E element)
   {
int index = 0; // Index counter
boolean found = false; // Search flag

// Perform a sequential search for element. When it is
// found, remove it and stop searching.
while (!found && index < elements)
{
  
if (list[index].equals(element))
   {
list[index] =
null;
found =
true;
}
   index++;
}

// If the value was found, shift all subsequent
// elements toward the front of the list.
if (found)
{
  
while(index < elements)
   {
list[index - 1] = list[index];
index++;
   }
  
  
// Adjust the number of elements.
   elements--;
}

// Return the status of the search.
return found;
   }
  
  
/** Remove an element at a specific index.
   @param index The index of the element to remove.
   @return The element that was removed.
   @exception IndexOutOtBoundsException When index
is out of bounds.
   */
  
public E remove(int index)
   {
if (index >= elements || index < 0)
  
throw new IndexOutOfBoundsException();

// Save the element, but remove it from the list.
E temp = list[index];
list[index] =
null;
index++;

// Shift all subsequent elements toward
// the front of the list.
while(index < elements)
{
   list[index - 1] = list[index];
   index++;
}

// Adjust the number of elements.
elements--;

// Return the element that was removed.
return temp;
   }
  
  
/** Resizes the list to twice its current length. */
   private void resize()
   {
// Calculate the new length, which is the current
// length multiplied by the resizing factor.
int newLength = list.length * RESIZE_FACTOR;

// Create a new list.
// Note: This statement causes a compiler warning.
// It is a necessary work-around because we cannot
// instantiate an array of a generic type.
E[] tempList = (E[])(new Object[newLength]);

// Copy the existing elements to the new list.
for (int index = 0; index < elements; index++)
   tempList[index] = list[index];

// Replace the existing list with the new one.
list = tempList;
   }

  
/** Replace the element at a specified index with
   another element.
   @param index The index of the element to replace.
   @param element The element to replace it with.
   @return The element previously stored at the index.
   @exception IndexOutOtBoundsException When index
is out of bounds.
   */
   public E set(int index, E element)
   {
if (index >= elements || index < 0)
  
throw new IndexOutOfBoundsException();

// Save the existing element at that index.
E temp = list[index];

// Replace the element with element.
list[index] = element;
  
// Return the previously stored element.
return temp;
   }

  
/** Get the number of elements in the list.
   @return The number of elements in the list.
   */
   public int size()
   {
return elements;
   }
  
  
/** Convert the list to a String array.
   @return A String array representing the the list.
   */
   public String[] toStringArray()
   {
  
// Create a String array large enough to hold
// all the elements of the list.
String[] strArray = new String[elements];

// Store each element's toString() return value
// as an element in the String array.
for (int index = 0; index < elements; index++)
   strArray[index] = list[index].toString();

// Return the String array.
return strArray;
   }
}

-------------------------------------------------------------------------------------------------------------------------------

/**
   The GeneralList interface specifies the general
   operations that should be supported by a list.
*/

public interface GeneralList<E>
{
  
/** The add method adds an element to the list.
   @param element The element to add.
   */
   public void add(E element);

  
/** The add method adds an element at a specific index.
   @param index The added element's position.
   @param element The element to add.
   @exception IndexOutOtBoundsException When index
is out of bounds.
   */
  
public void add(int index, E element);

  
/** The clear method clears the list. */
   public void clear();
  
  
/** The contains method searches the list
   for a specified element.
   @param element The element to search for.
   @return true if the list contains the element;
   false otherwise.
   */
   public boolean contains(E element);
  
  
/** The get method gets an element at a specific position.
   @param index The specified index.
   @return The element at index.
   @exception IndexOutOtBoundsException When index
is out of bounds.
   */
   public E get(int index);
  
  
/** The indexOf method gets the index of the first
   occurrence of the specified element.
   @param element The element to search for.
   @return The index of the first occurrence of element
   if it exists; -1 if element is not in the list.
   */
   public int indexOf(E element);
  
  
/** The isEmpty method determines whether the list is empty.
   @return true if the list is empty; false otherwise.
   */
   public boolean isEmpty();
  
  
/** The remove method removse a specific element from the list.
   @param element The element to remove.
   @return true if the element was found; false otherwise.
   */
   public boolean remove(E element);
  
  
/** The remove method removes an element at a specific index.
   @param index The index of the element to remove.
   @return The element that was removed.
   @exception IndexOutOtBoundsException When index
is out of bounds.
   */
  
public E remove(int index);
  
  
/** The set method replaces the element at a specified
   index with another element.
   @param index The index of the element to replace.
   @param element The element to replace it with.
   @return The element previously stored at the index.
   @exception IndexOutOtBoundsException When index
is out of bounds.
   */
   public E set(int index, E element);

  
/** The size method gets the number of elements in the list.
   @return The number of elements in the list.
   */
   public int size();
}

-------------------------------------------------------------------------------------------------------------------------------

/**
   The GeneralList interface specifies the general
   operations that should be supported by a list.
*/

public interface GeneralList<E>
{
  
/** The add method adds an element to the list.
   @param element The element to add.
   */
   public void add(E element);

  
/** The add method adds an element at a specific index.
   @param index The added element's position.
   @param element The element to add.
   @exception IndexOutOtBoundsException When index
is out of bounds.
   */
  
public void add(int index, E element);

  
/** The clear method clears the list. */
   public void clear();
  
  
/** The contains method searches the list
   for a specified element.
   @param element The element to search for.
   @return true if the list contains the element;
   false otherwise.
   */
   public boolean contains(E element);
  
  
/** The get method gets an element at a specific position.
   @param index The specified index.
   @return The element at index.
   @exception IndexOutOtBoundsException When index
is out of bounds.
   */
   public E get(int index);
  
  
/** The indexOf method gets the index of the first
   occurrence of the specified element.
   @param element The element to search for.
   @return The index of the first occurrence of element
   if it exists; -1 if element is not in the list.
   */
   public int indexOf(E element);
  
  
/** The isEmpty method determines whether the list is empty.
   @return true if the list is empty; false otherwise.
   */
   public boolean isEmpty();
  
  
/** The remove method removse a specific element from the list.
   @param element The element to remove.
   @return true if the element was found; false otherwise.
   */
   public boolean remove(E element);
  
  
/** The remove method removes an element at a specific index.
   @param index The index of the element to remove.
   @return The element that was removed.
   @exception IndexOutOtBoundsException When index
is out of bounds.
   */
  
public E remove(int index);
  
  
/** The set method replaces the element at a specified
   index with another element.
   @param index The index of the element to replace.
   @param element The element to replace it with.
   @return The element previously stored at the index.
   @exception IndexOutOtBoundsException When index
is out of bounds.
   */
   public E set(int index, E element);

  
/** The size method gets the number of elements in the list.
   @return The number of elements in the list.
   */
   public int size();
}

-------------------------------------------------------------------------------------------------------------------------------

/** This program gives a simple demonstration of some
of the generic ListType class's methods.
*/

public class GenericListDemo
{
   public static void main(String[] args)
   {
// Create a list to hold Car objects.
ListType<Car> carList = new ListType<Car>();

// Create a few Car objects.
Car vw = new Car("227H54", "1997 Volkswagen");
Car porsche = new Car("453B55", "2007 Porsche");
Car bmw = new Car("177R60", "1980 BMW");

// Add the cars to the list.
carList.add(vw);
carList.add(porsche);
carList.add(bmw);

// Display the elements in the list.
System.out.println("Here are the elements in the list:");
displayList(carList);

// Remove the first and last elements.
carList.remove(0);
carList.remove(carList.size() - 1);

// Display the list.
System.out.println(" Now with the first and last removed:");
displayList(carList);

// Create two new Car objects.
Car mustang = new Car("448A69", "1965 Mustang");
Car corvette = new Car("99C33", "2006 Corvette");

// Add the new Cars to the beginning and end of the list.
carList.add(0, mustang);
carList.add(corvette);

// Display the list one last time.
System.out.println(" Now with new first and last elements:");
displayList(carList);
   }

   /** The displayList method displays the elements in
   an GeneralList object.
   @param list The list to display.
   */
   public static void displayList(GeneralList list)
   {
for (int index = 0; index < list.size(); index++)
{
   System.out.println("Index " + index + ": " +
list.get(index));
}
   }
}

Explanation / Answer

Please Rate:Thanks

1. Modify the ListType class adding the following methods:
- public int getCapacity()
This method should return the current capacity of the list.
- public void ensureCapacity(int minCapacity)
If the list's capacity is less than minCapacity, this method should increase it so it is equal to minCapacity.
- public void trimSize()
This method trims the list's capacity so it is equal to the list's current size.
- Copy Constructor
Add a copy constructor to the class that accepts a reference to another GeneralList object.
- public int lastIndexOf(E element)
This method should return the last index of the specified element.
- public void removeRange(int fromIndex, int toIndex)
This method should remove all the elements with indexes in the range of fromIndex (inclusive) and toIndex (exclusive).
All of the succeedng elements in the list should be shifted toward the front of the list to occupy the gap left by the removed elements.
This method should trown an IndexOfBoundException if either of the specified indexes is invalid.

Demonstrate the new methods is a simple program.
*/

/** The ListType class is a concrete generic
class for storing a list of objects.
*/

public class ListType<E> implements GeneralList<E>
{
// Constants for the default capacity and
// the resizing factor.
private final int DEFAULT_CAPACITY = 10;
private final int RESIZE_FACTOR = 2;


// Private Fields
private E[] list; // The list
private int elements; // Number of elements stored

/** This constructor creates an empty list of the
default capacity.
*/
public ListType()
{
// The following statement causes a compiler warning.
// It is a necessary work-around because we cannot
// instantiate an array of a generic type.
list = (E[])(new Object[DEFAULT_CAPACITY]);
elements = 0;
}

/** This constructor creates an empty list of the
specified capacity.
@param capacity The initial capacity.
@exception IllegalArgumentException if the
specified capacity is less than one.
*/
public ListType(int capacity)
{
if (capacity < 1)
throw new IllegalArgumentException();

// The following statement causes a compiler warning.
// It is a necessary work-around because we cannot
// instantiate an array of a generic type.
list = (E[])(new Object[capacity]);
elements = 0;
}
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote