Question

In: Computer Science

You will implement the MyList ADT according to the following: 1. MyList must implement the List...

You will implement the MyList ADT according to the following:

1. MyList must implement the List interface. It will look something like below:

public class MyList<E> implements List<E> {

... } Note: As said in the class, when implementing an interface, you have to implement each method in it. There are a lot of methods in the List interface. We are going to address this issue below.

2. The initial capacity of MyList must be 2. This is not an ideal initial capacity. A smaller initial

capacity allows us to test out our code more quickly.

3. You must implement the following methods:

@SuppressWarnings("unchecked") public MyList() {

... } public int size() {

... } public boolean isEmpty() {

... } public boolean add(E e) {

... } public void add(int index, E element) {

... } public E get(int index) {

... } public E set(int index, E element) {

... } public E remove(int index) {

... } You can choose to implement any other methods as you need – private or public. For any method you don’t need but is required by the List interface, you can stub them out like below:

public void clear() {

StackTraceElement[] stackTrace =

new Throwable().getStackTrace(); String methodName = stackTrace[0].getMethodName(); throw new UnsupportedOperationException("Method '"

+ methodName + "' not implemented!"); }

4. There is at least one place where you will encounter an avoidable “unchecked” compiler warning. To get a clean compilation, use: @SuppressWarnings("unchecked").

But don’t abuse it for any other places where a warning is avoidable but due to your questionable coding skills.

Solutions

Expert Solution

Here is the completed code for this problem including a Test class for basic testing. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// MyList.java

import java.util.Collection;

import java.util.Iterator;

import java.util.List;

import java.util.ListIterator;

public class MyList<E> implements List<E> {

      // underlying array

      private E array[];

      // number of elements

      private int size;

      // initial size

      private static int INITIAL_CAPACITY = 2;

      // constructor initializing MyList object using initial capacity

      @SuppressWarnings("unchecked")

      public MyList() {

            array = (E[]) new Object[INITIAL_CAPACITY];

            size = 0;

      }

      // helper method to resize the array when array is full

      @SuppressWarnings("unchecked")

      private void resize(int newSize) {

            // creating array of new size

            E newArr[] = (E[]) new Object[newSize];

            // copying elements

            for (int i = 0; i < size && i < newSize; i++) {

                  newArr[i] = array[i];

            }

            // replacing old with new array

            array = newArr;

            // updating size if the array is smaller than size (in case of

            // downsizing (in future))

            if (array.length < size) {

                  size = array.length;

            }

      }

      @Override

      public boolean add(E element) {

            // resizing array to double capacity if full

            if (size == array.length) {

                  resize(array.length * 2);

            }

            // adding to end

            array[size] = element;

            size++;

            return true;

      }

      @Override

      public void add(int index, E element) {

            // throwing exception if index is invalid

            if (index < 0 || index > size) {

                  throw new IndexOutOfBoundsException("Invalid index");

            }

            // resizing array if necessary

            if (size == array.length) {

                  resize(array.length * 2);

            }

            // shifting elements starting from index to one place right

            for (int i = size; i > index; i--) {

                  array[i] = array[i - 1];

            }

            // adding element to index, and updating size

            array[index] = element;

            size++;

      }

      // returns a String containing list elements

      public String toString() {

            String data = "[";

            for (int i = 0; i < size; i++) {

                  data += array[i];

                  if (i != size - 1) {

                        data += ", ";

                  }

            }

            data += "]";

            return data;

      }

      @Override

      public E get(int index) {

            // throwing exception if index is invalid

            if (index < 0 || index >= size) {

                  throw new IndexOutOfBoundsException("Invalid index");

            }

            return array[index];

      }

      @Override

      public boolean isEmpty() {

            return size == 0;

      }

      @Override

      public E remove(int index) {

            // throwing exception if index is invalid

            if (index < 0 || index > size) {

                  throw new IndexOutOfBoundsException("Invalid index");

            }

            // storing element to remove

            E element = array[index];

            // shifting all elements starting from index+1 position, to one place left

            for (int i = index; i < size - 1; i++) {

                  array[i] = array[i + 1];

            }

            //updating size, returning removed element

            size--;

            return element;

      }

      @Override

      public int size() {

            return size;

      }

      @Override

      public E set(int index, E element) {

            // throwing exception if index is invalid

            if (index < 0 || index > size) {

                  throw new IndexOutOfBoundsException("Invalid index");

            }

            //storing old value

            E oldValue = array[index];

            //replacing with new value

            array[index] = element;

            //returned old value

            return oldValue;

      }

      // below methods are stubbed out since not required

      /**

      * NOT implemented

      */

      @Override

      public boolean addAll(Collection<? extends E> arg0) {

            StackTraceElement[] stackTrace = new Throwable().getStackTrace();

            String methodName = stackTrace[0].getMethodName();

            throw new UnsupportedOperationException("Method '" + methodName

                        + "' not implemented!");

      }

