Question
In this lab you will implement the CollectionInterface with a recursive linked-based solution. You will use GenericTester.java to test your solution. The following methods should
In this lab you will implement the CollectionInterface with a recursive linked-based solution. You will use GenericTester.java to test your solution. The following methods should all use recursion in its implementation:
a. add a recursive add method adds to the end of the list and returns an LLNode. private LLNode recAdd(LLNode node, T info) {}
b. remove recursive method must return an LLNode private LLNode recAdd(LLNode node, T info) {}
c. get
d. contains
e. size
6. You must also implement a toString method in the RecursiveLinkedCollection class. It does not have to use recursion. toString is used in the tester class.
7. Use the GenericTester class to test your implementation.
8. Submit RecursiveLinkedCollection.java only.
RECURSIVE LINKED COLLECTION:
public class RecursiveLinkedCollection implements CollectionInterface {
LLNode front; int size; RecursiveLinkedCollection() { front = null; size = 0;
}
@Override public boolean add(T element) {
LLNode newNode = new LLNode<>(element);
newNode.setLink(front); front = newNode; size++; return true;
}
private LLNode recAdd(LLNode node, T info) {}
}
@Override public T get(T target) {
LLNode node = front; while(node!=null) {
if(node.getInfo().equals(target)) return node.getInfo();
} return null;
}
@Override public boolean contains(T target) {
LLNode node = front; while(node!=null) {
if(node.getInfo().equals(target)) return true;
}
return false;
}
@Override public boolean remove(T element) { r
eturn false;
}
private LLNode recAdd(LLNode node, T info) {}
}
@Override public boolean isFull()
{
return false;
}
@Override public boolean isEmpty() {
return(size == 0);
}
@Override public int size() {
return size;
}
public String toString() {
if(front == null) return " ";
String retString = front.getInfo().toString();
LLNode temp = front.getLink();
while(temp!=null ) { retString += ", " + temp.getInfo().toString(); temp = temp.getLink();
}
return retString; } }
CONTACT CLASS:
public class Contact {
public String firstName; public String lastName; public String address; public String phoneNumber;
public Contact (String fn, String ln, String add, String pn) { firstName = fn; lastName = ln; address= add; phoneNumber = pn; }
public String getfirstName() { return firstName; }
public String getLastName() { return lastName; } public String getaddress() { return address; } public String getphoneNumber() { return phoneNumber; } public void setFirstName(String fname) { this.firstName = fname; } public void setLastName(String lname) { this.lastName = lname; } public void setaddress(String add ) { this.address = add; } public void setphoneNumber(String pNumber) { this.phoneNumber= pNumber; }
@Override public String toString() { String result= firstName +" "+ lastName +" "+ phoneNumber; return result; }
public boolean equals(Object o) { Contact otherContact = (Contact) o; if(otherContact.getfirstName().equals(getfirstName()) && otherContact.getLastName().equals(getLastName()) && otherContact.getphoneNumber().equals(getphoneNumber())) { return true; } return false; } }
GENERIC TESTER:
public class GenericTester { public static void main(String[] args) { //Create 6 Contact objects Contact jim = new Contact("Jim", "Keeler","125 Main St.", "5551212"); Contact kim = new Contact("Kim", "Payne","125 Main St.", "5551212"); Contact jane = new Contact("Jane", "Reynolds","125 Main St.", "5551212"); Contact steve = new Contact("Steve", "Malone","125 Main St.", "5551212"); Contact julie = new Contact("Julie", "Reynolds","125 Main St.", "5551212"); Contact shangwen = new Contact("Shangwen", "Liu", "22 Main St.", "5551212"); RecursiveLinkedCollection
public class LLNode
public interface CollectionInterface
T get(T target); boolean remove(T element); boolean contains(T target);
boolean isFull();
boolean isEmpty();
int size(); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started