The purpose of this exercise is to create an Array List data structure that mimics the behavior of the Java Standard Library Version. Task Check List  ONLY "for" loops should be used within the data structure class. The names of identifiers MUST match the names listed in the description below.  data fields: The fields to declare have private accessibility. size : stores the number of occupied locations in internal array, and is of type int. data : is a reference variable for the internal array and is of type E[ ]. constant: Use to describe defaults. DEFAULT_CAPACITY is a constant that holds a default capacity of  ten (10) for  the underlying array, is of type int , public accessibility and modified to static.  constructors: The overloaded constructors will initialize the data fields size and data. The default constructor calls the second constructor, generating an internal array with the specified DEFAULT_CAPACITY.       public ArrayList() The second constructor generates an internal array with the specified initial capacity. If the capacity is less than zero, this constructor should throw an IllegalArgumentException.   @SuppressWarnings("unchecked")       public ArrayList(int capacity)       Note: Java does not allow you to construct a generic array i.e     data = new E[capacity];            // illegal       so, you must use     data = (E[])new Object[capacity];   // valid generic type: Allows the uses of different data types. methods: manages the behavior of the internal array. Together, the methods below give the illusion of a dynamic array (an array that grows or shrinks). Implement these methods with in your generic Array List class.    Method Description Header  add(item) appends the item specified to the end of the list and updates the number of elements one at a time. This method returns true, if the data was added successfully.  public boolean add(E item)  add(index, item) inserts the item specified at the given index in the list. Shifts subsequent elements to the right and updates the number of elements in list one at a time.  public void add(int index, E item)  checkIndex(index) checks if the given index is valid. Throws an IndexOutOfBoundsException, if invalid. Validation means that you cannot access indexes where elements have not been placed. This is a private helper method. Note: This method should be used in any method that requires a precondition for access to valid index.  private void checkIndex(int index)   clear() clears list of all elements for garbage collection, returns size back to zero.  public void clear()  contains(item) searches for a specific value within the internal array and returns true, if the value is in the list.  public boolean contains(E item)  ensureCapacity( targetCapacity) doubles the capacity of the underlying array, or if the targetCapacity is greater that double the capacity increases the array to the targetCapacity to ensure that it can hold the number of elements specified by the targetCapacity value. This will most likely have a cast to a generic type.  @SuppressWarnings("unchecked")  public void ensureCapacity(int targetCapacity)  get(index) returns the item at the specified position in the list. This method first checks if the index requested is valid.  public E get(int index)  indexOf(item) searches for a specific item within the linked structure and returns the first occurrence (i.e. index location) in the array if found, otherwise returns -1 to indicate that the item was NOT FOUND.  public int indexOf(E item)  isEmpty() returns true, if the list is empty, i.e., the list contains no elements.  public boolean isEmpty()  remove(index) removes the item at the given index in the list. Shifts subsequent elements to the left and returns the item removed. This method first checks if the index requested is valid and reduces the number of elements by one.  public E remove(int index)  remove(item) removes the first occurrence of the item specified from the list, if present. Shifts subsequent elements to the left and returns true, if the item is removed. Reduces the number of elements by one.  public boolean remove(E item)  set(index, item) replaces the item at the specified position with the one specified. This method validates the index before replacing the item.   public E set(int index, E item)  shiftLeft(index) helper method that shifts elements of internal array left to index location. Does not update the size of the list.  private void shiftLeft(int index)  shiftRight(index) helper method that shifts elements of internal array right from index location. Does not update the size of the list.  private void shiftRight(int index)    size() returns the number of elements in the list. This is the size of the occupied locations of the array.    public int size()  toString() displays the contents of the list.  public String toString()

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter8: Arrays
Section: Chapter Questions
Problem 9PE
icon
Related questions
Question

The purpose of this exercise is to create an Array List data structure that mimics the behavior of the Java Standard Library Version.