      /**

      * NOT implemented

      */

      @Override

      public boolean addAll(int arg0, Collection<? extends E> arg1) {

            StackTraceElement[] stackTrace = new Throwable().getStackTrace();

            String methodName = stackTrace[0].getMethodName();

            throw new UnsupportedOperationException("Method '" + methodName

                        + "' not implemented!");

      }

      /**

      * NOT implemented

      */

      @Override

      public void clear() {

            StackTraceElement[] stackTrace = new Throwable().getStackTrace();

            String methodName = stackTrace[0].getMethodName();

            throw new UnsupportedOperationException("Method '" + methodName

                        + "' not implemented!");

      }

      /**

      * NOT implemented

      */

      @Override

      public boolean contains(Object arg0) {

            StackTraceElement[] stackTrace = new Throwable().getStackTrace();

            String methodName = stackTrace[0].getMethodName();

            throw new UnsupportedOperationException("Method '" + methodName

                        + "' not implemented!");

      }

      /**

      * NOT implemented

      */

      @Override

      public boolean containsAll(Collection<?> arg0) {

            StackTraceElement[] stackTrace = new Throwable().getStackTrace();

            String methodName = stackTrace[0].getMethodName();

            throw new UnsupportedOperationException("Method '" + methodName

                        + "' not implemented!");

      }

      /**

      * NOT implemented

      */

      @Override

      public int indexOf(Object arg0) {

            StackTraceElement[] stackTrace = new Throwable().getStackTrace();

            String methodName = stackTrace[0].getMethodName();

            throw new UnsupportedOperationException("Method '" + methodName

                        + "' not implemented!");

      }

      /**

      * NOT implemented

      */

      @Override

      public Iterator<E> iterator() {

            StackTraceElement[] stackTrace = new Throwable().getStackTrace();

            String methodName = stackTrace[0].getMethodName();

            throw new UnsupportedOperationException("Method '" + methodName

                        + "' not implemented!");

      }

      /**

      * NOT implemented

      */

      @Override

      public int lastIndexOf(Object arg0) {

            StackTraceElement[] stackTrace = new Throwable().getStackTrace();

            String methodName = stackTrace[0].getMethodName();

            throw new UnsupportedOperationException("Method '" + methodName

                        + "' not implemented!");

      }

      /**

      * NOT implemented

      */

      @Override

      public ListIterator<E> listIterator() {

            StackTraceElement[] stackTrace = new Throwable().getStackTrace();

            String methodName = stackTrace[0].getMethodName();

            throw new UnsupportedOperationException("Method '" + methodName

                        + "' not implemented!");

      }

      /**

      * NOT implemented

      */

      @Override

      public ListIterator<E> listIterator(int arg0) {

            StackTraceElement[] stackTrace = new Throwable().getStackTrace();

            String methodName = stackTrace[0].getMethodName();

            throw new UnsupportedOperationException("Method '" + methodName

                        + "' not implemented!");

      }

      /**

      * NOT implemented

      */

      @Override

      public boolean remove(Object arg0) {

            StackTraceElement[] stackTrace = new Throwable().getStackTrace();

            String methodName = stackTrace[0].getMethodName();

            throw new UnsupportedOperationException("Method '" + methodName

                        + "' not implemented!");

      }

      /**

      * NOT implemented

      */

      @Override

      public boolean removeAll(Collection<?> arg0) {

            StackTraceElement[] stackTrace = new Throwable().getStackTrace();

            String methodName = stackTrace[0].getMethodName();

            throw new UnsupportedOperationException("Method '" + methodName

                        + "' not implemented!");

      }

      /**

      * NOT implemented

      */

      @Override

      public boolean retainAll(Collection<?> arg0) {

            StackTraceElement[] stackTrace = new Throwable().getStackTrace();

            String methodName = stackTrace[0].getMethodName();

            throw new UnsupportedOperationException("Method '" + methodName

                        + "' not implemented!");

      }

      /**

      * NOT implemented

      */

      @Override

      public List<E> subList(int arg0, int arg1) {

            StackTraceElement[] stackTrace = new Throwable().getStackTrace();

            String methodName = stackTrace[0].getMethodName();

            throw new UnsupportedOperationException("Method '" + methodName

                        + "' not implemented!");

      }

      /**

      * NOT implemented

      */

      @Override

      public Object[] toArray() {

            StackTraceElement[] stackTrace = new Throwable().getStackTrace();

            String methodName = stackTrace[0].getMethodName();

            throw new UnsupportedOperationException("Method '" + methodName

                        + "' not implemented!");

      }

      /**

      * NOT implemented

      */

      @Override

      public <T> T[] toArray(T[] arg0) {

            StackTraceElement[] stackTrace = new Throwable().getStackTrace();

            String methodName = stackTrace[0].getMethodName();

            throw new UnsupportedOperationException("Method '" + methodName

                        + "' not implemented!");

      }

}

