Question: Complete the implementation of the methods in the SimpleBoundedList class. V remove(K key), V remove(int n), V lookup(K key), V remove(), size(), V get(int n),
Complete the implementation of the methods in the SimpleBoundedList class.
V remove(K key), V remove(int n), V lookup(K key), V remove(), size(), V get(int n), and Object[] toArray()
SimpleBoundedList.java
public class SimpleBoundedList implements List {
private class Entry {
protected K key; protected V value;
public Entry(K key, V value) { this.key = key; this.value = value; } } protected Object[] values;
private int start = 0; private int nextEmpty = 0;
/** * */ @SuppressWarnings("unchecked") public SimpleBoundedList(int bound) { values = new Object[bound]; }
@Override public boolean add(K key, V value) { boolean modify = false; int nextIndex = nextEmpty;
if (((nextEmpty + 1) % values.length) != start) { nextEmpty = (nextEmpty + 1) % values.length; modify = true; } else if (values[nextEmpty] == null) { modify = true; }
if (modify) values[nextIndex] = new Entry(key, value);
return modify; }
@Override public V remove(K key) { // TODO Auto-generated method stub return null; }
@Override public V lookup(K key) { return null; }
@Override public V remove(int n) { // TODO Auto-generated method stub return null; }
@Override public int size() { return 0; }
@Override public V get(int n) { return null; }
@Override public V remove() { // TODO Auto-generated method stub return null; }
@Override public Object[] toArray() { // TODO Auto-generated method stub return null; }
@Override public String toString() { StringBuilder sb = new StringBuilder();
for(Object en: values) { Entry entry = (Entry) en; sb.append(" Name : " + entry.key + " ID : " + entry.value); } return sb.toString(); } }
List.java
public interface List { //abstract methods
public abstract boolean add(K key,V value);
public abstract V remove(K key);
public abstract V remove(int n);
public abstract V remove();
public abstract V lookup(K key);
public abstract int size();
public abstract V get(int n);
public abstract Object[] toArray();
public abstract String toString();
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
