Programming Assignment: ArrayList

Instructions:

  1. Given the skeleton code for class MyArrayList, complete two methods: removeDuplicates and union. The print method has been completed. findMax method serves as an example. The items in the arrayList object are of type "Integer" which is provided by Java APIs.

    A skeleton code for class ArrayList:

    //This program has been tested with java version of 12.
    import java.util.ArrayList;
    import java.util.List;
    
    public class MyArrayList {
    	public static void main(String[] args) {
    		int [] a = {23, 5, 3, 7, 5, 6, 4, 25, 2, 2, 5, 2, 4};
    		ArrayList<Integer> list = new ArrayList<>();
    		
    		for (int i = 0; i < a.length; i++)
    		{
    			list.add(Integer.valueOf(a[i]));
    		}
    		
    		System.out.println("The max integer in the list is : " + findMax(list));
    				
    		System.out.println("The integers with duplicates are : ");
    		print(list);
    		removeDuplicates(list);
    		System.out.println("\nThe integers after removing duplicates are : ");
    		print(list);		
    		
    		int [] b = {8, 5, 9, 7, 6};
    		ArrayList list2 = new ArrayList<>();
    		
    		for (int i = 0; i < b.length; i++)
    		{
    			list2.add(Integer.valueOf(b[i]));
    		}
    		
    		System.out.println("\n\nThe integers before union are : ");
    		print(list);
    		System.out.println("\n");
    		print(list2);		
    		union(list, list2);
    		System.out.println("\nThe integers after union are : ");
    		print(list);		
    	}
    
    	public static int findMax(ArrayList list)
    	{	
    		int max = list.get(0).intValue();
    		
    		for (int i = 0; i < list.size(); i++){
    				Integer n = list.get(i);
    					if ( n.intValue() > max )
    						max = n.intValue();
    		}
    		
    		return max;
    	}
    	
    	//This method takes an array list object as input and remove the duplicates in it
    	//The items in the array are objects of Integer type (Java APIs). Use the methods defined for class Integer
    	public static void removeDuplicates(ArrayList list)
    	{	
    		//Using brute force method would be fine
    		//To be completed
    
    	}
    	
    	//This method takes two array lists as input. The resultant list after union is store into the first list.
    	public static void union(ArrayList list1, ArrayList list2)
    	{
    		//Hint: Use "contains() to check if an Integer object is in an arrayList object"
    		//To be completed
    	}	
    	
    	//This method prints items stored in an arrayList object.
    	public static void print(ArrayList lst)
    	{
    		for (int i = 0; i < lst.size(); i++){
    					System.out.print(lst.get(i).intValue() + ", ");
    		}
    	}
    }
    
    
    
  2. Sample output:
    The max integer in the list is : 25
    
    The integers with duplicates are :
    23, 5, 3, 7, 5, 6, 4, 25, 2, 2, 5, 2, 4,
    
    The integers after removing duplicates are :
    23, 3, 7, 6, 25, 5, 2, 4,
    
    The integers before union are :
    List 1: 23, 3, 7, 6, 25, 5, 2, 4,
    List 2: 8, 5, 9, 7, 6,
    The integers after union are :
    23, 3, 7, 6, 25, 5, 2, 4, 8, 9,