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

you will create a Java class of your choice, instantiate several objects of that

ID: 3844285 • Letter: Y

Question

you will create a Java class of your choice, instantiate several objects of that class, add those object to an ArrayList, and manipulate that list using several methods from the ArrayList class.
   Create a class of your own selection. It should have at least 2 instance variables, 2 constructors, toString(), and equals().
   In a different class, create a main method.
   In main, instantiate at least 8 objects of the class you created in step 1 above.
   Add those objects to an ArrayList which uses your class as its base type.
   Go to the Java API (Application Programming Interface) for ArrayLists and read about the ArrayList class, along with its constructors and methods:
https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
   Along with the set, get, remove, trimToSize, and clear methods, select at least 5 other methods from the API and use them on your ArrayList. Identify the 5 methods in your Introductory Comments and/or in your code. Do not use an iterator.
   Write two paragraphs on the differences and similarities between arrays, the Vector class, and the ArrayList class. Identify the advantages and disadvantages of each.
   Submit a listing of the class created in step 1 above, a listing of the class containing main, and the paragraphs written in the last step above. Make sure that you identify the 5 other methods you used besides those listed in step 6 above.

Explanation / Answer

Find the program below.

Employee.java

package com.chegg.emp;

public class Employee {

   private String name;
   private String idNumber;
   private String department;
   public Employee() {}
  
   public Employee(String name, String idNumber, String department) {
       this.name = name;
       this.idNumber = idNumber;
       this.department = department;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public String getIdNumber() {
       return idNumber;
   }

   public void setIdNumber(String idNumber) {
       this.idNumber = idNumber;
   }

   public String getDepartment() {
       return department;
   }

   public void setDepartment(String department) {
       this.department = department;
   }

   @Override
   public String toString() {
       return "Employee [name=" + name + ", idNumber=" + idNumber + ", department=" + department + "]";
   }
   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Employee other = (Employee) obj;
       if (department == null) {
           if (other.department != null)
               return false;
       } else if (!department.equals(other.department))
           return false;
       if (idNumber == null) {
           if (other.idNumber != null)
               return false;
       } else if (!idNumber.equals(other.idNumber))
           return false;
       if (name == null) {
           if (other.name != null)
               return false;
       } else if (!name.equals(other.name))
           return false;
       return true;
   }

}

ArrayListOperations.java

import java.util.ArrayList;

import java.util.List;

import com.chegg.emp.Employee; //The Employee class import which we created above

public class ArrayListOperations {

   public static void main(String[] args) {
       Employee e1 = new Employee();
       Employee e2 = new Employee();
       Employee e3 = new Employee();
       Employee e4 = new Employee();
       Employee e5 = new Employee();
       Employee e6 = new Employee();
       Employee e7 = new Employee();
       Employee e8 = new Employee();
       Employee e9 = new Employee();
       e1.setIdNumber("1");
       e2.setIdNumber("2");
       e3.setIdNumber("3");
       e4.setIdNumber("4");
       e5.setIdNumber("5");
       e6.setIdNumber("6");
       e7.setIdNumber("7");
       e8.setIdNumber("8");
       e9.setIdNumber("9");
       e1.setName("mars");
       e2.setName("venus");
       e3.setName("earth");
       e4.setName("Neptune");
       e5.setName("pluto");
       e6.setName("saturn");
       e7.setName("uranus");
       e8.setName("nexus");
       e9.setName("star");
       e1.setDepartment("A");
       e2.setDepartment("B");
       e3.setDepartment("C");
       e4.setDepartment("D");
       e5.setDepartment("E");
       e6.setDepartment("F");
       e7.setDepartment("G");
       e8.setDepartment("H");
       e9.setDepartment("I");
      
       List<Employee> empList = new ArrayList<Employee>(); //base type-Employee object
       empList.add(e1); // Appends the specified element to the end of this list.
       empList.add(e2);
       empList.add(e3);
       empList.add(e4);
       empList.add(e5);
       empList.add(e6);
       empList.add(e7);
       empList.add(e8);
       empList.add(e9);
       System.out.println(empList.toString());
       empList.set(1, e4); //Replaces the element at the specified position in this list with the specified element 1.
       System.out.println(empList.toString());
       System.out.println(empList.get(2)); //Returns the element at the specified position in this list.
       empList.remove(3); //Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).
       System.out.println(empList.toString());
       empList.remove(e9); //Removes the specified object from this list.
       System.out.println(empList.toString());
       ((ArrayList<Employee>) empList).trimToSize(); //Trims the capacity of this ArrayList instance to be the list's current size.
       System.out.println(empList.toString());
       System.out.println(empList.lastIndexOf(e5));
       System.out.println(empList.isEmpty()); //Returns true if this list contains no elements.
       System.out.println(empList.contains(e7)); //Returns true if this list contains the specified element.
       System.out.println(empList.toArray()); //Returns an array containing all of the elements in this list in proper sequence (from first to last element).
       System.out.println(empList.subList(1, 3)); //Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
       empList.clear(); //it clears all the element from the list.
   }

}

OUTPUT:

[Employee [name=mars, idNumber=1, department=A], Employee [name=venus, idNumber=2, department=B], Employee [name=earth, idNumber=3, department=C], Employee [name=Neptune, idNumber=4, department=D], Employee [name=pluto, idNumber=5, department=E], Employee [name=saturn, idNumber=6, department=F], Employee [name=uranus, idNumber=7, department=G], Employee [name=nexus, idNumber=8, department=H], Employee [name=star, idNumber=9, department=I]]
[Employee [name=mars, idNumber=1, department=A], Employee [name=Neptune, idNumber=4, department=D], Employee [name=earth, idNumber=3, department=C], Employee [name=Neptune, idNumber=4, department=D], Employee [name=pluto, idNumber=5, department=E], Employee [name=saturn, idNumber=6, department=F], Employee [name=uranus, idNumber=7, department=G], Employee [name=nexus, idNumber=8, department=H], Employee [name=star, idNumber=9, department=I]]
Employee [name=earth, idNumber=3, department=C]
[Employee [name=mars, idNumber=1, department=A], Employee [name=Neptune, idNumber=4, department=D], Employee [name=earth, idNumber=3, department=C], Employee [name=pluto, idNumber=5, department=E], Employee [name=saturn, idNumber=6, department=F], Employee [name=uranus, idNumber=7, department=G], Employee [name=nexus, idNumber=8, department=H], Employee [name=star, idNumber=9, department=I]]
[Employee [name=mars, idNumber=1, department=A], Employee [name=Neptune, idNumber=4, department=D], Employee [name=earth, idNumber=3, department=C], Employee [name=pluto, idNumber=5, department=E], Employee [name=saturn, idNumber=6, department=F], Employee [name=uranus, idNumber=7, department=G], Employee [name=nexus, idNumber=8, department=H]]
[Employee [name=mars, idNumber=1, department=A], Employee [name=Neptune, idNumber=4, department=D], Employee [name=earth, idNumber=3, department=C], Employee [name=pluto, idNumber=5, department=E], Employee [name=saturn, idNumber=6, department=F], Employee [name=uranus, idNumber=7, department=G], Employee [name=nexus, idNumber=8, department=H]]
3
false
true
[Ljava.lang.Object;@15db9742
[Employee [name=Neptune, idNumber=4, department=D], Employee [name=earth, idNumber=3, department=C]]

---------------------------------------------------------------------

In the above program I used 5 other methods also. Find below the table.

Arrays

Vector

ArrayList

Definition

1. An Array (java.util.Array) is a collection of similar type of elements that have contiguous memory location.
2. Java array is an object the contains elements of similar data type. It is a data structure where we store similar elements. We can store only fixed set of elements in a java array.

Vectors (java.util.Vector) are commonly used instead of arrays, because they expand automatically when new data is added to them. If you want to put a primitive type in a Vector, put it inside an object

The ArrayList class extends AbstractList and implements the List interface.ArrayList supports dynamic arrays that can grow as needed. Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, whichmeans that you must know in advance how many elements an array will hold.

Advantages

Code Optimization: It makes the code optimized, we can retrieve or sort the data easily.
Random access: We can get any data located at any index position.

All the methods in Vector are painfully synchronized. Synchronization over a collection if required can be applied outside the class, preferably on case by case basis.

ArrayList is variable length, which means ArrayList can grow or shrink its size dynamically. ArrayList can roughly increment its size by 1.5 times.

Disadvantages

Size Limit: We can store only fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in java.

Vector is a very old class from before the introduction of the Collections framework, therefore its API is polluted with many legacy methods that duplicate the methods from the Collection and List interface.

ArrayList has a performance limitation. It allocates a particular amount of space. the "capacity". Once you expand the ArrayList to exceed that capacity, the ArrayList has to copy the current array into new memory space, which has a performance hit.

5 ArrayList Methods Method Name Functions lastIndexOf(Object) it returns the last occurence index of the given object. isEmpty() it returns true if the list is empty, otherwise it returns false contains(Object) it returns true if the given object if availble in the list, otherwise it returns false. toArray() it returns the list as array subList(int fromIndex, int TOIndex) it returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.