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.. Doc

ID: 3667424 • 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 - 10%.

2.     Develop a solution for the problem and mention algorithms to be used -10%

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 45%

3.     Naming of program as required 5%

4.     Print out of source code 5%

Description of Program

You are to write a program name ArrayList.java that creates an ArrayList data structure that is similar to 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.

2.Another constructor that accepts a parameter of type int and sets the size to this parameter ---- public ArrayList(int n);

3.A method that allows you to place a value at the end of the ArrayList ---- public void add(Object x);

4.A method that allows you to place a value at a given location ---- public void add(int index, Object x);

5.A method that allows you to retrieve a value from a given location ---- public Object get(int index);

6.A method that allows you to return the number of elements that is currently in the the ArrayList ---- public int size();

7.A method would test to see if the  is empty ---- public boolean isEmpty();

8.A method that see if a particular object exist in the  --- public boolean isIn(Object ob);

9.A method that will return the location of first occurrence of an Object starting from location 0 ----- public int find (Object n);

10.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. Fill the ArrayList with Card Objects (i.e. you are creating a Deck of Cards -- 52) and print these to the screen. Now, write an additional function that would take this ArrayList of Cards, and shuffle it. Write a random shuffling routine (do not invoke the shuffling routine from the library) and now display the shuffled ArrayList (Deck) of Cards. Also test all nine 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.

Remember --- The size function should be based upon the actual number of elements you have in the arraylist. NOTE: I have made this method a little bit different (and easier) than we discussed in class.

Explanation / Answer

class ArrayList{
   Object[] list;
   int size;
   // Constructor
    public ArrayList(int n){
       list = new Object[n];
       size = 0;
   }
   // Add at the end of the ArrayList
   public void add(Object x){
       list[size] = x;
       size += 1;
   }
   // Add at specific location in the ArrayList
   public void add(int index,Object x){
       list[index] = x;
   }
   // return the Object at an index
   public Object get(int index){
       return list[index];
   }
   // return the size of the list
   public int size(){
       return size;
   }
   // check if the list is empty or not
   public boolean isEmpty(){
       if (size == 0) return true;
       return false;
   }
   // find if the object is in list or not
   public boolean isIn(Object ob){
       if (find(ob) == -1) return false;
       return true;
   }
   // return the first occurance of the object
   public int find (Object n){
       for (int i = 0; i < size; i++){
           if (list[i] == n)
               return i;
       }
       return -1;
   }
   // remove the first occurence of the Object
   public void remove (Object n){
       int index = find(n);
       if (index == -1)
           System.out.println("NO SUCH ITEM IS THERE");
       for (int i = index; i < size-1; i++)
           list[i] = list[i+1];
   }
}