In this java code, label the code with comments to show what is going on in the
ID: 3741145 • Letter: I
Question
In this java code, label the code with comments to show what is going on in the code for an programmer to understand.
public class SortedList implements SortedListInterface {
private static final int MAX_LIST = 50;
private Object items[]; // an array of list items
private int numItems; // number of items in list
// default constructor
public SortedList() {
items = new Object[MAX_LIST];
numItems = 0;
}
public boolean isEmpty() {
return numItems == 0;
}
public int size() {
return numItems;
}
public Object get(int index) throws ListException {
if (index >= 1 && index <= size()) {
return items[index - 1];
} else { // index out of range
throw new ListException("get (index out of range): " + index);
}
}
public void removeAll() {
items = new Object[MAX_LIST];
numItems = 0;
}
// new operations: sortedAdd
public void sortedAdd(Comparable newItem) throws ListException {
if (size() == MAX_LIST)
throw new ListException("add (array is full)");
if (numItems == 0) {
items[0] = newItem;
numItems++;
return;
}
Object[] newList = new Object[MAX_LIST];
for (int i = 0; i < numItems; i++) {
if (newItem.compareTo(items[i]) < 0) {
newList[i] = newItem;
numItems++;
for (int j = i + 1; j < numItems; j++) {
newList[j] = items[i];
i++;
}
items = newList;
return;
} else {
newList[i] = items[i];
}
}
newList[numItems]=newItem;
numItems++;
items=newList;
return;
}
// new operation: sortedRemove
public void sortedRemove(Comparable anItem) throws ListException {
Object[] newList = new Object[MAX_LIST];
for (int i = 0; i < numItems; i++) {
if (anItem.compareTo(items[i])== 0) {
for (int j = i ; j < numItems; j++) {
i++;
newList[j] = items[i];
}
newList[numItems-1]=null;
numItems--;
items = newList;
return;
} else {
newList[i] = items[i];
}
}
}
// new operation: locateIndex
public int locateIndex(Comparable anItem) {
int index = -1;
for (int i = 0; i < numItems; i++) {
if (anItem.compareTo(items[i]) == 0) {
return i+1;
}
}
return index;
}
}
Explanation / Answer
public class SortedList implements SortedListInterface {
private static final int MAX_LIST = 50;
private Object items[];
private int numItems;
public SortedList() {
items = new Object[MAX_LIST];
numItems = 0;
}
public boolean isEmpty() {
return numItems == 0;
}
public int size() {
return numItems;
}
public Object get(int index) throws ListException {
if (index >= 1 && index <= size()) {
return items[index - 1];
} else { // index out of range
throw new ListException("get (index out of range): " + index);
}
}
public void removeAll() {
items = new Object[MAX_LIST];
numItems = 0;
}
// new operations: sortedAdd
public void sortedAdd(Comparable newItem) throws ListException {
if (size() == MAX_LIST)
throw new ListException("add (array is full)");
if (numItems == 0) {
items[0] = newItem;
numItems++;
return;
}
Object[] newList = new Object[MAX_LIST];
for (int i = 0; i < numItems; i++) {
if (newItem.compareTo(items[i]) < 0) {
newList[i] = newItem;
numItems++;
for (int j = i + 1; j < numItems; j++) {
newList[j] = items[i];
i++;
}
items = newList;
return;
} else {
newList[i] = items[i];
}
}
newList[numItems]=newItem;
numItems++;
items=newList;
return;
}
// new operation: sortedRemove
public void sortedRemove(Comparable anItem) throws ListException {
Object[] newList = new Object[MAX_LIST];
for (int i = 0; i < numItems; i++) {
if (anItem.compareTo(items[i])== 0) {
for (int j = i ; j < numItems; j++) {
i++;
newList[j] = items[i];
}
newList[numItems-1]=null;
numItems--;
items = newList;
return;
} else {
newList[i] = items[i];
}
}
}
// new operation: locateIndex
public int locateIndex(Comparable anItem) {
int index = -1;
for (int i = 0; i < numItems; i++) {
if (anItem.compareTo(items[i]) == 0) {
return i+1;
}
}
return index;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.