Skip to content

Latest commit

 

History

History
68 lines (52 loc) · 2.46 KB

845d.md

File metadata and controls

68 lines (52 loc) · 2.46 KB

Back to questions

845d: Books and dictionaries

Consider the following class, which represents a book:

Book.java

Write a class Dictionary as a subclass of Book. Dictionary should provide a constructor:

public Dictionary(String isbn,
                  String title,
                  int pages,
                  String sourceLanguage,
                  String targetLanguage,
                  int numDefinitions);

that constructs the dictionary, recording which language it can translate to and from (e.g.~a Spanish-English dictionary), and the number of definitions the dictionary contains. Dictionary should also override toString() to provide a string representation of all the object's details.

Now write a class Bookshelf that can store books. Books should be stored in an object with apparent type java.util.List and actual type java.util.LinkedList. Bookshelf should provide the following methods:

// Returns the number of books on the bookshelf
public int size();

// Adds a book on the left side of the shelf.
// Shifts all other books one position to the right
public void addBookOnLeftSide(Book b);

// Adds a book on the right side of the shelf.
public void addBookOnRightSide(Book b);

// Adds book in position i counting from the left, starting
// from zero, and shifts all other books one position to the right.
// Hence addBook(0, b) is equivalent to addBookOnLeftSide(b), and
// addBook(size(), b) is equivalent to addBookOnRightSide(b)
public void addBook(int i, Book b);

// Removes book from position i, shifting all books with position
// greater than i to the left
public Book remove(int i);

// Prints all details of books from left to right
public void printLeftToRight();

// Prints all details of books from right to left
public void printRightToLeft();

Write a program that constructs a bookshelf containing six books: two dictionaries and four non-dictionaries, inserted in any order you wish, and prints the resulting bookshelf from left-to-right, and then from right-to-left.

Finally, write a static method that takes a bookshelf parameter and rearranges the books so that all the dictionaries are on the right. This method should operate using only the Bookshelf interface given above: you should not need to add any further methods to Bookshelf, or directly access any fields of a Bookshelf. Extend your demo program to show this method in action.