Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objectives: To be able to write and implement the ArrayList Data Structure Docum

ID: 3872607 • Letter: O

Question

Objectives: To be able to write and implement the ArrayList Data Structure Documentation: I. 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: I. 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 2. 3. A method that allows you to place a value at the end of the ArrayList (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); public void add(Object x); void add(int index, Object x); - public Object get(int index); 4. A method that allows you to place a value at a given location public 5. A method that allows you to retrieve a value from a given locatiorn

Explanation / Answer


public class ArrayList {

   private Object[] arr;

   private int actualSize;

   // default constructor
   public ArrayList()
   {
       this(10);

   }

   // parameterised constructor.
   public ArrayList(int n){
       arr=new Object[n];
   }


// this method get the list element based on index.
   public Object get(int index){
       if(index < actualSize){
           return arr[index];
       } else {
           throw new ArrayIndexOutOfBoundsException();
       }
   }

   // it adds the element at last position of the list.
   public void add(Object obj){
       if(arr.length==actualSize){
           increase();
       }
       arr[actualSize++] = obj;
   }

   // it removes the list element based on given index as in parameter.
   public Object remove(int index){
       if(index < actualSize){
           Object obj = arr[index];
           arr[index] = null;
           int tmp = index;
           while(tmp < actualSize){
               arr[tmp] = arr[tmp+1];
               arr[tmp+1] = null;
               tmp++;
           }
           actualSize--;
           return obj;
       } else {
           throw new ArrayIndexOutOfBoundsException();
       }

   }

   // it returns the size of list.
   public int size(){
       return actualSize;
   }

   // it returns true if list is empty, otherwise false.
   public boolean isEmpty()
   {
       return actualSize == 0;
   }

   // this method will increase the size of arrayList, when it saturates the length.
   private void increase(){
       int s=arr.length+(arr.length/2);
       Object[] temp=new Object[s];
       for(int i=0;i<arr.length;i++){
           temp[i]=arr[i];
       }
       arr=temp;
       System.out.println(" New length: "+arr.length);
   }


   // toString to print the arrayList elements.
   public String toString(){

       if(size()==0) return "[]";
       StringBuilder s=new StringBuilder("["+get(0));
       for(int i=1;i<size();i++){
           s.append(","+get(i));

       }
       s.append("]");
       return s.toString();

   }


// add method based on index positon.
   public void add(int index, int e)
   {

       if (actualSize == arr.length)
           increase();
       if (index != actualSize)
           System.arraycopy(arr, index, arr, index + 1, actualSize - index);
       arr[index] = e;
       actualSize++;
   }
  
}


public class TestArray {

  
  
    public static void main(String a[]){
        ArrayList mal = new ArrayList();
        for(int i=1;i<=15;i++)
        mal.add(((int) (Math.random()*(25 - 1))) + 1);
        System.out.println(mal);
         mal.add(1, 100);
         System.out.println(mal);
        ArrayList mal1 = new ArrayList(15);
        for(int i=1;i<=15;i++)
        mal1.add(((int) (Math.random()*(25 - 1))) + 1);
        System.out.println(mal1);
      
     
      
    }
}

/* output:-


New length: 15
[15,4,12,8,5,4,24,19,10,12,1,8,14,4,6]

New length: 22
[15,100,4,12,8,5,4,24,19,10,12,1,8,14,4,6]
[5,18,3,24,1,7,15,12,17,2,22,23,7,21,24]


*/