Task Check List 

  • ONLY "for" loops should be used within the data structure class.
  • The names of identifiers MUST match the names listed in the description below. 

data fields: The fields to declare have private accessibility.

  • size : stores the number of occupied locations in internal array, and is of type int.
  • data : is a reference variable for the internal array and is of type E[ ].

constant: Use to describe defaults.

  • DEFAULT_CAPACITY is a constant that holds a default capacity of  ten (10) for  the underlying array, is of type int , public accessibility and modified to static

constructors: The overloaded constructors will initialize the data fields size and data.

  • The default constructor calls the second constructor, generating an internal array with the specified DEFAULT_CAPACITY.

      public ArrayList()

  • The second constructor generates an internal array with the specified initial capacity. If the capacity is less than zero, this constructor should throw an IllegalArgumentException.

  @SuppressWarnings("unchecked")
      public ArrayList(int capacity)

      Note: Java does not allow you to construct a generic array i.e

    data = new E[capacity];            // illegal

      so, you must use

    data = (E[])new Object[capacity];   // valid

generic type: Allows the uses of different data types.

methods: manages the behavior of the internal array.

Together, the methods below give the illusion of a dynamic array (an array that grows or shrinks). Implement these methods with in your generic Array List class.   

Method

Description

Header

 add(item) appends the item specified to the end of the list and updates the number of elements one at a time. This method returns true, if the data was added successfully.

 public boolean add(E item)

 add(index, item) inserts the item specified at the given index in the list. Shifts subsequent elements to the right and updates the number of elements in list one at a time.

 public void add(int index, E item)

 checkIndex(index)

checks if the given index is valid. Throws an IndexOutOfBoundsException, if invalid. Validation means that you cannot access indexes where elements have not been placed. This is a private helper method. Note: This method should be used in any method that requires a precondition for access to valid index.

 private void checkIndex(int index)

  clear()

clears list of all elements for garbage collection, returns size back to zero.

 public void clear()

 contains(item) searches for a specific value within the internal array and returns true, if the value is in the list.

 public boolean contains(E item)

 ensureCapacity( targetCapacity) doubles the capacity of the underlying array, or if the targetCapacity is greater that double the capacity increases the array to the targetCapacity to ensure that it can hold the number of elements specified by the targetCapacity value. This will most likely have a cast to a generic type.

 @SuppressWarnings("unchecked")

 public void ensureCapacity(int targetCapacity)

 get(index) returns the item at the specified position in the list. This method first checks if the index requested is valid.

 public E get(int index)

 indexOf(item) searches for a specific item within the linked structure and returns the first occurrence (i.e. index location) in the array if found, otherwise returns -1 to indicate that the item was NOT FOUND.

 public int indexOf(E item)

 isEmpty()

returns true, if the list is empty, i.e., the list contains no elements.

 public boolean isEmpty()

 remove(index) removes the item at the given index in the list. Shifts subsequent elements to the left and returns the item removed. This method first checks if the index requested is valid and reduces the number of elements by one.

 public E remove(int index)

 remove(item) removes the first occurrence of the item specified from the list, if present. Shifts subsequent elements to the left and returns true, if the item is removed. Reduces the number of elements by one.

 public boolean remove(E item)

 set(index, item) replaces the item at the specified position with the one specified. This method validates the index before replacing the item. 

 public E set(int index, E item)

 shiftLeft(index)

helper method that shifts elements of internal array left to index location. Does not update the size of the list.

 private void shiftLeft(int index)

 shiftRight(index)

helper method that shifts elements of internal array right from index location. Does not update the size of the list.

 private void shiftRight(int index)

 

 size() returns the number of elements in the list. This is the size of the occupied locations of the array.  

 public int size()

 toString() displays the contents of the list.

 public String toString()

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming with Microsoft Visual Basic 2017
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:
9781337102124
Author:
Diane Zak
Publisher:
Cengage Learning