Question
Download Alist.java, List.java and ListTestExpand.java. Create AListGrow.java program with following specifications: Add a method in the Alist.java to grow the array dynamically or create subclass
Download Alist.java, List.java and ListTestExpand.java. Create AListGrow.java program with following specifications:
Add a method in the Alist.java to grow the array dynamically or create subclass to extend Alist.java with the dynamic array growth method. When the array is full, expand the array size to double of maximum size. Doubling the maximum size of the old maximum size. Use the ListTestExpand.java to test the program.
AList.java :
/** Array-based list implementation */ class AList
/** Constructors */ /** Create a list with the default capacity. */ AList() { this(defaultSize); } /** Create a new list object. @param size Max # of elements list can contain. */ @SuppressWarnings("unchecked") // Generic array allocation AList(int size) { maxSize = size; listSize = curr = 0; listArray = (E[])new Object[size]; // Create listArray }
public void clear() // Reinitialize the list { listSize = curr = 0; } // Simply reinitialize values
/** Insert "it" at current position */ public void insert(E it) { assert listSize < maxSize : "List capacity exceeded"; for (int i=listSize; i>curr; i--) // Shift elements up listArray[i] = listArray[i-1]; // to make room listArray[curr] = it; listSize++; // Increment list size }
/** Append "it" to list */ public void append(E it) { assert listSize < maxSize : "List capacity exceeded"; listArray[listSize++] = it; }
/** Remove and return the current element */ public E remove() { if ((curr<0) || (curr>=listSize)) // No current element return null; E it = listArray[curr]; // Copy the element for(int i=curr; i /** @return List size */ public int length() { return listSize; } /** @return Current position */ public int currPos() { return curr; } /** Set current list position to "pos" */ public void moveToPos(int pos) { assert (pos>=0) && (pos<=listSize) : "Pos out of range"; curr = pos; } /** @return Current element */ public E getValue() { assert (curr>=0) && (curr /** * Generate a human-readable representation of this list's contents * that looks something like this: < 1 2 3 | 4 5 6 >. The vertical * bar represents the current location of the fence. This method * uses toString() on the individual elements. * @return The string representation of this list */ public String toString() { // Save the current position of the list int oldPos = currPos(); int length = length(); StringBuffer out = new StringBuffer((length() + 1) * 4); moveToStart(); out.append("< "); for (int i = 0; i < oldPos; i++) { out.append(getValue()); out.append(" "); next(); } out.append("| "); for (int i = oldPos; i < length; i++) { out.append(getValue()); out.append(" "); next(); } out.append(">"); moveToPos(oldPos); // Reset the fence to its original position return out.toString(); } } List.java : /** List ADT */ public interface List /** Insert an element at the current location. The client must ensure that the list's capacity is not exceeded. @param item The element to be inserted. */ public void insert(E item); /** Append an element at the end of the list. The client must ensure that the list's capacity is not exceeded. @param item The element to be appended. */ public void append(E item); /** Remove and return the current element. @return The element that was removed. */ public E remove(); /** Set the current position to the start of the list */ public void moveToStart(); /** Set the current position to the end of the list */ public void moveToEnd(); /** Move the current position one step left. No change if already at beginning. */ public void prev(); /** Move the current position one step right. No change if already at end. */ public void next(); /** @return The number of elements in the list. */ public int length(); /** @return The position of the current element. */ public int currPos(); /** Set current position. @param pos The position to make current. */ public void moveToPos(int pos); /** @return The current element. */ public E getValue(); } ListTestExpand.java : import java.io.*; import java.util.*; public class ListTestExpand { public static void main(String args[]) { AListGrow arr = new AListGrow(10); Random gen = new Random(); for (int i= 0; i<10; i++) arr.insert(gen.nextInt(100)); System.out.println(" Before initial size is exceeded: "+arr.length()); System.out.println(arr.toString()); for (int i= 0; i<10; i++) arr.append(gen.nextInt(100)); System.out.println(" After expanding of array and before set: "+arr.length()); System.out.println(arr.toString()); for (int i= 0; i<40; i++) arr.append(gen.nextInt(100)); System.out.println(" After expanding of array and before set: "+arr.length()); System.out.println(arr.toString()); arr.next(); arr.insert(12345); System.out.println(" After set: "); System.out.println(arr.toString()); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started