SophiaPretty

(5)

$14/per page/Negotiable

About SophiaPretty

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Algebra See all
Accounting,Algebra,Applied Sciences,Architecture and Design,Art & Design,Biology,Business & Finance,Calculus,Chemistry,Communications,Computer Science,Economics,Engineering,English,Environmental science,Essay writing Hide all
Teaching Since: Jul 2017
Last Sign in: 305 Weeks Ago, 1 Day Ago
Questions Answered: 15833
Tutorials Posted: 15827

Education

  • MBA,PHD, Juris Doctor
    Strayer,Devery,Harvard University
    Mar-1995 - Mar-2002

Experience

  • Manager Planning
    WalMart
    Mar-2001 - Feb-2009

Category > Computer Science Posted 12 Jan 2018 My Price 10.00

(T item) throws IndexOutOfBoundsException; public

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;
    }
}

 

Attachments:

Answers

(5)
Status NEW Posted 12 Jan 2018 02:01 PM My Price 10.00

-----------  ----------- 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

Not Rated(0)