//QuickSort Shell Sort and Linear Probing package Quiz_Sort; /** * Library. * *
ID: 654391 • Letter: #
Question
//QuickSort Shell Sort and Linear Probing
package Quiz_Sort;
/**
* Library.
*
* A collection of Books implemeted as a
* linear probing hash table.
*
*/
public class Library
{
private Book[] books;
private int count;
private final static int INITIAL_SIZE = 1361;
public Library()
{
books = new Book[Library.INITIAL_SIZE];
}
/**
* add.
*
* Add Book b to the hash table.
*
* Use linear probing.
*
* Do not worry about resizing the table or rehashing.
*
* You must implement a linear probing hash table
* to get any credit.
*
* @param b the Book to add
*/
public void add(Book b)
{
}
/**
* getUnsortedBookList.
*
* Return an array of every book in the hash
* table not sorted. This is a helper function
* for the sorting methods.
*
* @return
*/
private Book[] getUnsortedBookList()
{
// YOUR CODE HERE
}
/**
* getBooksSortedByPrice.
*
* Return a list of all books in
* the hash table sorted by price
* from least at index 0 to
* greatest at the last index.
*
* You must implement shell sort
* to get any credit.
*
* @return a list of sorted books
*/
public Book[] getBooksSortedByPrice()
{
// YOUR CODE HERE
}
/**
* getBooksSortedByTitle.
*
* Return a list of all books in
* the hash table sorted by title
* alphabetically.
*
* You must implement quick sort
* to get any credit.
*
* @return a list of sorted books
*/
public Book[] getBooksSortedByTitle()
{
// YOUR CODE HERE
}
}
////////////////////
public class Tester
{
public static void main(String[] args)
{
Library books = new Library();
books.add(new Book("Pride and Prejudice", 4.29));
books.add(new Book("The Shining", 5.99));
books.add(new Book("Hyperion", 9.99));
books.add(new Book("The Hobbit", 7.99));
books.add(new Book("The Goldfinch", 8.99));
books.add(new Book("A Room with a View", 3.99));
books.add(new Book("Emma", 4.99));
Book[] sortedByPrice = books.getBooksSortedByPrice();
System.out.println("Actual: " + sortedByPrice[0].getTitle());
System.out.println("Expected: A Room with a View");
System.out.println("Actual: " + sortedByPrice[1].getTitle());
System.out.println("Expected: Pride and Prejudice");
System.out.println("Actual: " + sortedByPrice[6].getTitle());
System.out.println("Expected: Hyperion");
Book[] sortedByTitle = books.getBooksSortedByTitle();
System.out.println("Actual: " + sortedByTitle[0].getTitle());
System.out.println("Expected: A Room with a View");
System.out.println("Actual: " + sortedByTitle[1].getTitle());
System.out.println("Expected: Emma");
System.out.println("Actual: " + sortedByTitle[6].getTitle());
System.out.println("Expected: The Shining");
}
}
/**
* Book.
*
* This class stores the pertinent information
* about a book.
*
* This class also acts as an element in
* a linked list. See the "next" property.
*
*/
public class Book implements Comparable<Book>
{
private String title;
private double price;
public Book next;
public Book(String title, double price)
{
this.title = title;
this.price = price;
next = null;
}
public String getTitle()
{
return title;
}
public double getPrice()
{
return price;
}
@Override
public String toString()
{
String summary = String.format("Book[title=%s, price=%.2f]", this.title, this.price);
return summary;
}
@Override
public int hashCode()
{
return this.title.hashCode() * (int)(23 * this.price);
}
@Override
public int compareTo(Book o)
{
if( this.getPrice() < o.getPrice() ) return -1;
else if ( this.getPrice() > o.getPrice() ) return 1;
return 0;
}
}
Explanation / Answer
import java.io.*;
public class Library
{
private ArrayList<Book> allBook = new ArrayList<Book>();
public Library(ArrayList<Book> other)
{
if (other == null) {
throw new NullPointerException("null pointer");
} else
this.allBook = other;
}
public Library() {
this.allBook = new ArrayList<Book>();
}
public boolean add(Book book)
{
if (book != null && !book.equals(""))
{
throw new IllegalArgumentException("Can't be empty");
}
allBook.add(book);
return true;
}
public ArrayList<Book> findTitles(String title)
{
for(Book b: allBook) {
if(title.compareTo(b.getTitle())== 0)
{
return allBook;
}
}
return null;
}
public void sort()
{
Collections.sort(allBook);
}
public String toString()
{
return Library.this.toString();
}
}
public class Book implements Comparable<Book>
{
private String bookTitle;
private ArrayList<String> bookAuthor;
public Book(String title, ArrayList<String> authors)
{
if(title == null && authors == null)
{
throw new IllegalArgumentException("Can't be null");
}
if(title.isEmpty() && authors.isEmpty())
{
throw new IllegalArgumentException("Can't be empty");
}
bookTitle = title;
bookAuthor = authors;
}
public String getTitle()
{
return bookTitle;
}
public ArrayList<String> getAuthors( )
{
return bookAuthor;
}
public String toString( )
{
return bookTitle + bookAuthor;
}
public int compareTo(Book other)
{
return bookTitle.compareTo(other.bookTitle);
}
public boolean equals(Object o)
{
if(!(o instanceof Book))
{
return false;
}
Book b = (Book) o;
return b.bookTitle.equals(bookTitle)
&& b.bookAuthor.equals(bookAuthor);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.