Objectives: To be able to write and implement the ArrayList Data Structure.. Doc
ID: 3873548 • Letter: O
Question
Objectives:
To be able to write and implement the ArrayList Data Structure..
Documentation:
1. Explain the purpose of the program as detail as possible - 8%.
2. Develop a solution for the problem and mention algorithms to be used -12%
3. List data structures to be used in solution. - 5%.
4. Give a description of how to use the program and expected input/output - 5%
5. Explain the purpose of each class you develop in the program. - 5%.
Programming:
1. For each method, give the pre and post conditions and invariant, if any - 10%
2. Program execution according to the requirements given 50%
3. Naming of program as required 5%
Description of Program
You are to write a program name ArrayList.java that create/build the ArrayList data Structure that exist in the java library. The class must be written to accept any type of Objects. The following must be implemented i.e. YOU must write the code (do not import them from the Java Library):
1. One default constructor that will create an ArrayList object with a default size (capacity) of 10 ------ public ArrayList();Another constructor that accepts a parameter of type int and sets the size to this parameter ---- public ArrayList(int n);
2. A method that allows you to place a value at the end of the ArrayList ---- public void add(Object x);
3. A method that allows you to place a value at a given location ---- public void add(int index, Object x);
4. A method that allows you to retrieve a value from a given location ---- public Object get(int index);
5. A method that allows you the number of elements in the the ArrayList ---- public int size();
6. A method would test to see if the ArrayList is empty ---- public boolean isEmpty();
7. A method that see if a particular object exist in the ArrayList --- public boolean isIn(Object ob);
8. A method that will return the location of first occurrence of an Object starting from location 0 ----- public int find (Object n);
9. A method that will remove the first occurrence of an Object starting from location 0 ----- public void remove (Object n);
Now, write a driver program (the class with the public static void main(String[] args) method) name testarray.java to test the ArrayList data structure you just created. That is, you must test all ten (including the default constructor) of the above methods. For those that are returning a value, print out the returned value to the screen and for those that are not returning a value, print a message that indicate if it successfully completed its task.
To test the ArrayList, you must randomly generate 15 integer numbers ranging from 1 to 25 and add to the two ArrayList you created (One using the no-parameter constructor and the other using the one-parameter constructor). Of course, to use the one-parameter constructor, you must prompt the user for an initial size of the ArrayList.
Remember --- The size function should be based upon the actual number of elements you have in the ArrayList at any given time.
Explanation / Answer
// The purpose of the program is to impliment arraylist opertion with out using build in class
import java.io.*; // use standard io statement
import java.util.*; //use it for scanner and random functions
class ArrayList // create array list program
{
int size; //size of the arrayList
int a[]; // name of the array list
public ArrayList() //default constructor
{
size=25;
a = new int[25];
}
public ArrayList(int n) // parametarised constructor
{
size=n;
a = new int[size];
}
public int getSize() // find size of the array
{
int count=-1,i;
for(i=0;i<size;i++)
if(a[i]==0)
break;
else
count++;
return(count);
}
public void add(int x) // insert at the end of the array list
{
int i=getSize();
if(i==size) // check the condition before inserting
System.out.println("Array is full");
else
{
i++;
a[i]=x;
}
}
public void add(int ind,int x) // insert at specific position
{
int i=getSize();
if((i==size)&&(ind==size)) // check the condition before inserting
System.out.println("Array is full");
else
{
a[ind]=x;
}
}
public void display() //display the content of the arraylist
{
int i;
for(i=0;i<getSize();i++)
System.out.print(" "+a[i]);
}
public void remove(int ele) //remove the first occurance of element from the array list
{
int flag=0,i;
for(i=0;i<getSize();i++)
{
if(a[i]==ele)
{
a[i]=-1;
flag=1;
break;
}
}
if(flag==0) //check specifed element found or not
System.out.println("element not found");
else
System.out.println("element not exist");
}
public void find(int ele) // find an element from the array list
{
int flag=0,i;
for(i=0;i<getSize();i++)
{
if(a[i]==ele)
{
flag=1;
break;
}
}
if(flag==0)
System.out.println("element not found");
else
System.out.println("element found at location"+i);
}
public boolean isEmpty() // array is empty or not
{
int x=getSize();
if(x==-1)
return(true);
else
return(false);
}
}
class TestArray //implimentation array list in TestArray class
{
public static void main(String arg[]) //main program
{
ArrayList arr1,arr2; //create two objects
Scanner sc= new Scanner(System.in);
System.out.println("enter size of the second array");
int val=sc.nextInt();
int n,i,x,ch;
arr1=new ArrayList();
arr2=new ArrayList(val);
for(i=0;i<15;i++) // insert elements in arraly list through random generators
{
Random rand = new Random();
n = rand.nextInt(50);
arr1.add(n);
arr2.add(n);
}
do //display the menu
{
System.out.print(" 1. Insert at End 2. Insert at Specific Position 3. Remove First Occr 4. Is Empty 5. Find 6. Display 7. Exit ");
System.out.println("enter your choice");
ch=sc.nextInt(); //select menu item
switch(ch) //execute selected option
{
case 1: System.out.print("enter one element to be insert");
x=sc.nextInt();
arr1.add(x);
break;
case 2: System.out.print("enter one element to be insert");
x=sc.nextInt();
System.out.print("enter which location you have to insert");
int ind=sc.nextInt();
arr1.add(ind,x);
break;
case 3: System.out.print("enter one element to be delete");
x=sc.nextInt();
arr1.remove(x);
break;
case 4: if(arr1.isEmpty()==true)
System.out.println("First Array is empty ");
else
System.out.println("First Array is not empty ");
if(arr2.isEmpty()==true)
System.out.println("second Array is empty ");
else
System.out.println("second Array is not empty ");
break;
case 5: System.out.print("enter one element to be find");
x=sc.nextInt();
arr1.find(x);
break;
case 6: System.out.println("First Array elements are ");
arr1.display();
System.out.println("Second Array elements are ");
arr2.display();
break;
}
}while(ch!=7); //end of the program
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.