BookListInterface.java specifies a Java interface for an ADT that maintains a li
ID: 3591813 • Letter: B
Question
BookListInterface.java specifies a Java interface for an ADT that maintains a list of book titles. This interface includes two operations:
add(String book) – add a book title to the list
onThisList(String book) – check if a given book title is on the list
Develop a seperate Linked list implementations of this interface, with a different underlying data structure:
The implementation is a separate class, and it must include a toString() method to list out the books, one title per line.
The source code for the interface is :
package bookList;
public interface BookListInterface
{
void add(String book);
boolean onThisList(String book);
}
The linked list implementation must use the LLNode.java class provided in the bookList package; do not use the LinkedList class from the java.util package:
package bookList;
public class LLStringNode
{
private String info;
private LLStringNode next;
public LLStringNode(String info)
{
this.info = info;
next = null;
}
public void setInfo(String info)
{
this.info = info;
}
public String getInfo()
{
return info;
}
public void setNext(LLStringNode next)
{
this.next = next;
}
public LLStringNode getNext()
{
return next;
}
}
For each implementation:
Instantiate a list object and add the titles of five of your favorite books;
Using System.out.println(), display the list of books;
Use the onThisList() method to test whether Harry Potter and the Sorcerer's Stone is on your list, and report out the result;
Use the onThisList() method to test another title, and report out that result – make sure to use a title that produces the opposite result from the first test.
Explanation / Answer
public class StringLinkedList implements BookListInterface {
private LLStringNode start = null;
@Override
public void add(String book) {
LLStringNode tmp = new LLStringNode(book);
tmp.setNext(start);
start = tmp;
}
@Override
public boolean onThisList(String book) {
LLStringNode tmp = start;
while (tmp != null)
if (Objects.equals(tmp.getInfo(), book))
return true;
return false;
}
@Override
public String toString() {
String result = "";
LLStringNode tmp = start;
while (tmp != null){
result = result + tmp.getInfo();
if (tmp.getNext() != null)
result += " ,";
tmp = tmp.getNext();
}
return result;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.