Assignment Books, java I really need some help on how to do this assignment. Her
ID: 3782294 • Letter: A
Question
Assignment Books, java I really need some help on how to do this assignment.
Here is the CSV information
The Goldfinch Donna Tartt 2014 The Orphan Master's Son Adam Johnson 2013 No Pulitzer prize for fiction was awarded in 2012 A Visit from the Goon Squad Jennifer Egan 2011 Tinkers Paul Harding 2010 Olive Kitteridge Elizabeth Strout 2009 The Brief Wondrous Life of Oscar Wao Junot Diaz 2008 The Road Cormac McCarthy 2007 March Geraldine Brooks 2006 Gilead Marilynne Robinson 2005 The Known World Edward P. Jones 2004 Middlesex Jeffrey Eugenides 2003 Empire Falls Richard Russo 2002 The Amazing Adventures of Kavalier & Clay Michael Chabon 2001 Interpreter of Maladies Jhumpa Lahiri 2000 The Hours Michael Cunningham 1999 Assignment Books CSIS-2420 Turn inc Learning objectives urn in the ass plementing the interface Canvas programming against interfaces not objects. review reading data from a file use the internet to learm about the interface Comparator that is in class Collections List the book in the newly reversed orderExplanation / Answer
// Book.java
package books;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
public class Book implements Comparable<Book>{
private String title;
private String author;
private int year;
public Book(String title, String author, int year)
{
this.title = title;
this.author = author;
this.year = year;
}
// copy constructor
public Book(Book book)
{
this.title = book.title;
this.author = book.author;
this.year = book.year;
}
public Book()
{
// Null constructor
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getYear() {
return year;
}
public List<Book> getList()
{
BufferedReader br = null;
FileReader fr = null;
String FILENAME = "book.csv";
List<Book> bookList = new ArrayList<Book>();
try{
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
String[] lineParts = sCurrentLine.split(",");
if (lineParts.length == 3 &&
(! lineParts[0].isEmpty()) &&
(! lineParts[1].isEmpty()) &&
(! lineParts[2].isEmpty()))
{
Book b = new Book(lineParts[0], lineParts[1], Integer.parseInt(lineParts[2]));
bookList.add(b);
}
else
{
System.err.println("Problem reading in "" + sCurrentLine + """);
}
}
}
catch (Exception e)
{
System.out.println("Issue in reading file: " + e.toString());
}
return bookList;
}
@Override
public String toString() {
return title + " by " + author + " (" + year + ")";
}
@Override
public int compareTo(Book o) {
return this.getTitle().compareTo(o.getTitle());
}
}
// BookApp.java
package books;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Comparator;
public class BookApp {
public static void main(String[] args)
{
Book b = new Book();
List<Book> bookList = b.getList();
System.out.println("Number of books read in: " + bookList.size());
Collections.sort(bookList);
System.out.println();
System.out.println("Sorted book list:");
for(Book book: bookList)
{
System.out.println(book);
}
//Comparator for reverse order.
Comparator comparator = Collections.reverseOrder();
Collections.sort(bookList, comparator);
System.out.println();
System.out.println("Reverse order:");
for(Book book: bookList)
{
System.out.println(book);
}
}
}
/*
Sample run
Problem reading in "No Pulitzer prize for fiction was awarded in 2012,, "
Number of books read in: 15
Sorted book list:
A Visit from the Goon Squad by Jennifer Egan (2011)
Empire Falls by Richard Russo (2002)
Gilead by Marilynne Robinson (2005)
Interpreter of Maladies by Jhumpa Lahiri (2000)
March by Geraldine Brooks (2006)
Middlesex by Jeffrey Eugenides (2003)
Olive Kitteridge by Elizabeth Strout (2009)
The Amazing Adventures of Kavalier & Clay by Michael Chabon (2001)
The Brief Wondrous Life of Oscar Wao by Junot Diaz (2008)
The Goldfinch by Donna Tartt (2014)
The Hours by Michael Cunningham (1999)
The Known World by Edward P. Jones (2004)
The Orphan Master's Son by Adam Johnson (2013)
The Road by Cormac McCarthy (2007)
Tinkers by Paul Harding (2010)
Reverse order:
Tinkers by Paul Harding (2010)
The Road by Cormac McCarthy (2007)
The Orphan Master's Son by Adam Johnson (2013)
The Known World by Edward P. Jones (2004)
The Hours by Michael Cunningham (1999)
The Goldfinch by Donna Tartt (2014)
The Brief Wondrous Life of Oscar Wao by Junot Diaz (2008)
The Amazing Adventures of Kavalier & Clay by Michael Chabon (2001)
Olive Kitteridge by Elizabeth Strout (2009)
Middlesex by Jeffrey Eugenides (2003)
March by Geraldine Brooks (2006)
Interpreter of Maladies by Jhumpa Lahiri (2000)
Gilead by Marilynne Robinson (2005)
Empire Falls by Richard Russo (2002)
A Visit from the Goon Squad by Jennifer Egan (2011)
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.