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

1. Modify the class ArrayList given in the lecture by adding to it the functions

ID: 3545312 • Letter: 1

Question

1. Modify the class ArrayList given in the lecture by adding to it the functions listed below for Exercise 2. In each case, the appropriate error message should be generated if an invalid condition occurs. For example, an error message should be generated when trying to insert an item in a given location in the list and the location is out of range. Create a main class to test each of your methods that were upgraded. The code is below:

public class ArrayList

{

    /**

     * Default constructor. Sets length to 0, initializing the list as an empty

     * list. Default size of array is 20.

     */

    public ArrayList()

    {

        list = new int[SIZE];

        length = 0;

    }


    /**

     * Determines whether the list is empty

     *

     * @return true if the list is empty, false otherwise

     */

    public boolean isEmpty()

    {

        return length == 0;

    }


    /**

     * Prints the list elements.

     */

    public void display()

    {

        for (int i = 0; i < length; i++)

        {

            System.out.print(list[i] + " ");

        }


        System.out.println();

    }


    /**

     * Adds the element x to the end of the list. List length is increased by 1.

     *

     * @param x element to be added to the list

     */

    public void add(int x)

    {

        if (length == SIZE)

        {

            System.out.println("Insertion Error: list is full");

        } else

        {

            list[length] = x;

            length++;

        }

    }


    /**

     * Removes the element at the given location from the list. List length is

     * decreased by 1.

     *

     * @param pos location of the item to be removed

     */

    public void removeAt(int pos)

    {

        for (int i = pos; i < length - 1; i++)

        {

            list[i] = list[i + 1];

        }

        length--;

    }


    //Implementation of methods in the lab exercise

    /**

     * Non default constructor. Sets length to 0, initializing the list as an

     * empty list. Size of array is passed as a parameter.

     *

     * @param size size of the array list

     */

    public ArrayList(int size)

    {

    }


    /**

     * Returns the number of items in the list (accessor method).

     *

     * @return the number of items in the list.

     */

    public int getLength()

    {

        return -1;

    }


    /**

     * Returns the size of the list (accessor method).

     *

     * @return the size of the array

     */

    public int getSize()

    {

        return -1;

    }


    /**

     * Removes all of the items from the list. After this operation, the length

     * of the list is zero.

     */

    public void clear()

    {

    }


    /**

     * Replaces the item in the list at the position specified by location.

     *

     * @param location location of the element to be replaced

     * @param item value that will replace the value at location

     */

    public void replace(int location, int item)

    {

    }


    /**

     * Adds an item to the list at the position specified by location.

     *

     * @param location location where item will be added.

     * @param item item to be added to the list.

     */

    public void add(int location, int item)

    {

    }


    /**

     * Deletes an item from the list. All occurrences of item in the list will

     * be removed.

     *

     * @param item element to be removed.

     */

    public void remove(int item)

    {

    }


    /**

     * Returns the element at location

     *

     * @param location position in the list of the item to be returned

     * @return element at location

     */

    public int get(int location)

    {

        return -1;

    }

    

    private static int SIZE = 20;   //size of the array that stores the list items

    private int[] list;             //array to store the list items

    private int length;             //amount of items in the list

}


a. ArrayList(int size): create a constructor that sets the size of the array list to the value passed in size (note that the class variable SIZE cannot be final anymore). b. int length(): create this function to determine the number of items in the list (accessor function). c. int getSize(): create this function to determine the size of the list (accessor function). d. void clear(): create this function to remove all of the items from the list. After this operation, the length of the list is zero. e. void replace(int location, int item): create this function to replace the item in the list at the position specified by location. The item should be replaced with item. f. void insert(int location, int item): create this function to add an item to the list at the position specified by location. g. void remove(int item): create this function to delete an item from the list. All occurrences of item in the list should be removed. h. int get(int location): create a function that returns the element at location.

Explanation / Answer

public class ArrayList

{

/**

* Default constructor. Sets length to 0, initializing the list as an empty

* list. Default size of array is 20.

*/

public ArrayList()

{

list = new int[SIZE];

length = 0;

}


/**

* Determines whether the list is empty

*

* @return true if the list is empty, false otherwise

*/

public boolean isEmpty()

{

return length == 0;

}


/**

* Prints the list elements.

*/

public void display()

{

for (int i = 0; i < length; i++)

{

System.out.print(list[i] + " ");

}


System.out.println();

}


/**

* Adds the element x to the end of the list. List length is increased by 1.

*

* @param x

* element to be added to the list

*/

public void add(int x)

{

if (length == SIZE)

{

System.out.println("Insertion Error: list is full");

}

else

{

list[length] = x;

length++;

}

}


/**

* Removes the element at the given location from the list. List length is

* decreased by 1.

*

* @param pos

* location of the item to be removed

*/

public void removeAt(int pos)

{

for (int i = pos; i < length - 1; i++)

{

list[i] = list[i + 1];

}

length--;

}


// Implementation of methods in the lab exercise

/**

* Non default constructor. Sets length to 0, initializing the list as an

* empty list. Size of array is passed as a parameter.

*

* @param size

* size of the array list

*/

public ArrayList(int size)

{

SIZE = size;

list = new int[SIZE];

length = 0;

}


/**

* Returns the number of items in the list (accessor method).

*

* @return the number of items in the list.

*/

public int getLength()

{

return length;

}


/**

* Returns the size of the list (accessor method).

*

* @return the size of the array

*/

public int getSize()

{

return SIZE;

}


/**

* Removes all of the items from the list. After this operation, the length

* of the list is zero.

*/

public void clear()

{

length = 0;

}


/**

* Replaces the item in the list at the position specified by location.

*

* @param location

* location of the element to be replaced

* @param item

* value that will replace the value at location

*/

public void replace(int location, int item)

{

if (location < 0 || location >= length)

{

System.out.println("Error: The position is invalid.");

}

else

{

list[location] = item;

}

}


/**

* Adds an item to the list at the position specified by location.

*

* @param location

* location where item will be added.

* @param item

* item to be added to the list.

*/

public void add(int location, int item)

{

if (length == SIZE)

{

System.out.println("Insertion Error: list is full");

}

else

{

for (int i = length; i > location; i--)

list[i] = list[i-1];

list[location] = item;

length++;

}

}


/**

* Deletes an item from the list. All occurrences of item in the list will

* be removed.

*

* @param item

* element to be removed.

*/

public void remove(int item)

{

for (int i = 0; i < length; i++)

{

if (list[i] == item)

{

removeAt(i);

i--;

}

}

}


/**

* Returns the element at location

*

* @param location

* position in the list of the item to be returned

* @return element at location

*/

public int get(int location)

{

if (location < 0 || location >= length)

{

System.out.println("Error: The position is invalid.");

return 0;

}

else

{

return list[location];

}

}


private int SIZE = 20; // size of the array that stores the list

// items

private int[] list; // array to store the list items

private int length; // amount of items in the list

  

public static void main(String[] args)

{

ArrayList list = new ArrayList(30);

list.add(1);

list.add(2);

list.add(3);

list.add(1, 11);

list.display();

System.out.println("size = " + list.getSize());

System.out.println("length = " + list.getLength());

System.out.println("item at(1) = " + list.get(1));

list.replace(1, 22);

System.out.println("after replace, item at(1) = " + list.get(1));

list.add(4);

list.add(3);

list.display();

System.out.print("after remove(3): ");

list.remove(3);

list.display();

list.clear();

System.out.print("after clear, length= " + list.getLength());

  

}

}