Skip to content

Instantly share code, notes, and snippets.

@bhnascar
Last active March 23, 2016 11:24
Show Gist options
  • Save bhnascar/0aa461f9c11ba13f1d7b to your computer and use it in GitHub Desktop.
Save bhnascar/0aa461f9c11ba13f1d7b to your computer and use it in GitHub Desktop.
Solutions to last two problems
/* Returns all books in this library that are
* written by the given author.
*
* @param author - the author.
*/
public ArrayList<Book> getBooksByAuthor(String author) {
// Two comments:
//
// (1) Your solution is pretty close but it returns
// a SINGLE book by the given author, not all
// the books by the given author.
//
// (2) Note that the return type has to be ArrayList<Book>
// because the method definition is
// public ArrayList<Book> getBooksByAuthor
ArrayList<Book> booksByAuthor = new ArrayList<Book>();
// Then we find the the books with the same author...
for (int i = 0; i < books.size(); i++) {
Book book = books.get(i);
if (book.getAuthor().equals(author)) {
//...and add each valid book to our ArrayList of
// books by that author.
booksByAuthor.add(book);
}
}
// And then we return the whole ArrayList - so we return
// all books by that author.
return booksByAuthor;
}
/* Returns the title of the longest book (in pages)
* with the given genre.
*
* @param author - the author.
*/
public String getLongestBookWithGenre(String genre) {
// A few comments:
//
// This was probably the trickiest one..you were kinda close. :)
//
// (1) In this case, you can't set the longest book variable to
// books.get(0). This is because the Book at books.get(0)
// might not have the correct genre. This is where "null"
// is a good idea because it's a placeholder value that
// stands for the fact you don't have anything yet.
//
// (2) Also books.get(0) gives you a Book, not a String,
// since books is an ArrayList of Books. So the variable
// type is Book, not String.
Book longest = null;
// Then you have to loop through all the books
for (int i = 0; i < books.size(); i++) {
Book book = books.get(i);
// (3) Now we have to check a few things here. A book is a
// valid candidate for the longest book IF it has the
// correct genre AND it has more pages than our previous
// candidate for the longest book.
//
// Alternatively, if we DON'T currently have a candidate
// for the longest book (remember 'longest' starts out with
// the placeholder value of 'null'), then we simply have to
// check that the book has the right genre.
//
if (book.getGenre().equals(genre) &&
(longest == null || book.getNumberOfPages() > longest.getNumberOfPages()))
{
longest = book;
}
// (4) Beware of an early return statement!
// The return statement needs to go outside the
// for loop because you need to look at every single book.
// in order to find which one is the longest.
//
// In the other questions I asked, it was okay to have a
// return statement inside the for-loop because you only
// had to find a single book that met the critera (like
// a book that had a given author or a given title).
// Once you found that book, you didn't have to keep checking
// the rest of the books.
//
// However, if the criteria is something like the "longest"
// book (out of all the books), then you obviously have to check
// every single book. So the return statement must go outside.
}
if (longest != null) {
return longest.getTitle();
}
else {
// (5) Maybe none of the books in the library had the correct
// genre. In thise case, 'longest' is still 'null'.
// Since we didn't find anything, a good value to return
// is null, which is a placeholder value that basically
// means blank aka nothing aka...uhh you get the idea.
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment