Question: Topic: Array List (4 points) For each task, submit the source code with detail comments. Objective: Growable ArrayList (2 points) Download Alist.java, List.java and ListTestExpand.java
Topic: Array List (4 points)
For each task, submit the source code with detail comments.
Objective: Growable ArrayList (2 points)
Download Alist.java, List.java and ListTestExpand.java from Moodle. 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.
Objective: Implementation of Java ArrayList API (2 points)
Refer to the StaffMember class hierarchy Diagram, write a main
program to perform following specifications by using
java.util.ArrayList.
Download Staff.java, StaffMember.java, Volunteer.java, Employee.java, Executive.java and Hourly.java from moodle.
Initialize an ArrayList object that reads all employee records from Staff.java.
Allow user to enter employee name to
Display employee record;including employee type and pay rate
Delete employee record
Modify employees Address field
Display error if the user attempts to remove name that is not on the list.
At the end of program, display all employee records with updated information.
/** Array-based list implementation */ class AList implements List { 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=0) && (pos<=listSize) : "Pos out of range"; curr = pos; } /** @return Current element */ public E getValue() { assert (curr>=0) && (curr. 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 ADT */ public interface List { /** 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(); }
/** 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()); } }