Finish the implementation of SimpleBoundedList so that it satisifies the specifi
ID: 3823399 • Letter: F
Question
Finish the implementation of SimpleBoundedList so that it satisifies the specification
public class SimpleBoundedList<K extends Comparable<K>,V> implements List<K, V> {
protected Object[] values;
private int start = 0;
private int nextEmpty = 0;
/**
*
*/
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<K,V>(key,value);
return modify;
}
@Override
public V remove(K key) {
// TODO Auto-generated method stub
return null;
}
@SuppressWarnings("unchecked")
@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;
}
private class Entry<K, V> {
protected K key;
protected V value;
public Entry(K key, V value) {
this.key = key;
this.value = value;
}
}
}
Explanation / Answer
Hi,
Please see below the implementation of SimpleBoundedList.
Thanks.
SimpleBoundedList.java
public class SimpleBoundedList<K extends Comparable<K>,V> implements List<K, V> {
protected Object[] values;
private int start = 0;
private int nextEmpty = 0;
/**
*
*/
public SimpleBoundedList(int bound) {
values = new Object[bound];
}
/**
* To add and antry with a key and value
* @param key
* @param value
* @return
*/
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<K,V>(key,value);
return modify;
}
/**
* to remove an entry with a key
* @param key
* @return
*/
public V remove(K key) {
V deletedV = null;
for (int i = 0; i < values.length; i++) {
if (((Entry<K, V>) values[i]).key.compareTo(key) == 0) {
if (values[start] == values[i]
&& values[start] != values[nextEmpty]) {
start = (start + 1) % values.length;
} else if (values[nextEmpty] == values[i]
&& values[start] != values[nextEmpty]) {
nextEmpty = (nextEmpty - 1) % values.length;
}
deletedV = ((Entry<K,V>) values[i]).value;
values[i] = null;
}
shiftList();
}
return deletedV;
}
/**
* To shift
*/
private void shiftList() {
Object[] tempList = new Object[values.length];
for(int i = 0; i < values.length; i++) {
if(tempList[i] != null) {
tempList[i] = values[i];
}else {
values[i] = values[(i) % values.length];
}
}
}
/**
* To lookup for an entry with a key
* @param key
* @return
*/
public V lookup(K key) {
V value = null;
for (int i = 0; i < values.length; i++) {
if (((Entry<K, V>) values[i]).key.compareTo(key) == 0) {
value = ((Entry<K,V>) values[i]).value;
}
}
return value;
}
/**
* To remove an entry at index
* @param n
* @return
*/
public V remove(int n) {
V deletedV = (V) values[n];
values[(start + n) % values.length] = null;
if (values[start] == null && start != nextEmpty) {
start = (start + 1) % values.length;
} else if (values[nextEmpty] == null && start != nextEmpty) {
nextEmpty = (nextEmpty - 1) % values.length;
}
shiftList();
return deletedV;
}
/**
* To get the list size
* @return
*/
public int size() {
return Math.abs(start - nextEmpty) + 1;
}
/**
* To get an entry at index n
* @param n
* @return
*/
public V get(int n) {
return ((Entry<K, V>) values[(start + n) % values.length]).value;
}
/**
* To remove na entry fromthe list
* @return
*/
public V remove() {
V deletedV = (V) values[(start) % values.length];
values[(start) % values.length] = null;
if(start != nextEmpty) {
start = (start + 1) % values.length;
}
shiftList();
return deletedV;
}
/**
* Class Entry
* @author
*
* @param <K>
* @param <V>
*/
private class Entry<K, V> {
protected K key;
protected V value;
public Entry(K key, V value) {
this.key = key;
this.value = value;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.