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

Create a project and download the following files into that project: - Book.java

ID: 3830114 • Letter: C

Question

Create a project and download the following files into that project:

- Book.java:

- LinkedBookList.java:

- BookNode.java:

- R20.java:

File Descriptions

Book.java

Book.java is nothing out of the ordinary - it's just keeps track of the book's title and its author, as well as the number of pages. It has a few getter and setter methods, but overall this class is very straight forward.

BookNode.java

BookNode.java is a little more interesting. A BookNode is an object that stores a Book in its book instance variable, and has a reference to the next BookNode in its next instance variable. If the node is the last one in the sequence, its nextvariable will be set to null. There are also methods for setting the next node and getting the next node, as well as a method for returning the book that the node contains.

LinkedBookList.java

Now let's look at the LinkedBookList class. You'll see that the instance variables for this are very simple. Only a BookNode called head, which refers to the first item, and an int to keep track of the size of the linked list. This is all we need, since each element contains a reference to the next one.

R20.java

R20.java is your test program

Your task is to implement the missing add, totalPages, and remove methods per the instructions.

All these methods will require you to iterate through the list. You can do this either with a while loop or a for loop. If using a for loop, think what are the three components that make up the loop:

Many of the operations that we are trying to implement here, can be reasoned through by drawing pictures. Use that to reason how far to go, when to set which pointers, and when to delete pointers.

Explanation / Answer

Answer:

Note: User given code is modified.

File Name: Book.java:

File Name: LinkedBookList.java

public class LinkedBookList {

        private BookNode head;

        private int size;

       

        public LinkedBookList(){

                head = null;

                size = 0;

        }

       

        //returns size of the list

        public int size(){

                return size;

        }

       

        //IMPLEMENT -- adds a book to the end of the linked list

        public void add(Book b){

                add(b,size);

        }

       

        //IMPLEMENT -- adds a book at the specific index,

        // or at the end if index is greater than size

        public void add(Book b, int index){

                BookNode bb=new BookNode(b);

          BookNode tpp=head;

          if(index==0)

          {

              bb.setNext(head);

              this.head=bb;

          }

          else

          {

              for(int kk=1;kk<index;kk++)

              {

                   tpp=tpp.getNext() ;

              }

              bb.setNext(tpp.getNext())         ;

              tpp.setNext(bb);

          }

          size++;

         

        }

       

        //IMPLEMENT -- removes a book and returns it, or

        //      returns null if book is not present

        public Book remove(Book b){

               BookNode bb=head;

          if(head==null)

              return null;

          int indx=-1;

          int tpp=0;

          while(bb!=null)

          {

              Book mybk=bb.getBook() ;

              if(b.getAuthor().equals(mybk.getAuthor())

              {

                   indx=tpp;

                   break;

              }

              else

              {

                   bb=bb.getNext();

                   tpp++     ;

              }

          }

          return remove(indx);

         

        }

        //IMPLEMENT -- removes a book at a specific index and returns it,

        //      or returns null if index is not present

        public Book remove(int index){

                if(index<0)

              return null;

          else if(index>=size)

              return null;

          BookNode tpp=head;

          BookNode ptr=null;

          if(index==0)

          {

              head=head.getNext();

             

          }

          else

          {

          for(int kk=1;kk<index;kk++)

          {

              tpp=tpp.getNext() ;

          }

          tpp.setNext(tpp.getNext());

          }

          size--;

          return tpp;

               

        }

       

        //IMPLEMENT -- returns the total number of pages in the linked list

        public int totalPages(){

                int res = 0;

                for (BookNode pos = head; pos != null; pos = pos.getNext())

          {

                        if (pos.getBook() != null)

              {

                           Book b=pos.getBook();

                   res += b.getNumPages();    

                        }

                }

                return res;

        }

       

        public String toString()

        {

                String res = "";

                for (BookNode pos = head; pos != null; pos = pos.getNext()) {

                        if (pos.getBook() == null) {

                                res += "null";

                        } else {

                                res += pos.getBook();

                        }

                        if (pos.getNext() != null) res += " ";

                }

                return res;

        }

}

File Name: BookNode.java:

File Name: R20.java:

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