Question: Modify the array implementation of an orderd list class such that the array expands itself if it fills up. i.Both versions of the add() method

Modify the array implementation of an orderd list class such that the array expands itself if it fills up.

i.Both versions of the add() method need to expand the class internal array is it becomes full. The old data should be preserved after the arrays expansion. The following workarounds are used.

1.Create a newer larger array. The current length of the internal array should be used as a base for the length of this newer array. A good approach is to make the newer arrays length a constant value more than that of the older one. This constant value is chosen large enough to prevent too many resizes, yet small enough not to waste to much menory.

2.The contents of the older(smaller) array is copied cell by cell into the (newer) larger array, in order to preserve the original list data.

3.The older array is deleted, by copying the address of the newer array into the orginal refrence to the class iternal array.

Since several methods my call for the resize of the internal array,a private helper method called resize() will perform the steps above.

ii. Create a new operation called getCapacity reports the current size of the internal array.

ii. Create a new operation equals() compares two lists, and returns true if the current list contains the same items as its parameter

This is my program so far in JAVA.

public class AList implements Listinterface {

private T [] List; private int counter;

public AList() { List = (T[])new Object[5];

counter = 0;

}// end of Alist

//@parm item public void add(T item)throws ListExceptions { if( counter == List.length) {

}// end of if

if( item == null) { throw new ListExceptions("Error. Unable to add. Cannot add null entries"); }

else { List[counter] = item; counter ++;

}// end of else

}// end of add

//@Overload add method public void add(T item, int position) throws ListExceptions {

if(position <=0 || position > counter) { throw new ListExceptions("Error. Unable to insert. Bad position."); }// end of if

if(item == null) { throw new ListExceptions("Error. Unable to insert. Attempted to insert a null object"); }// end of if

if(counter == 0) { throw new ListExceptions("Error. Unable to insert. List is empty."); }// end of if

if(counter == List.length) {

}// end of if

else { for(int k = counter -1;k >= position -1;k--)//right shift to add { List[k+1]=List[k]; }// end of for loop

List[position -1] = item; counter++; }// end of else

}// end of add

//@parm position public void remove(int position)throws ListExceptions//position is in terms of 1 count { if(counter == 0)

throw new ListExceptions("Error. Unable to remove. The List is empty");

if(position <= 0 || position > counter)

throw new ListExceptions("Error. Unable to remove. Bad position");

int arrayposition = position -1;

for(int k = arrayposition +1;k<= counter -1; k++)//left Shift to delete { List[k-1]=List[k];

}// end of for

counter --;

}// end of remove

public void clear() { counter = 0; }// end of clear

public boolean isEmpty() { if(counter == 0) { return true; }// end of if

else { return false; }// end of else

}// end of isEmpty

public Object[] toArray() {

Object [] ob = List;

return ob;

}// end of toArray

//@parm position public T get(int position)throws ListExceptions { if(position <= 0 || position > counter) { throw new ListExceptions("Error. Unable to get. Bad position");

}// end of if

if(counter == 0) { throw new ListExceptions("Error unable to get. List is empty");

}// end of if

T temp = List[position-1]; return temp;

}// end of get

// @parm item, position public T set(T item, int position)throws ListExceptions { if(item == null) { throw new ListExceptions("Error. Unable to replace. Replacement cannot be null");

}// end of if

if(position <= 0 || position > counter) { throw new ListExceptions("Error. Unable to replace. Bad position");

}// end of if

if(counter == 0) { throw new ListExceptions("Error. Unable to replace. List is empty");

}// end of if

T temp = List[position-1];

List[position-1]= item;

return temp;

}// end of set

// @parm item, StartPosition, EndPosition public int find(T item, int StartPosition, int EndPosition)throws ListExceptions { int k =0;

if(StartPosition > EndPosition) { return -1;

}//end of if if(StartPosition <= 0 || EndPosition > counter ) { throw new ListExceptions("Error. Unable to find. Start and/or end position is bad.");

}// end of if

if(StartPosition == EndPosition) { k = StartPosition -1;

if(List[k].equals(item)) { return k +1;

}// end of if

}// end of if

else { for(k = StartPosition-1; k < EndPosition; k ++) { if(List[k].equals(item)) { return k +1; }// end of if

}// end of for loop

}// end of else

return -1;

}// end of find

public int size() { if(counter > 0) { return counter; } return 0; }// end of size method

public boolean equals() {

return false; }

// @Overrride toString method public String toString() { if(counter == 0) { return "The List is empty "; }// end of if

String Temp ="";

for(int k =0; k

}// end of for loop return Temp; }// end of toString

}// end of class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!