JAVA!!- In this activity, you will have to complete the implementation of anothe
ID: 3696879 • Letter: J
Question
JAVA!!-
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).
Explanation / Answer
Hi I have implemented Books.java (Linked List Node for each Book) and BooksLL.java
##################### Books.java ###########################
import java.util.ArrayList;
import java.util.Date;
public class Books {
private int pages;
private double price;
private boolean enjoyed;
private ArrayList<String> authors;
private String title;
private Date date;
private Books next;
/**
* @param pages
* @param price
* @param enjoyed
* @param authors
* @param title
* @param date
*/
public Books( String title,ArrayList<String> authors,int pages,String date, double price, boolean enjoyed) {
this.pages = pages;
this.price = price;
this.enjoyed = enjoyed;
this.authors = authors;
this.title = title;
// getting year, month and day from string
int year = Integer.parseInt(date.substring(0, 4));
int month = Integer.parseInt(date.substring(4, 6));
int day = Integer.parseInt(date.substring(6));
this.date = new Date(year, month, day);
}
/**
* @return the pages
*/
public int getPages() {
return pages;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
/**
* @return the enjoyed
*/
public boolean isEnjoyed() {
return enjoyed;
}
/**
* @return the authors
*/
public ArrayList<String> getAuthors() {
return authors;
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @return the date
*/
public Date getDate() {
return date;
}
/**
* @return the next
*/
public Books getNext() {
return next;
}
/**
* @param pages the pages to set
*/
public void setPages(int pages) {
this.pages = pages;
}
/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}
/**
* @param enjoyed the enjoyed to set
*/
public void setEnjoyed(boolean enjoyed) {
this.enjoyed = enjoyed;
}
/**
* @param authors the authors to set
*/
public void setAuthors(ArrayList<String> authors) {
this.authors = authors;
}
/**
* @param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @param date the date to set
*/
public void setDate(Date date) {
this.date = date;
}
/**
* @param next the next to set
*/
public void setNext(Books next) {
this.next = next;
}
// print method
public void print(){
System.out.println("Book: "+title+", of "+pages+" pages");
System.out.print("authors: {");
for(int i=0; i<authors.size(); i++){
System.out.print("""+authors.get(i)+""");
if(i != authors.size()-1)
System.out.print(", ");
}
System.out.println(" }");
System.out.println("First published on "+ date.getMonth()+"/"+date.getDate()+"/"+date.getYear());
System.out.println("Price: $"+String.format("%.2f", price));
if(enjoyed)
System.out.println("I enjoyed it very much!");
else
System.out.println("This book was not so great…");
}
}
######################## BooksLL.java ##############################
public class BooksLL {
private Books head, tail;
private int size;
public BooksLL() {
head = tail = null;
size = 0;
}
public void printLL(){
Books temp = head;
while(temp != null){
temp.print();
temp = temp.getNext();
}
}
public int sizeLL(){
return size;
}
public int sizeLLR(){
Books temp = head;
return sizeLLR(temp);
}
// helper function
public int sizeLLR(Books temp){
if(temp == null)
return 0;
return 1 + sizeLLR(temp.getNext());
}
public void removeHead(){
if(head == null)
return;
head = head.getNext();
}
public void addTail(Books book){
if(head == null){
head = book;
tail = head;
}else{
tail.setNext(book);
tail = tail.getNext();
}
}
public void addNth(Books book, int n){
if(n > size){
if(head == null){
head = book;
tail = head;
}else{
tail.setNext(book);
tail = tail.getNext();
}
}else{
Books temp = head;
for(int i=1; i<n; i++){
temp = temp.getNext();
}
book.setNext(temp.getNext());
temp.setNext(book);
}
}
}
Please let me know in case of any issue.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.