Requirements In this assignment you will implement a lightwe

Requirements

In this assignment, you will implement a lightweight version of an ArrayList class. You may refer to the Java code and documentation for guidance, but you must write the implementation yourself. Additionally, you must write unit tests for the class\'s methods. The use of good style is now a requirement. Furthermore, you must remain in lab for the entirety of the class period unless you can demonstrate that you have correctly completed the assignment and understand the topics involved.

Design Specifications

You must implement the MyArrayList class with the following public methods.

You must write a test class with test functions for the following public methods (except the constructors):

Solution

import java.util.*;
import java.io.*;

class MyArrayList<E>{
   E[] array;
   int size,top,count;

   public MyArrayList(int initialCapacity){
       array = (E[]) new Object[size];
       size = initialCapacity;
       top=0;
       count=0;
   }

   public void add(E e){
       array[top]=e;
       top++;
       System.out.println(top);
       count++;
   }

   public void addAt(int index, E element){
       array[index]=element;
       if(index==top){
           top++;
       }
       count++;
   }

   public E get(int index){
       return array[index];
   }

   public void set(int index, E element){
       array[index]=element;
   }

   public int size(){
       return size;
   }

   public int indexOf(E object){
       for(int i=0; i<size; i++){
           if(object==array[i]){
               return i;
           }
       }
       return -1;
   }

   public boolean isEmpty(){
       if(count==0){
           return true;
       }
       else{
           return false;
       }
   }

   public void remove(int index){
       array[index]=null;
       if(top-1==count){
           top--;
       }
       count--;
   }

   public void removeObject(E object){
       for(int i=0; i<size; i++){
           if(object==array[i]){
               array[i]=null;
               if(top-1==i){
                   top--;
               }
               count--;
           }
       }
   }

   public boolean contains(E object){
       for(int i=0; i<size; i++){
           if(object==array[i]){
               return true;
           }
       }
       return false;
   }

   public void printArray(){
       for(int i=0; i<size; i++){
           System.out.println(array[i]);
       }
   }
}

public class arrayList{
   public static void main(String[] arg){
       MyArrayList<Integer> arr = new MyArrayList<Integer>(10);
       System.out.println(arr.size());
       System.out.println(arr.isEmpty());
       arr.add(3);
       System.out.println(arr.isEmpty());
       // arr.addAt(1,\"test_1\");
       // arr.printArray();
   }
}

Requirements In this assignment, you will implement a lightweight version of an ArrayList class. You may refer to the Java code and documentation for guidance,
Requirements In this assignment, you will implement a lightweight version of an ArrayList class. You may refer to the Java code and documentation for guidance,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site