Activity 4. Finally, in ManageBooks.java , you will implement the following two
ID: 3698378 • Letter: A
Question
Activity 4.
Finally, in ManageBooks.java, you will implement the following two methods:
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.
public class ManageBooks {
/*********************************************************************
* A method, readFromFile, that takes a file name as a string – this
* file contains book information, and returns an array of items of
* type Book. This method should handle file-reading errors via
* exception handling.
*********************************************************************/
public static Book[] readFromFile(String filename) {
// add your code here
}
/*********************************************************************
* A method, sortByPublicationDate, that takes an array of items of
* type Book, and sorts them by publication date, from oldest to newest.
*********************************************************************/
public static void sortByPublicationDate(Book[] myBooks) {
// add your code here
}
/*********************************************************************
* A method, cheapest, that takes an array of items of type Book,
* and returns the cheapest book in the array.
*********************************************************************/
public static Book cheapest(Book[] myBooks) {
// add your code here
}
/*********************************************************************
* A method, enjoyed, that takes an array of items of type Book, and
* returns the number of books in the array that you enjoyed.
*********************************************************************/
public static int enjoyed(Book[] myBooks) {
// add your code here
}
/*********************************************************************
* A recursive method, recEnjoyed, that takes an array of items of
* type Book and an integer n, and returns the number of books in the
* array that you enjoyed, starting at index n in the array.
*********************************************************************/
public static int recEnjoyed(Book[] myBooks, int n) {
// add your code here
}
/*********************************************************************
* 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
*********************************************************************/
public static BooksLL buildLL(Book[] myBooks) {
// add your code here
}
/*********************************************************************
* A method chronoLLinsert that takes:
* a. an array A of books; and
* b. 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.
*********************************************************************/
public static void chronoLLinsert(BooksLL myBooks, Book B) {
// add your code here
}
/*********************************************************************
* A main method that allows you to test all above methods.
*********************************************************************/
public static void main(String[] args) {
// add your code here
}
}
Explanation / Answer
ManageBooks.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class ManageBooks {
/*********************************************************************
* A method, readFromFile, that takes a file name as a string – this
* file contains book information, and returns an array of items of
* type Book. This method should handle file-reading errors via
* exception handling.
*********************************************************************/
public static Book[] readFromFile(String filename) throws IOException {
// add your code here
FileReader fr = new FileReader(filename);
BufferedReader textReader = new BufferedReader(fr);
textReader.mark(1000);
int numOfLines = 0;
while (textReader.readLine()!=null) {
numOfLines++;
}
textReader.reset();
Book[] myBooks = new Book[numOfLines];
String title = "";
String authorsTemp = "";
int pages = 0;
int pubDate = 0;
double price = 0.0;
boolean enjoyed = true;
for (int i = 0; i < numOfLines; i++) {
String[] line = textReader.readLine().split(";");
title = line[0];
authorsTemp = line[1];
String[] authors = authorsTemp.split(", ");
pages = Integer.parseInt(line[2]);
pubDate = Integer.parseInt(line[3]);
price = Double.parseDouble(line[4]);
enjoyed = Boolean.parseBoolean(line[5]);
myBooks[i] = new Book (title, authors, pages, pubDate, price, enjoyed);
}
textReader.close();
return myBooks;
}
/*********************************************************************
* A method, sortByPublicationDate, that takes an array of items of
* type Book, and sorts them by publication date, from oldest to newest.
*********************************************************************/
public static void sortByPublicationDate(Book[] myBooks) {
// add your code here
int i = 0;
int j = 0;
int leastRecent = 0;
int indexLeastRecent = 0;
int tempIndex = 0;
Book tempBook = new Book();
// get most recent and swap
for (i = 0; i < myBooks.length; i++) {
leastRecent = myBooks[i].getPubDate();
indexLeastRecent = i;
for (j = i; j < myBooks.length; j++) {
if (myBooks[j].getPubDate() < leastRecent) {
leastRecent = myBooks[j].getPubDate();
indexLeastRecent = j;
}
}
//swap title
tempBook.setTitle(myBooks[i].getTitle());
myBooks[i].setTitle(myBooks[indexLeastRecent].getTitle());
myBooks[indexLeastRecent].setTitle(tempBook.getTitle());
//swap authors
tempBook.setAuthors(myBooks[i].getAuthors());
myBooks[i].setAuthors(myBooks[indexLeastRecent].getAuthors());
myBooks[indexLeastRecent].setAuthors(tempBook.getAuthors());
//swap pages
tempBook.setPages(myBooks[i].getPages());
myBooks[i].setPages(myBooks[indexLeastRecent].getPages());
myBooks[indexLeastRecent].setPages(tempBook.getPages());
//swap pubDate
tempBook.setPubDate(myBooks[i].getPubDate());
myBooks[i].setPubDate(myBooks[indexLeastRecent].getPubDate());
myBooks[indexLeastRecent].setPubDate(tempBook.getPubDate());
//swap price
tempBook.setPrice(myBooks[i].getPrice());
myBooks[i].setPrice(myBooks[indexLeastRecent].getPrice());
myBooks[indexLeastRecent].setPrice(tempBook.getPrice());
//swap enjoyed
tempBook.setEnjoyed(myBooks[i].getEnjoyed());
myBooks[i].setEnjoyed(myBooks[indexLeastRecent].getEnjoyed());
myBooks[indexLeastRecent].setEnjoyed(tempBook.getEnjoyed());
myBooks[i].Print();
System.out.println(" ");//test
}
}
/*********************************************************************
* A method, cheapest, that takes an array of items of type Book,
* and returns the cheapest book in the array.
*********************************************************************/
public static Book cheapest(Book[] myBooks) {
// add your code here
int i = 0;
int j = 0;
double cheapest = myBooks[0].getPrice();
int indexCheapest = 0;
// get most recent and swap
for (i = 1; i < myBooks.length; i++) {
if (myBooks[i].getPrice() < cheapest) {
cheapest = myBooks[i].getPrice();
indexCheapest = i;
}
}
return myBooks[indexCheapest];
}
/*********************************************************************
* A method, enjoyed, that takes an array of items of type Book, and
* returns the number of books in the array that you enjoyed.
*********************************************************************/
public static int enjoyed(Book[] myBooks) {
// add your code here
int counter = 0;
for (int i = 0; i < myBooks.length; i++) {
if (myBooks[i].getEnjoyed()) {
counter++;
}
}
return counter;
}
/*********************************************************************
* A recursive method, recEnjoyed, that takes an array of items of
* type Book and an integer n, and returns the number of books in the
* array that you enjoyed, starting at index n in the array.
*********************************************************************/
public static int recEnjoyed(Book[] myBooks, int n) {
// add your code here
int counter = 0;
if (myBooks.length == 0) {
return 0;
}
if ( myBooks[n].getEnjoyed() ) {
counter++;
}
return counter + recEnjoyed(myBooks, (n + 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
*********************************************************************/
public static BooksLL buildLL(Book[] myBooks) {
// add your code here
BooksLL firstBookOfList = new BooksLL(myBooks[0],null);
BooksLL currentBookOfList = firstBookOfList;
for (int i = 1; i < myBooks.length; i++) {
BooksLL newBookOfList = new BooksLL(myBooks[i], null);
currentBookOfList.setNext(newBookOfList);
currentBookOfList = newBookOfList;
}
return firstBookOfList;
}
/*********************************************************************
* A method chronoLLinsert that takes:
* a. an array A of books; and
* b. 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.
*********************************************************************/
/*public static void chronoLLinsert(BooksLL myBooks, Book B) {
// add your code here
}*/
/*********************************************************************
* A main method that allows you to test all above methods.
*********************************************************************/
public static void main(String[] args) throws IOException {
// add your code here
Scanner scnr = new Scanner(System.in);
String filename = "";
int i = 0;
System.out.println("Enter File Name:");
filename = scnr.nextLine();
Book[] myBooks = readFromFile(filename);
System.out.println("Printing books... ");
for (i = 0; i < myBooks.length; i++) {
myBooks[i].Print();
System.out.println(" ");
}
System.out.println("_____________________");
System.out.println("Sorting books from oldest to newest... ");
sortByPublicationDate(myBooks);
System.out.println("_____________________");
System.out.println("Printing cheapest book: ");
cheapest(myBooks).Print();
System.out.println("_____________________");
System.out.println("Number of enjoyed books: " + enjoyed(myBooks));
System.out.println("_____________________");
System.out.println("Number of enjoyed books (recursive): " + enjoyed(myBooks));
System.out.println("_____________________");
System.out.println("Building and printing linked list: ");
BooksLL myListOfBooks = buildLL(myBooks);
myListOfBooks.printLL();
System.out.println(" _____________________");
System.out.println("Counting size of linked list:");
System.out.println(myListOfBooks.sizeLL());
System.out.println(" _____________________");
System.out.println("Counting size of linked list Recursively:");
System.out.println(myListOfBooks.sizeLLR());
System.out.println(" _____________________");
System.out.println("Adding Tail to myListOfBooks and printing it:");
BooksLL newFirstListItem = new BooksLL(myBooks[0],null);
myListOfBooks.addTail(newFirstListItem);
myListOfBooks.printLL();
System.out.println(" _____________________");
System.out.println("Removing Head to myListOfBooks and printing it:");
myListOfBooks.removeHead();
myListOfBooks.printLL();
//bonus2
System.out.println(" _____________________");
System.out.println("Enter nth");
int nth = scnr.nextInt();
//myListOfBooks.addNth(myBooks[0], nth);
myListOfBooks.printLL();
}
}
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);
}
}
}
Book.java
public class Book {
// Attributes... ARE PROVIDED TO YOU:
private String title;
private String[] authors;
private int pages;
private int pubDate;
private double price;
private boolean enjoyed;
// NOW COMPLETE THE FOLLOWING SECTIONS WITH RELEVANT METHODS
// Constructors
public Book () {
}
public Book (String theTitle, String[] theAuthors, int thePages, int thePubDate, double thePrice, boolean theEnjoyed) {
title = theTitle;
authors = theAuthors;
pages = thePages;
pubDate = thePubDate;
price = thePrice;
enjoyed = theEnjoyed;
}
// Accessors
public String getTitle() {
return title;
}
public String[] getAuthors() {
return authors;
}
public int getPages() {
return pages;
}
public int getPubDate() {
return pubDate;
}
public double getPrice() {
return price;
}
public boolean getEnjoyed() {
return enjoyed;
}
// Mutators
public void setTitle(String theTitle) {
title = theTitle;
}
public void setAuthors(String[] theAuthors) {
authors = theAuthors;
}
public void setPages(int thePages) {
pages = thePages;
}
public void setPubDate(int thePubDate) {
pubDate = thePubDate;
}
public void setPrice(double thePrice) {
price = thePrice;
}
public void setEnjoyed(boolean theEnjoyed) {
enjoyed = theEnjoyed;
}
// Print method
public void Print() {
String stringPubDate = Integer.toString(pubDate);
String formattedPubDate = stringPubDate.substring(4,6) + "/" + stringPubDate.substring(6) + "/" + stringPubDate.substring(0,4);
System.out.println("Book: " + title + ", of " + pages + " pages");
System.out.println("First published on " + formattedPubDate);
System.out.println("Price: " + price);
if (enjoyed) {
System.out.println("I enjoyed it very much!");
}
if (!enjoyed) {
System.out.println("This book was not so great...");
}
}
}
z.txt
The Power of Algorithms: Inspiration and Examples in Everyday Life;Giorgio Ausiello, Rossella Petreschi;255;20131109;40.08;true
A philosophical essay on probabilities;P. S. Laplace;342;20080910;54.21;false
The obssesion;Nora Roberts;40;20151110;15.88;true
The Sixth Extinction: An Unnatural History;Elizabeth Kolbert;319;20140327;18.76;false
The Whole30: The 30-Day Guide to Total Health and Food Freedom;Melissa Hartwig, Dallas Hartwig;420;20150118;18.00;true
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.