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

PSEUDOCODE!- 1/ A method buildLL that takes an array of items of type Book, and

ID: 3696883 • Letter: P

Question

PSEUDOCODE!-

1/ A method buildLL that takes an array of items of type Book, and returns a linked list of all the Book items in the array.

2/ A method chronoLLinsert that takes:

an array A of books; and

an extra book B,

and returns a linked-list of all of the items in A in chronological order of their publication date (from oldest to newest) as well as book B inserted at the right chronological position to ensure that the resulting list is sorting in ascending chronological order.

Hint: you will need to use the method sortByPublicationDate and the method addTail.

Explanation / Answer

Hi Previously I have created my own Linked List of Book.

Here I have used LinkedList class from Collection Framework. That is more easy.

############### Book.java #######################3

import java.util.ArrayList;
import java.util.Date;

public class Book implements Comparable<Book>{
  
   private int pages;
   private double price;
   private boolean enjoyed;
   private ArrayList<String> authors;
   private String title;
   private Date date;
  
   // Default constructor
   public Book() {
      
   }

   /**
   * @param pages
   * @param price
   * @param enjoyed
   * @param authors
   * @param title
   * @param date
   */
   public Book( String title,ArrayList<String> authors,int pages,String date, double price, boolean enjoyed) {
       super();
       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;
   }

   /**
   * @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;
   }
  
   // 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…");
   }
  
   private String getDateString(){
       return ""+date.getYear()+""+date.getMonth()+""+date.getDate();
   }

   // compare method based on Published method
   @Override
   public int compareTo(Book o) {
       String d1 = getDateString();
       String d2 = getDateString();
      
       return d1.compareTo(d2);
   }
}

##################### BooksLL.java #####################

I have added all required methods.

import java.util.Collections;
import java.util.LinkedList;

import firstWeek.Book;

public class BooksLL {

   private LinkedList<Book> books;

   public BooksLL() {
       books = new LinkedList<Book>();
   }

   public void printLL() {
       for (Book book : books)
           book.print();
   }

   public int sizeLL() {
       return books.size();
   }

   public int sizeLLR() {
       return sizeLLR(0);
   }

   // helper function
   public int sizeLLR(int index) {
       if (index == books.size())
           return 0;
       return 1 + sizeLLR(index + 1);
   }

   public void removeHead() {
       if (books == null)
           return;
       books.removeFirst();
   }

   public void addTail(Book book) {
       books.addLast(book);
   }

   public void addNth(Book book, int n) {
       if (n > books.size()) {
           books.addLast(book);
       } else {
           books.add(n, book);
       }
   }
  
   public void sortByPublicationDate(){
       Collections.sort(books);
   }
  
   public LinkedList<Book> buildLL(Book[] books){
       if(books == null)
           return null;
       LinkedList<Book> booksLL = new LinkedList<Book>();
       for(Book book: books)
           booksLL.add(book);
       return booksLL;
   }
  
   public void chronoLLinsert(Book book){
      
       sortByPublicationDate(); // first sort
      
       for(int i=0; i<books.size(); i++){
           if(books.get(i).getDate().compareTo(book.getDate()) < 0)
               i++;
           books.add(i, book);
       }
   }

}