
Ex. 02 : ArrayList<E>
About
Instructions : Create the following Array List Data Structure in your utility package and use "for loops" for your repetitive tasks.
Where to find starter code in my-api
package.class : utility.ArrayList
package.class : testing.ArrayListTest
What has to be added to my-api
package.interface : utility.List
Where to find unit test in my-api
package.class : tests.ArrayListJUnitTest
Task Check List
- Place a SCREENSHOT of your results (pass/fail) JUNIT Tests for your Array List in your report.
- ONLY "for" loops should be used within the data structure class.
- Names of identifiers MUST match the names listed in the description below. Deductions otherwise.
Description
This Array List Data Structure operates like a dynamic array , which can grow or reduce its size. Think through the class construction so that it meets the following specifications:
data fields: The fields to declare have private accessibility.
- size : stores the number of occupied locations in internal array, and is of type int.
- data : is a reference variable for the internal array and is of type E[ ].
constant: Use to describe defaults.
- DEFAULT_CAPACITY is a constant that holds a default capacity of ten (10) for the underlying array, is of type int , public accessibility and modified to static.
constructors: The overloaded constructors will initialize the data fields size and data.
- The default constructor calls the second constructor, generating an internal array with the specified DEFAULT_CAPACITY.
- The second constructor generates an internal array with the specified initial capacity. If the capacity is less than zero, this constructor should throw an IllegalArgumentException.
public ArrayList(int capacity)
Note: Java does not allow you to construct a generic array i.e
data = new E[capacity]; // illegal
so, you must use
data = (E[])new Object[capacity]; // valid
methods: manages the behavior of the internal array.
Together, the methods below give the illusion of a dynamic array (an array that grows or shrinks). Implement these methods with in your generic Array List class.
Method |
Description |
Header |
add(item) | appends the item specified to the end of the list. This method returns true, if the data was added successfully. |
public boolean add(E item) |
add(index, item) | inserts the item specified at the given index in the list. Shifts subsequent elements to the right. |
public void add(int index, E item) |
checkIndex(index) |
checks if the given index is valid. Throws an IndexOutOfBoundsException, if invalid. This is a private helper method. Note: This method should be used in any method that requires a precondition for access to valid index. |
private void checkIndex(int index) |
clear() |
clears list of all elements, returns size back to zero. |
public void clear() |
contains(item) | searches for a specific value within the internal array and returns true, if the value is in the list. |
public boolean contains(E item) |
ensureCapacity(int capacity) | doubles the capacity of the underlying array, if necessary, to ensure that it can hold the number of elements specified by the capacity argument. This will most likely have a cast to a generic type. |
@SuppressWarnings("unchecked") public void ensureCapacity(int capacity) |
get(index) | returns the item at the specified position in the list. This method first checks if the index requested is valid. |
public E get(int index) |
indexOf(item) | searches for a specific item within the linked structure and returns the first occurrence (location/index) in the array, otherwise returns -1, if NOT found. |
public int indexOf(E item) |
isEmpty() |
returns true, if the list is empty, i.e., the list contains no elements. |
public boolean isEmpty() |
remove(index) | removes the item at the given index in the list. Shifts subsequent elements to the left and returns the item removed. |
public E remove(int index) |
remove(item) | removes the first occurrence of the item specified from the list, if present. Shifts subsequent elements to the left and returns true, if the item is removed. |
public boolean remove(E item) |
set(index, item) | replaces the item at the specified position with the one specified. This method validates the index before replacing the item. |
public E set(int index, E item) |
shiftLeft(index) |
helper method that shifts elements of internal array left to index location. |
private void shiftLeft(int index) |
shiftRight(index) |
helper method that shifts elements of internal array right from index location. |
private void shiftRight(int index)
|
size() | returns the number of elements in the list. This is the size of the occupied locations of the array. |
public int size() |
toString() | displays the contents of the list. |
public String toString() |

Trending nowThis is a popular solution!
Step by stepSolved in 2 steps

- Java code 12. Given an existing ArrayList named contactList, find the number of contacts in the ArrayList and store it in the existing variable named numContacts.arrow_forwardOnly number 3arrow_forwardUsing an appropriate package and test class name, write the following tests for GamerProfile's constructor: testNameShouldNotBeNull testNameShouldNotBeEmpty testNameShouldNotBeBlank testShouldCreateValidGamerProfile Hint: use assertTrue or assertFalse for getters involving boolean values Hint: get the game list from the gamer and call the list's isEmpty along with an assertTrue or assertFalse, as appropriate.GamerProfile:public class GamerProfile { private String userName; private boolean pvpEnabled; private boolean online; private ArrayList<GameInfo> gameLibrary; public GamerProfile(String userName) { this.userName = userName; this.pvpEnabled = false; this.online = false; gameLibrary = new ArrayList<GameInfo>(); } // Getter for the getUserName variable public String getUserName() { return userName; } // Getter for the PvpEnabled…arrow_forward
- You may have found it somewhat tedious and unpleasant to use the debugger and visualizer to verify the correctness of your addFirst and addLast methods. There is also the problem that such manual verification becomes stale as soon as you change your code. Imagine that you made some minor but uncertain change to addLast]. To verify that you didn't break anything you'd have to go back and do that whole process again. Yuck. What we really want are some automated tests. But unfortunately there's no easy way to verify correctness of addFirst and addLast] if those are the only two methods we've implemented. That is, there's currently no way to iterate over our list and get bad its values and see that they are correct. That's where the toList method comes in. When called, this method returns a List representation of the Deque. For example, if the Deque has had addLast (5) addLast (9) addLast (10), then addFirst (3) called on it, then the result of toList() should be a List with 3 at the…arrow_forwardWhat happens if the programmer removes the member function getBalance (), and instead substitutes it with sac. getBalance() in conjunction with sac.balance inside the displayBalance instruction? Why? You will need to make the appropriate adjustments to the application so that it can continue to run once these changes have been made.arrow_forwardWhat are the Javadoc comments for each class? I am strugglingarrow_forward
- I'm interested in creating a digital personal assistant similar to Alexa and Siri, which can efficiently manage schedules. Specifically, I want to design a tool in Java that allows users to find events during a certain time period. This tool should also enable users to add and cancel events as their schedules change. To achieve this, I plan to use a SkipListMap class with specific operations (get, put, remove, subMap) and a DoublyLinkedList class. Additionally, I'll use the FakeRandomHeight class for debugging and testing. Could you provide guidance on how to approach this project, especially in terms of implementing the SkipListMap class and managing event data efficiently? Sample Input: AddEvent 101910 BreakfastAddEvent 101913 LunchAddEvent 101918 DinnerGetEvent 101914AddEvent 101911 Data StructuresAddEvent 102007 ExercisePrintSkipListAddEvent 101915 HomeworkCancelEvent 102007AddEvent 102013 RelaxAddEvent 102011 Read a BookGetEvent 101915AddEvent 101914 Call MomPrintSkipList I…arrow_forwardYou have to use comment function to describe what each line does import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class PreferenceData { private final List<Student> students; private final List<Project> projects; private int[][] preferences; private static enum ReadState { STUDENT_MODE, PROJECT_MODE, PREFERENCE_MODE, UNKNOWN; }; public PreferenceData() { super(); this.students = new ArrayList<Student>(); this.projects = new ArrayList<Project>(); } public void addStudent(Student s) { this.students.add(s); } public void addStudent(String s) { this.addStudent(Student.createStudent(s)); } public void addProject(Project p) { this.projects.add(p); } public void addProject(String p) { this.addProject(Project.createProject(p)); } public void createPreferenceMatrix() { this.preferences = new…arrow_forwardQuestion 1 Which of the following is NOT true about ArrayLists? ArrayLists can only hold Objects, not primitive types. ArrayLists have built in methods that you can call on them for quick add, delete, search, etc. ArrayLists can hold more elements than an Array. ArrayLists have a flexible size which you do not have to define at declaration.arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY





