Activity 3. Activity 2 assumes that you know exactly how many books you will hav
ID: 3698376 • Letter: A
Question
Activity 3.
Activity 2 assumes that you know exactly how many books you will have to handle. In particular, it assumes that what you read from the file is final: nothing should be added or removed from it.
In Activity 3, we will implement tools for the forgetful user J. We will assume that 1/ the user asks the computer to use the readFile method, 2/ sortByPublicationDate, and 3/ he/she remembers that he/she forgot a book. At that point, since we do not want to copy the content of the current array of books to another array (this might happen more than once and we do not want to risk the cost), we will need to become more flexible, i.e., use a linked list of books.
In this activity, you will have to complete the implementation of another user-defined type: BooksLL, in BooksLL.java. This includes all necessary constructors, accessors (one per attribute), mutators (one per attribute), and the following methods:
A (non static) method, printLL, that prints a linked list;
A (non static) method, sizeLL, that returns the number of items in the list;
A (non static) recursive method, sizeLLR, that returns the number of items in the list;
Bonus: A (non static) method, removeHead, that modifies the original list by cropping out its first node;
A (non static) method, addTail, that takes a new book B and modifies the original list where B has been added as the last node in the list.
Bonus: A (non static) method, addNth, that takes a new book B and an integer n, and modifies the original list where B has been added as the nth node in the list (or at the end of the list if n is larger than the size of the list + 1).
public class BooksLL {
// Attributes ARE GIVEN TO YOU...
private Book mybook;
private BooksLL next;
// NOW COMPLETE THE FOLLOWING SECTIONS WITH RELEVANT METHODS
// Constructors
// Accessors
// Mutators
/*******************************************************************
* Other methods
*******************************************************************/
// A (non static) method, printLL, that prints a linked list
public void printLL() {
// add your code here
}
// A (non static) method, sizeLL, that returns the number of items in
// the list
public int sizeLL() {
// add your code here
}
// A (non static) recursive method, sizeLLR, that returns the number
// of items in the list
public int sizeLLR() {
// add your code here
}
// A (non static) method, addTail, that takes a new book B and modifies
// the original list where B has been added as the last node in the list.
public void addTail(BooksLL B) {
// add your code here
}
// Bonus 1: A (non static) method, removeHead, that modifies the original list by cropping out its first node
// Bonus 2: A (non static) method, addNth, that takes a new book B and
// an integer n, and modifies the original list where B has been added
// as the nth node in the list (or at the end of the list if n is larger
// than the size of the list + 1).
}
Explanation / Answer
BooksLL.java
public class BooksLL {
// Attributes ARE GIVEN TO YOU...
private Book myBook;
private BooksLL next;
// NOW COMPLETE THE FOLLOWING SECTIONS WITH RELEVANT METHODS
// Constructors
public BooksLL () {
}
public BooksLL (Book theMyBook, BooksLL theNext) {
myBook = theMyBook;
next = theNext;
}
public BooksLL (Book theMyBook) {
myBook = theMyBook;
next = null;
}
// Accessors
public Book getMyBook() {
return myBook;
}
public BooksLL getNext() {
return next;
}
// Mutators
public void setMyBook (Book theMyBook) {
myBook = theMyBook;
}
public void setNext (BooksLL theNext) {
next = theNext;
}
/*******************************************************************
* Other methods
*******************************************************************/
// A (non static) method, printLL, that prints a linked list
public void printLL() {
// add your code here
if (myBook != null) {
myBook.Print();
System.out.println(" ");
}
if(next != null) {
next.printLL();
}
}
// A (non static) method, sizeLL, that returns the number of items in
// the list
public int sizeLL() {
//add your code here
BooksLL temp = this;
int counter = 0;
while (temp != null) {
temp = temp.next;
counter++;
}
return counter;
}
// A (non static) recursive method, sizeLLR, that returns the number
// of items in the list
public int sizeLLR() {
// add your code here
BooksLL temp = this;
int counter = 0;
if (temp.next == null) {
return counter + 1;
}
counter++;
return counter + next.sizeLLR();
}
// A (non static) method, addTail, that takes a new book B and modifies
// the original list where B has been added as the last node in the list.
public void addTail(BooksLL B) {
// add your code here
BooksLL temp = this;
while (temp.getNext() != null) {
temp = temp.next;
}
temp.next = B;
return;
}
// Bonus 1: A (non static) method, removeHead, that modifies the original list by cropping out its first node
public void removeHead () {
this.setMyBook(getNext().getMyBook());
this.setNext(getNext().getNext());
}
// Bonus 2: A (non static) method, addNth, that takes a new book B and
// an integer n, and modifies the original list where B has been added
// as the nth node in the list (or at the end of the list if n is larger
// than the size of the list + 1).
public void addNth (BooksLL B, int n) {
int sizeOfThisList = this.sizeLLR();
BooksLL tempCurr = null;
if (n > sizeOfThisList) {
addTail(B);
}
else if (n == 1) {
//add head
B.setNext(this);
}
else {
//iterate
for (int i = 2; i < n; i++) {
System.out.println("enteredfor");
tempCurr = next;
}
//insert
System.out.println("exitedfor. tempCurr:"); tempCurr.printLL();
System.out.println("---");;
B.setNext(tempCurr.next);
tempCurr.setNext(B);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.