The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 305 Weeks Ago, 1 Day Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
This is a question about java interface and inheritance. Please see if you can help out. Thank you.

.............................................................................................................
public interface List<T> {
   public void add(T item) throws IndexOutOfBoundsException;
   public T get(int i) throws IndexOutOfBoundsException;
   public T remove(int i);
   public int size();
Â
   default void println() {
       System.out.print("[");
       for (int i = 0; i < size()-1; i++) {
           System.out.print(get(i) + ",");
       }
       if (size() > 0) {
           System.out.println(get(size()-1) + "]");
       }
   }
}
Â
.............................................................................................................
public class LinkedList<T> implements List<T> {
Â
   private class Node<T> {
       public T value;
       public Node<T> next;
   }
Â
   private Node<T> head;
Â
   public void add(T item) throws IndexOutOfBoundsException {
       Node<T> curr = head;
       if (curr == null) {
           head = new Node<>();
           head.value = item;
           head.next = null;
       } else {
           while (curr.next != null) {
               curr = curr.next;
           }
Â
           curr.next = new Node<>();
           curr.next.value = item;
           curr.next.next = null;
       }
   }
Â
   public T get(int i) throws IndexOutOfBoundsException {
       if (i < 0 && i >= size()) throw new IndexOutOfBoundsException("Element doesn't exist at location " + i);
Â
       Node<T> curr = head;
       while (i > 0) {
           curr = curr.next;
           i--;
       }
       return curr.value;
   }
Â
   public T remove(int i) {
       if (i < 0 && i >= size()) throw new IndexOutOfBoundsException("Element doesn't exist at location " + i);
Â
       // Implement
   }
Â
   public int size() {
       int size = 0;
       Node<T> curr = head;
       while (curr != null) {
           curr = curr.next;
           size++;
       }
       return size;
   }
}
...........................................................................................
public class Main {
Â
   public static void testList(List<String> list) {
       list.add("a");
       list.add("b");
       list.add("c");
       list.add("d");
Â
       list.println(); // Should print [a,b,c,d]
Â
       System.out.println(list.size()); // Should print 4
Â
       String second = list.get(2);
Â
       System.out.println(second); // Should print c
Â
       list.add("e");
       list.add("f");
       list.add("g");
       list.add("h");
       list.add("i");
       list.add("j");
Â
       list.println(); // Should print [a,b,c,d,e,f,g,h,i,j]
Â
       try {
           list.add("k"); // Should throw an error for DumbList but work for LinkedList
       } catch (IndexOutOfBoundsException e) {
           System.out.println(e.getMessage()); // Should print You cannot add any more item. Only 10 allowed. for DumbLink
       }
Â
       list.remove(0);
Â
       list.println(); // Should print [b,c,d,e,f,g,h,i,j]
   }
Â
   public static void main(String[] args) {
       List<String> list = new DumbList<>();
       //List<String> list = new LinkedList<>();
Â
       testList(list);
   }
}
..................................................................................................
class FastLinkedList<T> extends LinkedList<T> {
Â
   private int size = 0;
Â
   @Override
   public void add(T item) throws IndexOutOfBoundsException {
       //Implement
   }
Â
   @Override
   public T remove(int i) {
       //Implement
   }
Â
   @Override
   public int size() {
       return size;
   }
Â
   public void prettyPrint() {
       //Implement
   }
Â
}
.......................................................................
public class DumbList<T> implements List<T> {
Â
   private final int MAX_SIZE = 10;
Â
   private Object[] array = new Object[MAX_SIZE];
Â
   private int size = 0;
Â
   public void add(T item) throws IndexOutOfBoundsException {
      if (size + 1 > MAX_SIZE) {
          throw new IndexOutOfBoundsException("You cannot add any more item. Only " + MAX_SIZE + " allowed.");
      } else {
          // Add implementation goes here
      }
   }
Â
   @SuppressWarnings("unchecked")
   public T get(int i) throws IndexOutOfBoundsException {
       return (T) array[i];
   }
Â
   public T remove(int i) {
       if (i < 0 && i >= size) {
           throw new IndexOutOfBoundsException("Invalid index, plesae supply a number between 0 and " + this.size);
       } else {
           @SuppressWarnings("unchecked")
           T toBeRemoved = (T) array[i];
Â
           // Removal Implementation goes here
Â
           return toBeRemoved;
       }
   }
Â
   public int size() {
       return size;
   }
}
Â
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly