Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

BookListInterface.java specifies a Java interface for an ADT that maintains a li

ID: 3885150 • 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 three implementations of this interface, each with a different underlying data structure:

Array

Linked list

ArrayList from the java.util package

Each implementation is a separate class, and each of these classes is to 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;

       }

}

Use an appropriate ArrayList method to efficiently develop the book list ADT based on that data structure, along with an Iterator object to traverse the list of book titles for the toString() method.

In addition to the three ADT classes, create a Java program – i.e., a Java class with a main() method – to test each of the implementations. 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; and

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

// BookListInterface

package bookList;

public interface BookListInterface {

   void add(String book);    

     boolean onThisList(String book);
  
}


// BookArray

package bookList;

import java.util.Arrays;

public class BookArray implements BookListInterface{

   private String[] bookArray=null;
   private int bookLength=0;
  
   public BookArray() {
       this(10);
  
   }
  
   public BookArray(int bookArraySize) {
       super();
       this.bookArray = new String[bookArraySize];
  
   }

   @Override
   public void add(String book) {
      
       if(bookLength>=bookArray.length) increase();
       bookArray[bookLength++]=book;
      
   }

   @Override
   public boolean onThisList(String book) {
      
       for(int i=0;i<bookArray.length;i++){
           if(bookArray[i].equals(book)) return true;
       }
       return false;
   }

   public void increase(){
      
       int s=bookArray.length+(bookArray.length/2);
       String[] temp=new String[s];
       for(int i=0;i<bookArray.length;i++){
           temp[i]=bookArray[i];
       }
       bookArray=temp;
   }
  
   @Override
   public String toString() {
      
       return "BookArray"+" "+ Arrays.toString(bookArray) +" ";
   }
  
public static void main(String[] args) {
      
       BookArray b=new BookArray();
       b.add("java");
       b.add("spring");
       System.out.println(b.onThisList("java"));
     System.out.println(b);
}
}

// BookArrayList

package bookList;

import java.util.ArrayList;
import java.util.Iterator;

import bookList.BookListInterface;
public class BookArrayList implements BookListInterface{

   ArrayList<String> bookList=new ArrayList<String>();
   @Override
   public void add(String book) {
        bookList.add(book);
   }

   @Override
   public boolean onThisList(String book) {
      
       Iterator<String> bookIterator = bookList.iterator();
       while (bookIterator.hasNext()) {
          
           if(bookIterator.next().equals(book)){
               return true;
           }
      
       }
      
       return false;
   }

   @Override
   public String toString() {
      
       return "BookArrayList"+" "+ bookList +" ";
   }

   public static void main(String[] args) {
      
       BookArrayList b=new BookArrayList();
       b.add("java");
       b.add("spring");
       System.out.println(b.onThisList("java"));
       System.out.println(b);
   }
}

// LLStringNode

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;

     }

   @Override
   public String toString() {
       return "LLStringNode [info=" + info + ", next=" + next + "]";
   }
   
   
}


// LLNode.java

package bookList;

public class LLNode implements BookListInterface{

   private LLStringNode start;
   int count;
  
   @Override
   public void add(String book) {
      
       if(start==null){
           start=new LLStringNode(book);
           count++;
           return;
       }
       LLStringNode p=start;
       while(p.getNext()!=null){
           p=p.getNext();
       }
       p.setNext(new LLStringNode(book));
       count++;
   }

   @Override
   public boolean onThisList(String book) {
      
       LLStringNode current = start;    //Initialize current
        while (current != null)
        {
           System.out.println(current);
            if (current.getInfo().equals(book))
                return true;    //data found
            current = current.getNext();
        }
        return false;    //data not found
   }

   @Override
   public String toString() {
       return "LLNode [start=" + start + ", count=" + count + "]";
   }

public static void main(String[] args) {
  
   LLNode ll=new LLNode();
   ll.add("java");
   ll.add("spring");
   System.out.println(ll);
   System.out.println(ll.onThisList("spring22"));
  
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote