Question

In: Computer Science

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 look something like below:

public class MyList implements List {

... } 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!"); }

What each method will do:

void add(E item) add item to the end of the List
void add(int pos, E item) add item at position pos in the List, moving the items originally in positions pos through size()-1 one place to the right to make room (error if pos is less than 0 or greater than size())
int size() return the number of items in the List
boolean isEmpty() return true iff the List is empty
E get(int pos) return the item at position pos in the List (error if pos is less than 0 or greater than or equal to size())
E remove(int pos) remove and return the item at position pos in the List, moving the items originally in positions pos+1 through size()-1 one place to the left to fill in the gap (error if pos is less than 0 or greater than or equal to size())

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


MyList implemented as per the specifications in question is given below.
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class MyList<E> implements List<E> {
   private E[] data;
   private int size, capacity;
   @SuppressWarnings("unchecked")
   public MyList() {
       capacity = 2;
       size = 0;
       data = (E[]) new Object[capacity];
   }
   public int size() {
       return size;
   }

   public boolean isEmpty() {
       return size == 0;
   }

   @SuppressWarnings("unchecked")
   //helper method which will double the array capacity when it is full, so more elements can be added
   private void ensureSpace() {
       if(size() == capacity) {
           capacity *= 2; //double the capacity
           E[] temp = (E[])new Object[capacity];
           for(int i = 0; i < size; i++) {
               temp[i] = data[i];
           }
           data = temp;
       }
   }
   public boolean add(E e) {
       ensureSpace();
       data[size] = e;
       size++;
       return true;
     
   }
  
   public void add(int index, E element) {
       if(index < 0 || index > size())
           throw new IndexOutOfBoundsException();
       ensureSpace();
       //shift the elements
       for(int i = size()-1; i >= index; i--)
           data[i+1] = data[i];
       data[index] = element;
       size++;
   }

   public E get(int index) {
       if(index < 0 || index >= size())
           throw new IndexOutOfBoundsException();

       return data[index];
   }


   public E set(int index, E element) {
       if(index < 0 || index >= size())
           throw new IndexOutOfBoundsException();
       E oldVal = data[index];
       data[index] = element;
       return oldVal;
   }


  
   public E remove(int index) {
       if(index < 0 || index >= size())
           throw new IndexOutOfBoundsException();
       E val = data[index];
       for(int i = index + 1; i < size(); i++)
           data[i-1] = data[i];
       size--;
       return val;      
   }

   public boolean remove(Object o) {
       StackTraceElement[] stackTrace =
               new Throwable().getStackTrace();
       String methodName = stackTrace[0].getMethodName();
       throw new UnsupportedOperationException("Method '" + methodName + "' not implemented!");

   }


   public boolean contains(Object o) {
       StackTraceElement[] stackTrace =
               new Throwable().getStackTrace();
       String methodName = stackTrace[0].getMethodName();
       throw new UnsupportedOperationException("Method '" + methodName + "' not implemented!");
   }


   public Iterator<E> iterator() {
       StackTraceElement[] stackTrace =
               new Throwable().getStackTrace();
       String methodName = stackTrace[0].getMethodName();
       throw new UnsupportedOperationException("Method '" + methodName + "' not implemented!");

   }


   public Object[] toArray() {
       StackTraceElement[] stackTrace =
               new Throwable().getStackTrace();
       String methodName = stackTrace[0].getMethodName();
       throw new UnsupportedOperationException("Method '" + methodName + "' not implemented!");

   }


   public <T> T[] toArray(T[] a) {
       StackTraceElement[] stackTrace =
               new Throwable().getStackTrace();
       String methodName = stackTrace[0].getMethodName();
       throw new UnsupportedOperationException("Method '" + methodName + "' not implemented!");

   }

   public boolean containsAll(Collection<?> c) {
       StackTraceElement[] stackTrace =
               new Throwable().getStackTrace();
       String methodName = stackTrace[0].getMethodName();
       throw new UnsupportedOperationException("Method '" + methodName + "' not implemented!");

   }


   public boolean addAll(Collection<? extends E> c) {
       StackTraceElement[] stackTrace =
               new Throwable().getStackTrace();
       String methodName = stackTrace[0].getMethodName();
       throw new UnsupportedOperationException("Method '" + methodName + "' not implemented!");

   }


   public boolean addAll(int index, Collection<? extends E> c) {
       StackTraceElement[] stackTrace =
               new Throwable().getStackTrace();
       String methodName = stackTrace[0].getMethodName();
       throw new UnsupportedOperationException("Method '" + methodName + "' not implemented!");

   }


   public boolean removeAll(Collection<?> c) {
       StackTraceElement[] stackTrace =
               new Throwable().getStackTrace();
       String methodName = stackTrace[0].getMethodName();
       throw new UnsupportedOperationException("Method '" + methodName + "' not implemented!");

   }


   public boolean retainAll(Collection<?> c) {
       StackTraceElement[] stackTrace =
               new Throwable().getStackTrace();
       String methodName = stackTrace[0].getMethodName();
       throw new UnsupportedOperationException("Method '" + methodName + "' not implemented!");

   }


   public void clear() {
       StackTraceElement[] stackTrace =
               new Throwable().getStackTrace();
       String methodName = stackTrace[0].getMethodName();
       throw new UnsupportedOperationException("Method '" + methodName + "' not implemented!");

   }

   public int indexOf(Object o) {
       StackTraceElement[] stackTrace =
               new Throwable().getStackTrace();
       String methodName = stackTrace[0].getMethodName();
       throw new UnsupportedOperationException("Method '" + methodName + "' not implemented!");

   }


   public int lastIndexOf(Object o) {
       StackTraceElement[] stackTrace =
               new Throwable().getStackTrace();
       String methodName = stackTrace[0].getMethodName();
       throw new UnsupportedOperationException("Method '" + methodName + "' not implemented!");

   }


   public ListIterator<E> listIterator() {
       StackTraceElement[] stackTrace =
               new Throwable().getStackTrace();
       String methodName = stackTrace[0].getMethodName();
       throw new UnsupportedOperationException("Method '" + methodName + "' not implemented!");

   }


   public ListIterator<E> listIterator(int index) {
       StackTraceElement[] stackTrace =
               new Throwable().getStackTrace();
       String methodName = stackTrace[0].getMethodName();
       throw new UnsupportedOperationException("Method '" + methodName + "' not implemented!");

   }


   public List<E> subList(int fromIndex, int toIndex) {
       StackTraceElement[] stackTrace =
               new Throwable().getStackTrace();
       String methodName = stackTrace[0].getMethodName();
       throw new UnsupportedOperationException("Method '" + methodName + "' not implemented!");
   }
}


Related Solutions

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....
Write a Java program to implement a Single Linked List that will take inputs from a...
Write a Java program to implement a Single Linked List that will take inputs from a user as Student Names. First, add Brian and Larry to the newly created linked list and print the output Add "Kathy" to index 1 of the linked list and print output Now add "Chris" to the start of the list and "Briana" to the end of the list using built-in Java functions. Print the output of the linked list.
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
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Exercise Overview Implement a Java program that creates math flashcards for elementary grade students. User will...
Exercise Overview Implement a Java program that creates math flashcards for elementary grade students. User will enter his/her name, the type (+, -, *, /), the range of the factors to be used in the problems, and the number of problems to work. The system will provide problems, evaluate user responses to the problems, score the problems, and provide statistics about the session at the end. Functional Requirements User enters name at the beginning of a session. System covers four...
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...
Java The List ADT has an interface and a linked list implementation whose source code is...
Java The List ADT has an interface and a linked list implementation whose source code is given at the bottom of this programming lab description. You are to modify the List ADT's source code by adding the method corresponding to the following UML: +hasRepeats() : boolean hasRepeats() returns true if the list has a value that occurs more than once in the list hasRepeats() returns false if no value in the list occurs more than once in the list For...
Java The List ADT has an interface and a linked list implementation whose source code is...
Java The List ADT has an interface and a linked list implementation whose source code is given at the bottom of this programming lab description. You are to modify the List ADT's source code by adding the method corresponding to the following UML: +hasRepeats() : boolean hasRepeats() returns true if the list has a value that occurs more than once in the list hasRepeats() returns false if no value in the list occurs more than once in the list For...
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...
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT