PLEASE ANSWER THE JAVA QUESTION FULLY DO NOT SKIP ANY OF THE
PLEASE ANSWER THE JAVA QUESTION FULLY.. DO NOT SKIP ANY OF THE SPECIFICATIONS PLEASE:
Use Alist.java, List.java and ListTestExpand.java found below and Create AListGrow.java program with following specifications:
1) Add an expand method in the Alist.java to grow the array dynamically.
2) When the array is full, expand the array size to double of maximum size.
3) Doubling the maximum size of the old maximum size.
4) Use the ListTestExpand.java to test the program.
Alist Java File:
/** Source code example for \"A Practical Introduction to Data
 Structures and Algorithm Analysis, 3rd Edition (Java)\"
 by Clifford A. Shaffer
 Copyright 2008-2011 by Clifford A. Shaffer
 */
/** Array-based list implementation */
 class AList<E> implements List<E> {
 private static final int defaultSize = 10; // Default size
 private int maxSize; // Maximum size of list
 private int listSize; // Current # of list items
 private int curr; // Position of current element
 private E[] listArray; // Array holding list elements
/** 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<listSize-1; i++) // Shift them down
 listArray[i] = listArray[i+1];
 listSize--; // Decrement size
 return it;
 }
 public void moveToStart() { curr = 0; } // Set to front
 public void moveToEnd() { curr = listSize; } // Set at end
 public void prev() { if (curr != 0) curr--; } // Back up
 public void next() { if (curr < listSize) curr++; }
/** @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<listSize) :
 \"No current element\";
 return listArray[curr];
 }
 // Extra stuff not printed in the book.
/**
 * 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 File:
/** Source code example for \"A Practical Introduction to Data
 Structures and Algorithm Analysis, 3rd Edition (Java)\"
 by Clifford A. Shaffer
 Copyright 2008-2011 by Clifford A. Shaffer
 */
/** List ADT */
 public interface List<E> {
 /** Remove all contents from the list, so it is once again
 empty. Client is responsible for reclaiming storage
 used by the list elements. */
 public void clear();
/** 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 File:
/** Source code example for \"A Practical Introduction to Data
 Structures and Algorithm Analysis, 3rd Edition (Java)\"
 by Clifford A. Shaffer
 Copyright 2008-2011 by Clifford A. Shaffer
 */
/** A JUnit test class for lists. */
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());
 }
}
Solution
This is key methods which fullfill your requirements.......
//this method can grow the array double dynamically you need to pass old array in this methods
// 1) Add an expand method in the Alist.java to grow the array dynamically.
// 2) When the array is full, expand the array size to double of maximum size.
// 3) Doubling the maximum size of the old maximum size.
// 4) Use the ListTestExpand.java to test the program.
private static Object expendArraydouble (Object oldArray) {
int oldSize = java.lang.reflect.Array.getLength(oldArray);
int newsize=oldSize*2;
System.out.println(\"newsize===\"+newsize+\"======oldSize=======\"+oldSize);
Class elementType = oldArray.getClass().getComponentType();
Object newArray = java.lang.reflect.Array.newInstance(
elementType, newsize);
int preserveLength = Math.min(oldSize, newsize);
if (preserveLength > 0)
System.arraycopy(oldArray, 0, newArray, 0, preserveLength);
return newArray;
}
// Test this function for resizeArray().
public static void main (String[] args) {
int[] a = {1, 2, 3,7,8};
a = (int[])expendArraydouble(a);
a[3] = 4;
a[4] = 5;
for (int i=0; i<a.length; i++)
System.out.println(a[i]); }
//Full class with multiple methods
package com.list;
import java.util.Arrays;
public class AListGrow {
String[] OrigArray;
String size;
public AListGrow(int i) {
OrigArray = new String[i+1];
size=Integer.toString(i);
}
public void insert(int nextInt) {
addElement(OrigArray, Integer.toString(nextInt));
}
public void append(int nextInt) {
if(OrigArray[OrigArray.length-1]!=null)
OrigArray=(String[]) expendArraydouble(OrigArray);
//need to check array is full or not if full then call below methods
addElement(OrigArray, Integer.toString(nextInt));
// TODO Auto-generated method stub
}
public void next() {
// TODO Auto-generated method stub
}
//this method can grow the array double dynamically you need to pass old array in this methods
// 1) Add an expand method in the Alist.java to grow the array dynamically.
// 2) When the array is full, expand the array size to double of maximum size.
// 3) Doubling the maximum size of the old maximum size.
// 4) Use the ListTestExpand.java to test the program.
private static Object expendArraydouble (Object oldArray) {
int oldSize = java.lang.reflect.Array.getLength(oldArray);
int newsize=oldSize*2;
System.out.println(\"newsize===\"+newsize+\"======oldSize=======\"+oldSize);
Class elementType = oldArray.getClass().getComponentType();
Object newArray = java.lang.reflect.Array.newInstance(
elementType, newsize);
int preserveLength = Math.min(oldSize, newsize);
if (preserveLength > 0)
System.arraycopy(oldArray, 0, newArray, 0, preserveLength);
return newArray; }
// Test routine for resizeArray().
public static void main (String[] args) {
int[] a = {1, 2, 3,7,8};
a = (int[])expendArraydouble(a);
a[3] = 4;
a[4] = 5;
for (int i=0; i<a.length; i++)
System.out.println(a[i]); }
public String length() {
// TODO Auto-generated method stub
return size;
}
String[] addElement(String[] a, String e) {
System.out.println(\"adding===========\");
a = Arrays.copyOf(a, a.length + 1);
a[a.length - 1] = e;
System.out.println(\"==============\"+a);
return a;
}
}







