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

Here are four files: Book.java-Book.java is nothing out of the ordinary - it\'s

ID: 3827987 • Letter: H

Question

Here are four files:

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 next variable 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-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.

R.java-R20.java is your test program to run and show the output

top10.txt:

How to complete this project?

Explanation / Answer

Updated LinkedBookList.java file:

public class LinkedBookList {

private BookNode head;
private BookNode tail; // points to the last element in the list
private int size;
  
public LinkedBookList(){
head = null;
tail = null;
size = 0;
}
  
//returns size of the list
public int size(){
return size;
}
  
//adds a book to the end of the linked list
public void add(Book b){
int i;
BookNode temp = head;
BookNode newBook = new BookNode(b);

for(i=1;i<size;i++){
temp=temp.getNext();
}
newBook.setNext(temp.getNext());
temp.setNext(newBook);
  
size++; // Increase the size of the list
return;
}
  
//adds a book at the specific index,
// or at the end if index is greater than size
public void add(Book b, int index){
int i;
BookNode temp = head;
BookNode newBook = new BookNode(b);
if(index>size){
add(b);
} else {
for(i=1;i<index;i++){
temp=temp.getNext();
}
newBook.setNext(temp.getNext());
temp.setNext(newBook);
size++; // Increase the size of the list
}
return;
}
  
//removes a book and returns it, or
// returns null if book is not present
public Book remove(Book b){
int i;
BookNode temp = head;
BookNode prev;
if(head==null)
return null;
else {
if(b.equals(temp.getBook())){
head = head.getNext();
} else{
for(i=1;i<size;i++){
prev = temp;
temp = temp.getNext();
if(b.equals(temp.getBook())){
prev.setNext(temp.getNext());
break;
}
}
}
size--;
return b;
}
}

//Removes a book at a specific index and returns it,
// or returns null if index is not present
public Book remove(int index){
int i;
BookNode temp = head;
BookNode prev = null;
if(index>size){
return null;
}else {
for(i=1;i<index;i++){
prev = temp;
temp = temp.getNext();
}
prev.setNext(temp.getNext());
}
size--;
return temp.getBook();
}
  
//returns the total number of pages in the linked list
public int totalPages(){
int i, totalPages=0;
BookNode temp = head;
for(i=1;i<=size;i++){
totalPages+=temp.getNumPages();
temp = temp.getNext();
}
return totalPages;
}
  

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;
}

}

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