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

Using the class ArrayList completed in the previous exercise, write a program to

ID: 667748 • Letter: U

Question

Using the class ArrayList completed in the previous exercise, write a program to store 1,000 random numbers, each in the interval [0, 500]. The initial size of the array in the class should be set to 100. Print the numbers.

// ArrayList

public class ArrayList
{
/**
* Default constructor. Sets length to 0, initializing the list as an empty
* list. Default size of array is 20.
*/
public ArrayList()
{
list = new int[SIZE];
length = 0;
}
/**
* Determines whether the list is empty
*
* @return true if the list is empty, false otherwise
*/
public boolean isEmpty()
{
return length == 0;
}
/**
* Prints the list elements.
*/
public void display()
{
for (int i = 0; i < length; i++)
{
System.out.print(list[i] + " ");
}
System.out.println();
}
/**
* Adds the element x to the end of the list. List length is increased by 1.
*
* @param x element to be added to the list
*/
public void add(int x)
{
if (length == SIZE)
{
   SIZE = 2 * list.length;
int temp[] = new int[2 * list.length];
for(int i = 0; i < list.length; i++){
   temp[i] = list[i];
}
temp[list.length] = x;
list = temp;
} else
{
list[length] = x;
length++;
}
}
/**
* Removes the element at the given location from the list. List length is
* decreased by 1.
*
* @param pos location of the item to be removed
*/
public void removeAt(int pos)
{
for (int i = pos; i < length - 1; i++)
{
list[i] = list[i + 1];
}
length--;
}
  
private static int SIZE = 20; //size of the array that stores the list items
private int[] list; //array to store the list items
private int length; //amount of items in the list
}

// Main.java

public class Main
{
public static void main(String[] args)
{
ArrayList myList = new ArrayList();
for (int i = 0; i < 35; i++)
{
myList.add((int) (Math.random() * 50));
}
myList.display();
myList.removeAt(10);
myList.display();
}
}

Explanation / Answer

public class ArrayList
{
/**
* Default constructor. Sets length to 0, initializing the list as an empty
* list. Default size of array is 20.
*/
   public ArrayList()
   {
       list = new int[SIZE];
       length = 0;
   }
/**
* Determines whether the list is empty
*
* @return true if the list is empty, false otherwise
*/
   public boolean isEmpty()
   {
       return length == 0;
   }
/**
* Prints the list elements.
*/
   public void display()
   {
       for (int i = 0; i < length; i++)
       {
           System.out.print(list[i] + " ");
       }
       System.out.println();
   }
/**
* Adds the element x to the end of the list. List length is increased by 1.
*
* @param x element to be added to the list
*/
   public void add(int x)
   {
       if (length == SIZE)
       {
            SIZE = 2 * list.length;
           int temp[] = new int[2 * list.length];
           for(int i = 0; i < list.length; i++)
           {
                temp[i] = list[i];
           }
           temp[list.length] = x;
           list = temp;
       }
       else
       {
           list[length] = x;
           length++;
       }
   }
  
   private static int SIZE = 100; //size of the array that stores the list items
   private int[] list; //array to store the list items
   private int length; //amount of items in the list
}

// Main.java
public class Main
{
   public static void main(String[] args)
   {
       ArrayList myList = new ArrayList();
       for (int i = 0; i < 1000; i++)
       {
           myList.add((int) (Math.random() * 500));
       }
       myList.display();
      
  
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote