Question: JAVA Implement the following methods for the ArrayUnsortedList: 1. remove(T element) The method should remove the element from the list. If the removal is successful
JAVA
Implement the following methods for the ArrayUnsortedList: 1. remove(T element) The method should remove the element from the list. If the removal is successful the method should return true, otherwise it should return false. Use the equals method to compare elements. Remember that in this unsorted implementation, if a gap is created in the list, the strategy is to move the last element of the list into that space to avoid shifting all the succeeding elements 2. removeAll(T element) Similar to the remove() method, but in this case, remove all instances of the element found in the list. It should return true if any element was successfully removed, false otherwise. Use comments to document your algorithm and design decisions 3. toString() Return a formatted string that represents the list. Should contain basic information of the elements in the list. Implement a tester class to test the ArrayUnsortedList 1. Make sure the tester includes a variety of scenarios such as Removing an element successfully Attempting to remove an element that is not in the list Removing all instances of an element successfully, both when there is only one instance and when there are many Attempting to remove all instances of an element not in the list Use the toString method after each remove to confirm the results Use the return value of the remove methods appropriately to display success/failure
ArrayUnsorted list class
package ch06.lists;
public class ArrayUnsortedList
protected int numElements;
protected T[] list;
protected final int DEFCAP = 10;
int currentPos;
public ArrayUnsortedList() {
numElements = 0;
currentPos = -1;
list = (T[]) new Object[DEFCAP];
}
@Override
public int size() {
// TODO Auto-generated method stub
return numElements;
}
@Override
public void add(T element) {
// TODO Auto-generated method stub
//check if full
if (numElements == list.length)
enlarge();
list[numElements] = element;
numElements++;
}
private void enlarge() {
T[] newList = (T[]) new Object[numElements+DEFCAP];
for (int i= 0; i < numElements; i++)
newList[i] = list[i];
list = newList;
}
@Override
public boolean contains(T element) {
// TODO Auto-generated method stub
return (find(element) != -1);
}
@Override
public boolean remove(T element) {
// TODO Auto-generated method stub
return false;
}
@Override
public T get(T element) {
// TODO Auto-generated method stub
int location = find(element);
if (location == -1)
return null;
else
return list[location];
}
@Override
public void reset() {
// TODO Auto-generated method stub
currentPos = -1;
}
@Override
public T getNext() {
// TODO Auto-generated method stub
if (numElements == 0)
return null;
//our assumption is currPos starts at -1
currentPos++;
return list[currentPos];
}
protected int find(T element) {
int location = 0;
while (location < numElements) {
if (list[location].equals(element))
return location;
else
location++;
}
return -1;
}
}
Lister class
package ch06;
import ch06.lists.*;
public class ListTester {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayUnsortedList
/*
for (int i = 0; i < 15; i++)
nameList.add("Anna");
*/
nameList.add("Anna");
nameList.add("Zoe");
nameList.add("Brad");
System.out.println("Size of list: " + nameList.size());
System.out.println(nameList.get("Zoe"));
String foundName = nameList.get("Brad");
System.out.println(foundName);
System.out.println(nameList.get("Matthew"));
if (nameList.get("Justin") == null)
System.out.println("Name not found");
if (!nameList.contains("Justin"))
System.out.println("Name not found");
String nextName = nameList.getNext();
System.out.println("Next name is [Anna]: " + nextName);
nextName = nameList.getNext();
System.out.println("Next name is [Zoe]: " + nextName);
nextName = nameList.getNext();
System.out.println("Next name is [Brad]: " + nextName);
nextName = nameList.getNext();
System.out.println("Next name is [??]: " + nextName);
nextName = nameList.getNext();
nextName = nameList.getNext();
nextName = nameList.getNext();
nextName = nameList.getNext();
nextName = nameList.getNext();
nextName = nameList.getNext();
nextName = nameList.getNext();
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
