Question
package edu.dccc.datastructures; import java.util.Comparator; public class PhonebookData implements Comparable { String name; String mobilePhone; public PhonebookData(String name, String mobilePhone) { this.name = name; this.mobilePhone =
package edu.dccc.datastructures;
import java.util.Comparator;
public class PhonebookData implements Comparable {
String name;
String mobilePhone;
public PhonebookData(String name, String mobilePhone) {
this.name = name;
this.mobilePhone = mobilePhone;
}
public String getName() {
return name;
}
public String getMobilePhone() {
return mobilePhone;
}
public String toString() {
return name + " " + mobilePhone;
}
public static int compare(String str1, String str2) {
int l1 = str1.length(); int l2 = str2.length();
int lmin = Math.min(l1, l2);
for (int i = 0; i < lmin; i++) {
int str1_ch = (int) str1.charAt(i);
int str2_ch = (int) str2.charAt(i);
if (str1_ch != str2_ch) {
return str1_ch - str2_ch;
}
}
// Edge case for strings like
// String 1="Geeks" and String 2="Geeksforgeeks"
if (l1 != l2) {
return l1 - l2;
}
// If none of the above conditions is true,
// it implies both the strings are equal else {
return 0;
}
}
@Override
public int compareTo(Object o) {
PhonebookData pd = (PhonebookData)o; return compare(this.name,pd.name);
}
}
1. change addNode to accept PhonebookData as your data attribute:
addNode(PhonebookData data)
2. The class Node's data attribute and its constructor should be modified as follows:
class Node {
PhonebookData data;
Node previous;
Node next;
public Node(PhonebookData data) {
this.data = data;
}
}
3. In main, you should add test nodes to your class like this:
dList = new DoublyLinkedListPhonebook(); //Add nodes to the list dList.addNode(new PhonebookData("Miqun Robinson", "908-239-2222")); dList.addNode(new PhonebookData("Michael Davis", "443-904-2332")); dList.addNode(new PhonebookData("Jackson Evers", "484-904-2222")); dList.addNode(new PhonebookData("Allison Whitehead", "650-455-2222")); dList.addNode(new PhonebookData("David Lamm", "484-885-2222")); dList.addNode(new PhonebookData("Zachary Whitehead", "484-223-1234")); //Displays the nodes present in the list dList.display();
create or download a list of names and phone numbers into an external file and export the PhonebookData records into your LinkedList .You could also import a list to initialize your DLL.
4. Your app should implement a new search method in your DoublyLinkedList class that takes a String searchItem as an input parameter. Inside this method, declare a TreeSet as follows to hold your search result:
SortedSet sortedSet = new TreeSet();
This method should iterate through your DLL in a while loop as in the display method. In this case, as you iterate, you can use the contains() method on strings to apply the search:
while (current != null) { //Checks each node by incrementing the pointer. if (current.data.name.contains(searchItem) || current.data.mobilePhone.contains(searchItem)) { sortedSet.add((PhonebookData) current.data); } current = current.next; } return sortedSet;
In main, scan for your searchItem and send it to your new search method. If results are found loop through the SortedSet and print out the PhonebookData.
if (sortedSet != null) for(Object node: sortedSet) { System.out.println(((PhonebookData) node).toString()); } }
5. Allow your app to run several tests before exiting the program as we have done in previous assignments.
System.out.println(" Starting search from head test..."); System.out.print("Enter search item (or q to quit):"); searchItem = scanner.nextLine(); while (!searchItem.equals("q")) { SortedSet sortedSet = dList.search(searchItem); if (sortedSet.size() != 0) { for (Object node : sortedSet) { System.out.println(((PhonebookData) node).toString()); } } else { System.out.println("No search results found..."); } System.out.print(" Enter search item (or q to quit):"); searchItem = scanner.nextLine(); }
6. Create a search from the tail-first search method to improve your understanding of how to iterate both directions. This method will start from the tail and iterate backward using the previous attribute.
public SortedSet searchTailFirst(String searchItem) { SortedSet sortedSet = new TreeSet(); Node current = tail; if (tail == null) { System.out.println("List is empty"); return null; } // System.out.println("Nodes of doubly linked list: "); while (current != null) { //Checks each node by incrementing the pointer. if (current.data.name.toLowerCase().contains(searchItem.toLowerCase().strip()) || current.data.mobilePhone.contains(searchItem)) { sortedSet.add((PhonebookData) current.data); } current = current.previous; } return sortedSet; }
Once you have added this new method, you can run two consecutive versions of your program, one using head-first search, the other using tail-first, or you could scan for a value before you begin your test, to let the program know which direction you want to search either from the tail or from the head. Your test output should indicate at the beginning which search direction you are using.
7. PhonebookData implements the Comparable interface. Why? What happens if the implements comparable tag and the @Overrides annotation in this class are removed? Why is there a user-defined compare method in PhonebookData? Can we apply inequalities (<,>,<=,etc) to Java strings?
Sample Test Output
Nodes of doubly linked list: Miqun Robinson 908-239-4740 Michael Davis 443-904-2152 Michael Donellson 443-924-2153 Allison Whitehead 650-455-5076 David Lamm 484-885-0859 Madison Jackson 215-222-3359 Zachary Whitehead 484-223-1234
Starting search from head test... Enter search item (or q to quit):215 Madison Jackson 215-222-3359 Michael Davis 443-904-2152 Michael Donellson 443-924-2153
Enter search item (or q to quit):443 Michael Davis 443-904-2152 Michael Donellson 443-924-2153
Enter search item (or q to quit):all Allison Whitehead 650-455-5076
Enter search item (or q to quit):
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