Class Program4


  • public class Program4
    extends Object
    Question 4. ArrayLists of Objects [32 Marks] This question concerns a program for tracking the books in a collection, as well as their rating (from 0-5). Answer this question in this class. The program has two classes: this class and the Book class. This class has a collection field which contains an ArrayList of Book objects. The Book class defines Book objects, which store the title, author, and rating of the book. - The title and author are both strings, such as ”Harrow the Ninth”, or ”Tamsyn Muir” - The rating is stored as a double (to allow for half values), but is always between 0 and 5.
    • Constructor Summary

      Constructors 
      Constructor Description
      Program4()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean addBook​(String title, String author, double rating)
      (b) [17 marks] Complete the addBook(...) method so that it will create a book with the title, author, and rating specified in the parameters, and then add the book to the collection if and only if: - The rating is valid (between 0 and 5 inclusive) - A book with that title is not already in the collection The method should return true if the book was successfully added, or false otherwise.
      void createTestData()
      This method will add some data to the Schedule field so you can test your code.
      void doAddBook()  
      static void main​(String[] args)  
      void printCollection()
      This method prints out the current contents of the schedule.
      void searchByTitleOrAuthor​(String keyword)
      (a) [15 marks] Complete the searchByTitleOrAuthor(...) method that will search the collection to print the information for all books that have titles or authors that contain the text specified in the parameter.
      void setupGUI()  
    • Constructor Detail

      • Program4

        public Program4()
    • Method Detail

      • searchByTitleOrAuthor

        public void searchByTitleOrAuthor​(String keyword)
        (a) [15 marks] Complete the searchByTitleOrAuthor(...) method that will search the collection to print the information for all books that have titles or authors that contain the text specified in the parameter. For example, searchByTitleOrAuthor(”Time”) should print any books that contain ”time” in their title or author, eg: ’This Is How You Lose the Time War’, by Amal El-Mohtar and Max Gladstone (5.0/5.0) ’A Brief History of Time’, by Stephen Hawking (4.2/5) If no matching books are found, this method should display a ”no matching books found” message.
      • addBook

        public boolean addBook​(String title,
                               String author,
                               double rating)
        (b) [17 marks] Complete the addBook(...) method so that it will create a book with the title, author, and rating specified in the parameters, and then add the book to the collection if and only if: - The rating is valid (between 0 and 5 inclusive) - A book with that title is not already in the collection The method should return true if the book was successfully added, or false otherwise.
      • createTestData

        public void createTestData()
        This method will add some data to the Schedule field so you can test your code.
      • printCollection

        public void printCollection()
        This method prints out the current contents of the schedule.

        It is provided for your convenience.

      • doAddBook

        public void doAddBook()
      • setupGUI

        public void setupGUI()
      • main

        public static void main​(String[] args)