Objectives: To be able to write and implement the ArrayList Data Structure.. l.
ID: 3751025 • Letter: O
Question
Objectives: To be able to write and implement the ArrayList Data Structure.. l. 2, 3. 4. 5, Explain the purpose of the program as detail as possible-8%. Develop a solution for the problem and mention algorithms to be used-12% List data structures to be used in solution,-5%. Give a description of how to use the program, and expected input/output-5% Explain the purpose of each class you develop in the program,-5%. For each method, give the pre and post conditions and invariant, if any-10% . 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. 1 One default constructor that will create an ArrayList object with a default size (capacity) of 10public ArrayListO: 2. Another constructor that accepts a parameter of type int and sets the size to this 3. A method that allows you to place a value at the end of the ArrayListpublic void 4. A method that allows you to place a value at a given locationpublic void add(int 5. A method that allows you to retrieve a value from a given locationpublic Object 6. A method that allows you the number of elements in the the ArrayListpublic int 7. Amethod would test to see if the ArrayList is emptypublic boolean isEmpty0 parameter public ArrayList(int n); add Object x); index, Object x); get(int index); size0 8. A method that see if a particular object exist in the ArrayListpublic boolean isIn(Object ob); 9. A method that will return the location of first occurrence of an Object starting from location 0public 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(Stringll args) method) name testarray.java to test the ArrayList data structure you just created. That is, you must test all ten (including the default constructor) 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. To test the ArrayList, you must randomly grnerate 15 integer pumbers ranging from 1 to 25 and add to the two ArrayList you created (One asing the no-parameter coestructor and the other using the one-parameter constructor). Of eourse, to use the ene-parameter ceastructer, you must prompt the user for an initial size of the ArrayList RememberThe size function should be based upon the actual number of elements you have in the ArrayList at any given time.Explanation / Answer
Below is your code: -
ArrayList.java
public class ArrayList {
private final int CAPACITY = 100;
private int rear;
private Object[] list;
public ArrayList() {
rear = 0;
list = new Object[CAPACITY];
}
public ArrayList(int n) {
rear = 0;
list = new Object[n];
}
public void add(Object x) {
if (size() == list.length)
expandCapacity();
list[rear] = x;
rear++;
}
public void add(int index, Object x) {
if (index < 0 || index > rear) {
System.out.println(index + " is an invalid index to add.");
return;
}
if (size() == list.length)
expandCapacity();
for (int i = rear; i > index; i--)
list[i] = list[i - 1];
list[index] = x;
rear++;
System.out.println("The object " + x + " is added at the index " + index);
}
public Object get(int index) {
if (index < 0 || index > rear)
return null;
else
return list[index];
}
public int size() {
return rear;
}
public boolean isEmpty() {
return (rear == 0);
}
public boolean isIn(Object ob) {
if (find(ob) >= 0)
return true;
else
return false;
}
public int find(Object n) {
for (int i = 0; i < rear; i++) {
if (n.equals(list[i]))
return i;
}
return -1;
}
public void remove(Object n) {
int index = find(n);
if (index < 0) {
System.out.println(n + " is not found in tha list to remove.");
return;
}
rear--;
for (int i = index; i < rear; i++) {
list[i] = list[i + 1];
}
list[rear] = null;
System.out.println("The object " + n + " is removed from the list.");
}
private void expandCapacity() {
Object[] newList = new Object[list.length * 2];
for (int i = 0; i < list.length; i++) {
newList[i] = list[i];
}
list = newList;
}
public String toString() {
String result = "";
for (int i = 0; i < rear; i++) {
result = result + list[i].toString() + " ";
}
return result;
}
}
testArray.java
public class testArray {
public static void main(String[] args) {
// parameterized constructor
ArrayList al = new ArrayList(50);
Random rand = new Random();
// add method
for (int i = 0; i < 50; i++) {
int randNum = 1 + rand.nextInt(20);
al.add(randNum);
}
// toString method
System.out.println("List: " + al);
// get method
int index1 = 25;
Object obj1 = al.get(index1);
if (obj1 == null)
System.out.println(index1 + " is an invalid index to get an element.");
else
System.out.println("The object located at the index " + index1 + " is " + obj1);
// size method
int length = al.size();
System.out.println("The number of elements in the list: " + length);
// isIn method
Object obj2 = 15;
boolean found = al.isIn(obj2);
if (found)
System.out.println("The object " + obj2 + " is found in the list.");
else
System.out.println("The object " + obj2 + " is not found in the list.");
// find method
Object obj3 = 20;
int index2 = al.find(obj3);
if (index2 >= 0)
System.out.println("The index of the object " + obj3 + " is " + index2);
else
System.out.println("The object " + obj3 + " is not found in the list.");
// remove method
Object obj4 = 10;
al.remove(obj4);
// toString method
System.out.println("List: " + al);
// another add method
int index3 = 20;
Object obj5 = 20;
al.add(index3, obj5);
// toString method
System.out.println("List: " + al);
}
}
Output
List: 7 17 11 14 17 6 18 14 18 17 17 14 17 16 19 4 4 10 16 19 14 13 16 3 2 17 8 1 4 20 19 12 17 19 16 11 7 19 19 19 8 14 18 6 5 4 6 10 15 5
The object located at the index 25 is 17
The number of elements in the list: 50
The object 15 is found in the list.
The index of the object 20 is 29
The object 10 is removed from the list.
List: 7 17 11 14 17 6 18 14 18 17 17 14 17 16 19 4 4 16 19 14 13 16 3 2 17 8 1 4 20 19 12 17 19 16 11 7 19 19 19 8 14 18 6 5 4 6 10 15 5
The object 20 is added at the index 20
List: 7 17 11 14 17 6 18 14 18 17 17 14 17 16 19 4 4 16 19 14 20 13 16 3 2 17 8 1 4 20 19 12 17 19 16 11 7 19 19 19 8 14 18 6 5 4 6 10 15 5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.