Ok, I can not figure this out for the life of me. Program has been primarily wri
ID: 3792209 • Letter: O
Question
Ok, I can not figure this out for the life of me. Program has been primarily written for us and we can only change certain code. I would appreciate any help, I think this is where the problem is
public class BookList {
// TODO Convert the ArrayList from "raw" to an appropriate type
private BookList bookList = new BookList();
I have no error in the code above but I can not get the next two to work, it says the error is with the add, which I can not change and the binarySearch which I can not change.
public void sortBooks() {
// Call Collections.sort which will use compareTo
// TODO add code here
Collections.sort(Object);
public Book findBook(Book book) {
// Call Collections.binarySearch
// TODO Add code here
Collections.binarySearch(bookList, title);
// If the book is found, return it
// TODO Add code here
return book;
// If not, return null
return null;
Explanation / Answer
//Book.java
public class Book {
String title,author; // Attributes
int pages;
double cost;
//Setters and getters
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
}
//BookComparator.java
import java.util.Comparator;
//Comparator of Type Book
public class BookComparator implements Comparator<Book>{
public int compare(Book b1, Book b2) { // compare method compares two book titles
if(b1.getTitle().compareTo(b2.getTitle())>0) // Titles Comparision
return 1;
else if(b1.getTitle().compareTo(b2.getTitle())<0)
return -1;
else
return 0;
}
}
//BookList.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class BookList {
List<Book> bookList = new ArrayList<Book>(); // List of Type Book
public void addBook(Book book){
bookList.add(book); // Adding Book object to List
}
public void sortBooks(){
Collections.sort(bookList, new BookComparator()); // Comparator comparing books using sort method
System.out.println("After Sorting Books..");
System.out.println("**************************");
Iterator<Book> itr = bookList.iterator();
while(itr.hasNext()){ // Iterating books using Iterator
Book bkk = itr.next();
System.out.println(bkk.getTitle()+" "+bkk.getAuthor()+" "+bkk.getCost()+" "+bkk.getPages());
}
System.out.println("**************************");
}
public Book findBook(Book book){ //Searching book using binarySearch
int value = Collections.binarySearch(bookList,book,new BookComparator());
if(value>-1){ // If book is found
Book bk = new Book(); // Storing the info of book in bk object
Iterator<Book> itr = bookList.iterator();
while(itr.hasNext()){
Book b = itr.next();
if(b.getTitle().equals(book.getTitle())){ // Setting book Details Using Iterator
bk.setAuthor(b.getAuthor());
bk.setTitle(b.getTitle());
bk.setPages(b.getPages());
bk.setCost(b.getCost());
}
}
return bk; // If found return book object
}
else
return null; // Else Null
}
public static void main(String[] args) {
Book book1 = new Book(); // Creating book objects
book1.setTitle("JavaSE");book1.setAuthor("James");
book1.setPages(145);book1.setCost(280);
Book book2 = new Book();
book2.setTitle("JavaME");book2.setAuthor("Jack");
book2.setPages(241);book2.setCost(287);
Book book3 = new Book();
book3.setTitle("JavaEE");book3.setAuthor("Peter");
book3.setPages(245);book3.setCost(270);
Book book4 = new Book();
book4.setTitle("JavaServerFaces");book4.setAuthor("Mary");
book4.setPages(200);book4.setCost(279);
BookList bList = new BookList();
bList.addBook(book1); bList.addBook(book2); // Adding books to List
bList.addBook(book3); bList.addBook(book4);
Scanner s = new Scanner(System.in);
bList.sortBooks(); // Calling Sorting method to sort books
System.out.println("Enter Book Title,Author,Cost and Pages:");
Book key = new Book(); // Finding book using key object
key.setTitle(s.next());
//key.setAuthor(s.next());
//key.setCost(s.nextDouble());
//key.setPages(s.nextInt());
Book b = bList.findBook(key); // Passing Book to search
if(b!=null){
System.out.println("The Book Details are :"); // Printing Book details when book found
System.out.println("Book Title:"+b.getTitle());
System.out.println("Book Author:"+b.getAuthor());
System.out.println("Book Pages:"+b.getPages());
System.out.println("Book Cost:"+b.getCost());
}
else
{
System.out.println("Book Not found...");
}
}
}
Output:
After Sorting Books..
**************************
JavaEE Peter 270.0 245
JavaME Jack 287.0 241
JavaSE James 280.0 145
JavaServerFaces Mary 279.0 200
**************************
Enter Book Title to Find:
JavaEE
The Book Details are :
Book Title:JavaEE
Book Author:Peter
Book Pages:245
Book Cost:270.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.