Define a class named OrderedListArray that extends the IntegerListArray class (a
ID: 3638982 • Letter: D
Question
Define a class named OrderedListArray that extends the IntegerListArray class (attached). Override the add(Integer elt) method so that it will add the new element in the correct numerical order. Also, for any method that will cause the list not to be in the correct order, override that method with a method that simply prints a message saying that the method is not implemented.
Test your class by instantiating a new OrderedListArray and adding 25 random numbers less than 100 ((int)(Math.random()*100)) and then printing the list.
The code must be completed how i asked to get full points. The code must include a comment for each method describing the method, a comment for each parameter describing what the parameter represents, and a comment for each variable declaration (including instance variables) describing what the variable represents.
Here is the IntegerListArray class:
public class IntegerListArray{
protected Integer[] theArray;
protected int listSize;
public IntegerListArray() {
theArray = new Integer[5];
listSize=0;
}
public void ensureCapacity(int requestedSize) {
if (requestedSize>=theArray.length) {
Integer[] newArray = new Integer[theArray.length*2];
System.arraycopy(theArray, 0, newArray, 0, theArray.length);
theArray = newArray;
}
}
public void add(Integer elt) {
ensureCapacity(listSize+1);
theArray[listSize++] = elt;
}
public void addFirst(Integer elt) {
addAt(elt, 0);
}
public int find(Integer elt) {
int idx = -1;
for (int i=0; i<listSize; i++) {
if (elt.equals(theArray[i]))
idx=i;
}
return idx;
}
public Integer findKth(int idx){
if (idx<0 || idx>=listSize)
return null;
return theArray[idx];
}
public void addAt(Integer elt, int idx) {
if ((idx>listSize) || (idx<0))
throw new IllegalArgumentException("Illegal insertion point " + idx);
if (idx==listSize) ensureCapacity(listSize+1);
makeRoomAt(idx);
theArray[idx] = elt;
}
public boolean isEmpty() {
return (listSize==0);
}
public void makeEmpty(){
theArray = new Integer[5];
listSize=0;
}
public void makeRoomAt(int idx) {
ensureCapacity(listSize+1);
//NB: listSize is incremented after initialization
for (int i=listSize++; i>idx; i--)
theArray[i]=theArray[i-1];
}
public void printList() {
for (int i=0; i<listSize; i++)
System.out.print(theArray[i]+ ((i+1<listSize)?", ":" "));
}
public boolean removeAt(int idx){
if (idx<0 || idx>=listSize)
return false;
fillInHoleAt(idx);
return true;
}
public void fillInHoleAt(int idx) {
for (int i = idx; i<listSize; i++)
theArray[i] = theArray[i+1];
--listSize;
}
public boolean remove(Integer idx){
int remloc = find(idx);
if (remloc == -1)
return false;
removeAt(remloc);
return true;
}
}//end of class IntegerListArray
Explanation / Answer
You should be able to do the rest with this. public class OrderedListArray extends IntegerListArray { public void add(Integer elt) { ensureCapacity(listSize+1); int newIndex = 0; if(isEmpty()) { theArray[listSize] = elt; } else { while(newIndexRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.