//Test.java

public class Test {

      public static void main(String[] args) {

            // creating a MyList object, adding some numbers and testing various

            // methods

            MyList<Integer> list = new MyList<Integer>();

            for (int i = 0; i < 10; i++) {

                  list.add(i);

            }

            System.out.println(list);

            System.out.println("Size: " + list.size());

            list.add(5, 999);

            System.out.println(list);

            System.out.println("get(5): " + list.get(5));

            System.out.println("remove(5): " + list.remove(5));

            System.out.println(list);

            System.out.println("set(0, 1234)");

            list.set(0, 1234);

            System.out.println(list);

      }

}

/*OUTPUT*/

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Size: 10

[0, 1, 2, 3, 4, 999, 5, 6, 7, 8, 9]

get(5): 999

remove(5): 999

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

set(0, 1234)

[1234, 1, 2, 3, 4, 5, 6, 7, 8, 9]


Related Solutions

1. Implement the graph ADT using the adjacency list structure. 2. Implement the graph ADT using...
1. Implement the graph ADT using the adjacency list structure. 2. Implement the graph ADT using the adjacency matrix structure. LANGUAGE IS IN JAVA Comment for any questions Data structures and algorithms
Overview: implement the ADT List in Java. This program is meant to the ADT List from...
Overview: implement the ADT List in Java. This program is meant to the ADT List from the ground up In the lecture, we learned how to implement an ADT like the ArrayList you have used in Project 1. With this project, you have the chance to implement an ADT called MyList, which is a simplified replacement for the full-blown ArrayList. Requirements You will implement the MyList ADT according to the following: 1. MyList must implement the List interface. It will...
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
26.5 Project 3: List & Queue ADT Overview For this project, you are to implement two...
26.5 Project 3: List & Queue ADT Overview For this project, you are to implement two abstract data types (ADTs). You will write a doubly linked list (Dll) and a queue class. The queue will use the dll internally. The class interfaces are downloadable below. You must follow the interface exactly. While you can define other public and private methods and fields, the class names and methods given must appear as provided, or you will not pass the unit tests....
Implement the ADT character string as the class LinkedString by using a linked list of characters....
Implement the ADT character string as the class LinkedString by using a linked list of characters. Include the following LinkedString constructors and methods: LinkedString(char[] value) Allocates a new character linked list so that it represents the sequence of characters currently contained in the character array argument. LinkedString(String original) Initializes a new character linked list so that it represents the same sequence of characters as the argument. char charAt(int index) Returns the char value at the specified index. The first character...
In C++, Design and implement an ADT that represents a triangle. The data for the ADT...
In C++, Design and implement an ADT that represents a triangle. The data for the ADT should include the three sides of the triangle but could also include the triangle’s three angles. This data should be in the private section of the class that implements the ADT. Include at least two initialization operations: one that provides default values for the ADT’s data, and another that sets this data to client-supplied values. These operations are the class’s constructors. The ADT also...
Implement a Bag ADT using Dynamic Array structure as underlying data storage for Bag ADT. RESTRICTIONS:...
Implement a Bag ADT using Dynamic Array structure as underlying data storage for Bag ADT. RESTRICTIONS: Not allowed to use ANY built-in Python data structures and their methods. You must solve by importing the DynamicArray class and using class methods to write solution. Also not allowed to directly access any variables of the DynamicArray class (like self.size, self.capacity and self.data in part 1). All work must be done by only using class methods. Below is the Bag ADT starter code...
3.1 Implement the stack ADT using array (4 marks) 3.1.1 Implement the pop() operation in the...
3.1 Implement the stack ADT using array 3.1.1 Implement the pop() operation in the stack (1 mark) Implement a stack class named Stack2540Array using array. The starter code is as follows. The instance variables and most operations are provided. You need to implement the pop operation. Make sure that your program checks whether the stack is empty in the pop operation. import java . io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int...
C++ problem - Implement and add the advanced functionalities to the ADT of the BST made...
C++ problem - Implement and add the advanced functionalities to the ADT of the BST made with the fundamental functionalities: Visit - Description: It will display each of the data stored in the BST depending on the input parameter:Preorder Inorder Bidder Level by level Input - An integer (1-4) Exit - Nothing Precondition - A valid BST Postcondition - Nothing Height - Description:Will return the height of the BST Entry - Nothing Output - An integer with which to indicate...
User ADT: Describes and manipulates user information. You must track the following information about a user / provide the following methods:
• User ADT: Describes and manipulates user information. You must track the following information about a user / provide the following methods:o usernameo firstNameo lastNameo a list of the 10 most recently purchased itemso A user can bid on and purchase ItemsThe User class should have a default constructor, as well as one accepting all parameters. It should also provide accessor (getter) and mutator (setter) methods for appropriate methods. By default, a user is able to buy products only (not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT