Write a List class implementation including the methods that we wrote together i
ID: 3708288 • Letter: W
Question
Write a List class implementation including the methods that we wrote together in class: a default constructor that initializes the list to have a maximum size of 10 a non-default constructor that initializes the maximum list size to a specific value a size0 method that returns the number of actual items in the list an add0 method that adds a new value to the list a contains0 method that searches for a specific value and returns true/false if it is found/not found a get0 method that returns the value in the list at a specific index a toString0 method that prints out information about the data fields of the List class Write a separate Main program that declares a List object. Then declares objects of type Integer and adds them to the List. Call the various List methods to test them and make sure that they function properly in all cases. Now add two new methods to the class. Which of these methods can potentially throw an ArrayIndexOutOfBounds exception? Include a comment in your implementation to explain boolean remove(Object o) This method should remove the first occurrence of the specified object in the List. .Object remove(int i) This method should remove the value in the List at position i. The value of the removed item should be returned Use your Main class to test your remove0 methods.Explanation / Answer
Hello. I have created the required List class and the Main class to test the working of List class. Everything is implemented exactly as needed and explained using Javadoc comments. Drop a comment if you have any doubts. Thanks.
// List.java
public class List {
// attributes
private Object[] items;
private int size;
/**
* default constructor, initializes the array with max size 10
*/
public List() {
items = new Object[10];
size = 0;
}
/**
* parameterized constructor, initializes the array with max size 'MAX'
*/
public List(int MAX) {
items = new Object[MAX];
size = 0;
}
/**
* returns the current size of the list
*/
public int size() {
return size;
}
/**
* method to add an object to the list
*
* @param o
* - object to be added
*/
public void add(Object o) {
/**
* checking whether the list is full or not
*/
if (size < items.length) {
/**
* adding to the end
*/
items[size] = o;
size++;
}
}
/**
* checks if the list contains an object
*
* @param obj
* - object to be searched
* @return - true if found else false
*/
public boolean contains(Object obj) {
for (int i = 0; i < size; i++) {
if (items[i].equals(obj)) {
return true;
}
}
return false;
}
/**
* method to get an element by giving the index
*
* @param i
* - index value
* @return - object if the index is valid, else null
*/
public Object get(int i) {
if (i < 0 || i >= size) {
// invalid index
return null;
}
return items[i];
}
@Override
public String toString() {
/**
* returning a String representation of the list
*/
String data = "[";
for (int i = 0; i < size; i++) {
data += items[i];
if (i == size - 1) {
data += "]";
} else {
data += ",";
}
}
return data;
}
/**
* method to remove an object if exist
*
* @param o
* - object to be removed
* @return - true if found and removed else false
*/
public boolean remove(Object o) {
for (int i = 0; i < size; i++) {
if (items[i].equals(o)) {
// found
/**
* shifting elements to the left to occupy the vacant space
*/
for (int j = i; j < size - 1; j++) {
items[j] = items[j + 1];
}
size--;
return true;
}
}
return false;
}
/***
* method to remove an object at the index i. This method could potentially
* throw ArrayIndexOutOfBoundsException , but the index value is validated
* before removing the element from a given index, hence, it will not throw
* the exception anymore
*
* @param i
* - index
* @return - the removed object if the index is valid, else null
*/
public Object remove(int i) {
if (i < 0 || i >= size) {
// invalid index (out of bounds)
return null;
}
Object obj = items[i];
/**
* shifting elements to the left to occupy the vacant space
*/
for (int j = i; j < size - 1; j++) {
items[j] = items[j + 1];
}
size--;
return obj;
}
}
// Main.java
public class Main {
public static void main(String[] args) {
/**
* Defining a list, testing with a few Integer objects
*/
List list = new List();
Integer int1 = 10, int2 = 20, int3 = 30, int4 = 40, int5 = 50;
list.add(int1);
list.add(int2);
list.add(int3);
list.add(int4);
list.add(int5);
System.out.println("List: " + list);
System.out.println("Current size: " + list.size());
System.out.println("List contains 20? " + list.contains(20));
System.out.println("List contains 90? " + list.contains(90));
System.out.println("Removing " + int2);
list.remove(int2);
System.out.println("List after removing " + int2 + ": " + list);
System.out.println("Removing object at index 2");
Object o = list.remove(2);
System.out.println(o + " is removed");
System.out.println("Current list: " + list);
}
}
/*OUTPUT*/
List: [10,20,30,40,50]
Current size: 5
List contains 20? true
List contains 90? false
Removing 20
List after removing 20: [10,30,40,50]
Removing object at index 2
40 is removed
Current list: [10,30,50]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.