Implement a method to add elements to an arraylist
Exercise 1: Common Features of Lists
Exercise Description: Implement an abstract class called `AbstractList` that provides a skeleton implementation for common features of lists. This abstract class should define methods such as `add`, `remove`, `get`, and `size`. Then, create a concrete class called `ArrayList` that extends `AbstractList` and implements the methods using an array-based approach.
Test Cases:
1. Test Case 1:
- Input: ArrayList
- Operations:
- list.add(5);
- list.add(10);
- list.add(15);
- Expected Output:
- list.size() returns 3
- list.get(1) returns 10
2. Test Case 2:
- Input: ArrayList
- Operations:
- list.add("apple");
- list.add("banana");
- list.remove("apple");
- Expected Output:
- list.size() returns 1
- list.get(0) returns "banana"
Hint: Use Java 8 features like generics and method references to implement the abstract class and the concrete `ArrayList` class.
Exercise 2: Dynamic List Using an Array
Exercise Description: Implement a class called `DynamicList` that provides a dynamic list using an array-based approach. The class should automatically resize the array when necessary, and it should include methods such as `add`, `remove`, `get`, and `size`.
Test Cases
1. Test Case 1:
- Input: DynamicList
- Operations:
- list.add(5);
- list.add(10);
- list.add(15);
- Expected Output:
- list.size() returns 3
- list.get(1) returns 10
2. Test Case 2:
- Input: DynamicList
- Operations:
- list.add("apple");
- list.add("banana");
- list.remove("apple");
- Expected Output:
- list.size() returns 1
- list.get(0) returns "banana"
Hint: Use the concept of dynamic array resizing to implement the `DynamicList` class.
Exercise 3
Implement a method to add elements to a Collection and use an Iterator to print them
- Test Case 1: Add `Apple`, `Banana`, `Cherry` to the collection. Expected outcome: The elements are printed in the order they were added.
- Test Case 2: Add `Orange`, `Pear`, `Grape` to the collection. Expected outcome: The elements are printed in the order they were added.
Hint: Use the `add()` method of the Collection interface to add elements to the collection. Use an Iterator to traverse the collection and print the elements.
Exercise 4
Implement a method to add elements to an ArrayList, remove an element, and use a for-each loop to print the remaining elements
- Test Case 1: Add `Apple`, `Banana`, `Cherry` to the ArrayList and remove `Banana`. Expected outcome: `Apple` and `Cherry` are printed.
- Test Case 2: Add `Orange`, `Pear`, `Grape` to the ArrayList and remove `Pear`. Expected outcome: `Orange` and `Grape` are printed.
Hint: Use the `add()` method of the ArrayList class to add elements to the list. Use the `remove()` method to remove an element. Use a for-each loop to traverse the list and print the elements.