Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 addressBook = new RecursiveLinkedCollection<>(); addressBook.add(jim); addressBook.add(kim); addressBook.add(jane); addressBook.add(steve); addressBook.add(julie); addressBook.add(shangwen); //Test toString and add methods System.out.println("toString expected: Jim Keeler 5551212,Kim Payne 5551212,Jane Reynolds 5551212,Steve Malone 5551212,Julie Reynolds 5551212,Shangwen Liu 5551212"); System.out.println("toString acutal: "+addressBook.toString()); System.out.println(); //Test get method System.out.println("get expected: Steve Malone 5551212"); System.out.println("get acutal: "+addressBook.get(steve).toString()) ; System.out.println(); //Test contains method - element in collection System.out.println("contains expected: true"); System.out.println("contains acutal: "+addressBook.contains(shangwen)); System.out.println(); //Test contains method - element NOT in collection Contact notInList = new Contact("Not", "InList","125 Main St.", "5551212"); System.out.println("contains expected: false"); System.out.println("contains acutal: "+addressBook.contains(notInList)); System.out.println(); //Test isEmpty method System.out.println("isEmpty expected: false"); System.out.println("isEmpty acutal: "+addressBook.isEmpty()); System.out.println(); //Test isFull method System.out.println("isFull expected: false"); System.out.println("isFull acutal: "+addressBook.isFull()); System.out.println(); //Test size method System.out.println("size expected: 6"); System.out.println("size acutal: "+addressBook.size()); System.out.println(); //Test remove methods addressBook.remove(shangwen); System.out.println("remove expected: Jim Keeler 5551212,Kim Payne 5551212,Jane Reynolds 5551212,Steve Malone 5551212,Julie Reynolds 5551212"); System.out.println("remove acutal: "+addressBook.toString()); System.out.println(); //Test remove methods addressBook.remove(jane); System.out.println("remove expected: Jim Keeler 5551212,Kim Payne 5551212,Steve Malone 5551212,Julie Reynolds 5551212"); System.out.println("remove acutal: "+addressBook.toString()); System.out.println(); //Test size method System.out.println("size expected: 4"); System.out.println("size acutal: "+addressBook.size()); System.out.println(); } } LL NODE:

public class LLNode { protected T info; //holds the data protected LLNode link; //holds memory address of next node in list public LLNode(T info) { super(); this.info = info; } public T getInfo() { return info; } public void setInfo(T info) { this.info = info; } public LLNode getLink() { return link; } public void setLink(LLNode link) { this.link = link; } } COLLECTION INTERFACE:

public interface CollectionInterface { boolean add(T element);

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions

Question

LO1 Discuss four different views of motivation at work.

Answered: 1 week ago