**Program using Java** For each task, submit the source code with detail comment
ID: 3667179 • Letter: #
Question
**Program using Java**
For each task, submit the source code with detail comments, test data and output result .
1. Use Alist.java & List.java provided, write a java main program to
a. Append integer array int [] a = {11,4,15,20 } to a Integer AList object.
b. Insert each element from int [] b = {9,3,7,22,13} before the 3rd position of above AList.
c. Print the list with its length.
d. After complete (c), allow user to enter number from console to remove number from the Alist if the number is on the AList else display “not find”. Continue the process until either the Alist is empty or terminate by user. Print the list.
e. Allow user to input more number from console and insert the number in the beginning of AList until user terminate or maximum size reached. Print the AList
Given Code.......
***********************************************************************************************
// Alist.java
/** 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 int getMaxsize()
{
return maxSize;
}
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
/** 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();
}
*********************************************************************************************************************
Thank you.
Explanation / Answer
Test.java
import java.io.*;
public class Test {
public static void main(String[] args) throws IOException {
InputStreamReader ir =new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
AList<Integer> l=new AList<Integer>(20);
//part a
int a[]={11,4,15,20};
for(int i=0;i<a.length;i++)
{
l.append(a[i]);
}
//part b
int b[]={9,3,7,22,13};
//representing the 3rd element of list
int pos=2;
for(int i=0;i<b.length;i++)
{
//moving the current position as third element of list so element will be inserted before third element
l.moveToPos(pos);
//inserted the element in the list at the current location
l.insert(b[i]);
//moving the current position after the inserted element and before the third element of original list
pos++;
}
//moving the current position to last
l.moveToEnd();
//printing the length of AList and the AList itself
System.out.println(l.length());
System.out.println(l.toString());
//Part c
while(true)
{
System.out.println("Enter the number you want to remove from the list or enter -1 to terminate");
int n=Integer.parseInt(br.readLine());
int listSize=l.length();
//if user terminates or list becomes empty i.e list size=0
if(n==-1 || listSize==0)
{
break;
}
boolean found=false;
for(int i=0;i<listSize;i++)
{
//iterating the current position from 0 to end to find if element is there in the list or not
l.moveToPos(i);
if(n==l.getValue())
{
found=true;
//removing the element from the list
l.remove();
break;
}
}
if(found==false)
{
System.out.println("not find");
}
}
l.moveToEnd();
//printing the length of AList and the AList itself
System.out.println(l.length());
System.out.println(l.toString());
//part d
while(true)
{
System.out.println("Enter the number you want to insert at the beginning of list or enter -1 to terminate");
int n=Integer.parseInt(br.readLine());
int listSize=l.length();
//if user terminates or list reaches max size
if(n==-1 || listSize==l.getMaxsize())
{
break;
}
l.moveToStart();
l.insert(n);
}
l.moveToEnd();
//printing the length of AList and the AList itself
System.out.println(l.length());
System.out.println(l.toString());
}
}
sample output:
9
< 11 4 9 3 7 22 13 15 20 | >
Enter the number you want to remove from the list or enter -1 to terminate
4
Enter the number you want to remove from the list or enter -1 to terminate
15
Enter the number you want to remove from the list or enter -1 to terminate
3
Enter the number you want to remove from the list or enter -1 to terminate
0
not find
Enter the number you want to remove from the list or enter -1 to terminate
-1
6
< 11 9 7 22 13 20 | >
Enter the number you want to insert at the beginning of list or enter -1 to terminate
1
Enter the number you want to insert at the beginning of list or enter -1 to terminate
2
Enter the number you want to insert at the beginning of list or enter -1 to terminate
3
Enter the number you want to insert at the beginning of list or enter -1 to terminate
-1
9
< 3 2 1 11 9 7 22 13 20 | >
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.