Question

In: Computer Science

Create “New Class…” named Book to store the details of a book Declare 3 fields for...

  • Create “New Class…” named Book to store the details of a book
  • Declare 3 fields for the details of the book:
  • field named author of type String
  • field named title of type String
  • field named callNumber of type String
  • Add overloaded constructors with the following headers and make sure each constructor has only 1 statement in the body that makes an internal method call to setBookDetails:
  1. public Book(String author, String title, String callNumber)
  2. public Book(String author, String title)

HINT: Initialize the callNumber field to an empty String in #2 constructor

  • Add accessor method getAuthor to return the author field
  • Add accessor method getTitle to return the title field
  • Add accessor method getCallNumber to return callNumber field
  • Add accessor method getBookDetails to return a String in the format:

“<title> :   <author> (<callNumber>)”

HINT: Complete with ONLY 1 line of code using String concatenation

  • Add mutator method setBookDetails with the following header to will initialize the fields to these parameters of the same name:

      public void setBookDetails(String author, String title, String callNumber)                    

NOTE: Use method call getBookDetails where the details of a book are needed (i.e. when listing/printing books or supplying information about a particular book)

  • Create “New Class…” named Genre to store a group of books
  • Import all necessary Java package library classes
  • Declare 2 fields for each genre:
  • field named genreName of type String
  • field named books of type ArrayList of Book
  • Write a constructor (with the following header) to initialize BOTH fields

public Genre(String genreName)

HINT: Make sure you instantiate a new books ArrayList object

  • Add accessor method getGenreName to return the genreName field
  • Add accessor getGenreBooks to return the books field
  • Add accessor getNumberOfGenreBooks returning # of items in the books
  • Add a method (with header below) to check if an index is valid in books

public boolean bookIndexValid(int index)

HINT: First make sure books collection is NOT null or empty

  • Overload method addGenreBook (with the following headers) to add a new book to the genre using the ArrayList .add method
  1. public void addGenreBook(Book book)
  2. public void addGenreBook(String author,String title,String callNumber)

HINT: Make sure you create a new Book object in #2

  • Add method to return the index (or –1, if not found) of the FIRST book in ArrayList books that EXACTLY matches a particular callNumber

public int findGenreBookWithCallNumber(String callNumber)

  • Use 2 local variables (index and searching) along with a while loop
  • (Only after ENTIRE search loop is completed - thus, outside of loop)    Check (using searching) if match was found, to determine the return value
  • (At some point) Print formatted book details (if found) or error (if not found)
  • Add method to remove ONLY the FIRST book in books matching callNumber:

public void removeGenreBookWithCallNumber(String callNumber)                                                               

  • MUST 1st use findGenreBookWithCallNumber( ) to find index of callNumber
  • if bookIndexValid( ) of the returned index, then perform removal using the index and then print formatted output “Removing: <book details>”
  • else print "NO book with call number: <callNumber>”

HINT: MUST NOT use any type of loops at all

  • Add method to remove ALL items in books matching a particular author

public void removeAllGenreBooksByAuthor(String author)                                                            

  • MUST use Iterator and while loop to search through the books ArrayList
  • Remove book if author EXACTLY matches (without case-sensitivity)
  • MUST print book details for EACH removed or 1 error output (if not found)

HINT: MUST use .remove method from Iterator to ensure proper removals

  • Add method void listAllGenreBooks( ) to print out ALL items in books
  • if books ArrayList is empty, print ”NO books to print”
  • else print heading “<genreName> BOOKS:” and use for-each to print each Book’s book details with leading “ <spaces>” on its own line
  • Add void listGenreBooksByAuthor(String author) to print ALL author matches
  • Always print heading: “<genreName> BOOKS BY AUTHOR <author>:”
  • Use for-each and String.equalsIgnoreCase( ) to check every book in books to find author matches to print their book details with leading “ <spaces>”

(Only after ENTIRE search loop is completed - thus, outside of loop)    Check if NO match found, then print ”NO books by author: <author>”

  • Create “New Class…” Library for a group of genres and a book of the week
  • Import all necessary Java package library classes
  • Declare 2 fields for each library:
  • field named bookOfTheWeek of type Book
  • field named genres of type ArrayList of Genre
  • Write a constructor with header public Library( ) to initialize both fields

HINT: Include instantiation of a new genres ArrayList object AND initialize bookOfTheWeek to either null or use (optional) pickBookOfTheWeek( )

  • Add method int getNumberOfTotalBooks to return the TOTAL number of books in the ENTIRE library (include ALL genres) or 0 if there are NO books

HINT: MUST use for-each loop and call to getNumberOfGenreBooks( )

  • Overload method addGenre (with the following headers) to add a new genre to the library using the ArrayList .add method:
  1. public void addGenre(Genre genre)                                                                         
  2. public void addGenre(String genreName)

HINT: MUST create and use a new Genre anonymous object in #2.

  • Add method void removeGenre(String genreName) to remove the FIRST genre ONLY in ArrayList genres matching the genreName search parameter                                 

HINT: MUST use Iterator to help get the genre to remove it and also include any formatted output detailing the removal or an error message (if not found)

  • Add method void listAllGenres( ) to print out ALL genreNames in genres with a heading format: “THE GENRE NAMES:” (or error msg if NO genres)

HINT: Use for-each and print formatted genre info with leading spaces “     ”

  • Add method void listAllLibraryBooks( ) to print out ALL books in ALL genres in the ENTIRE library                                               

HINT: First check using getNumberOfTotalBooks that the library has books and print error message if there are NO books in the entire library

HINT: MUST use Iterator and listAllGenreBooks to help print the books for each genre (Yes, I know it’s possible w/o an Iterator, BUT you MUST use it !!!)

Add method void printBookOfTheWeek( ) to print out the details of the bookOfTheWeek or an error stating that ”There is NO Book of the Week”HINT: MUST have a heading and use getBookDetails( ) to print the details

  • Add method void pickBookOfTheWeek( ) to randomly pick a Book in the entire library from any of the genres as the bookOfTheWeek                                               

HINT: First check that Library has books to chose from, then use Random to pick a random Genre. Then, MUST check the chosen Genre has books before picking a random Book from the randomly selected genre. If there are NO books in the selected genre, then repeat the loop by trying another genre. Remember to call getGenreBooks and printBookOfTheWeek (as needed)

Solutions

Expert Solution

Solution:

Book.java:

public class Book {

   private String author;
   private String title;
   private String callNumber;

   public Book(String author, String title) {
       setBookDetails(author, title, "");
   }

   public Book(String author, String title, String callNumber) {
       setBookDetails(author, title, callNumber);
   }

   public String getAuthor() {
       return author;
   }

   public String getTitle() {
       return title;
   }

   public String getCallNumber() {
       return callNumber;
   }

   public String getBookDetails() {
       return this.getTitle() + ": " + this.getAuthor() + " (" + this.getCallNumber() + ")";
   }

   public void setBookDetails(String author, String title, String callNumber) {
       this.author = author;
       this.title = title;
       this.callNumber = callNumber;
   }

}

Genre.java

import java.util.ArrayList;
import java.util.Iterator;

public class Genre {

   private String genreName;
   private ArrayList<Book> books;

   public Genre(String genreName) {
       this.genreName = genreName;
       books = new ArrayList<Book>();
   }

   public String getGenreName() {
       return genreName;
   }

   public ArrayList<Book> getGenreBooks() {
       return books;
   }

   public int getNumberOfGenreBooks() {
       return books.size();
   }

   public void addGenreBook(Book book) {
       books.add(book);
   }

   public void addGenreBook(String author, String title, String callNumber) {
       books.add(new Book(author, title, callNumber));
   }

   public boolean bookIndexValid(int index) {
       if (books != null) {
           return (index < books.size() && index >= 0);
       } else {
           return false;
       }
   }

   public void removeGenreBookWithCallNumber(String callNumber) {
       int index = findGenreBookWithCallNumber(callNumber);
       if (bookIndexValid(index)) {
           System.out.println("Removing: " + books.get(index).getBookDetails());
           books.remove(index);
       } else {
           System.out.println("NO book with call number: " + callNumber);
       }
   }

   //
   public int findGenreBookWithCallNumber(String callNumber) {
       int index = 0;
       boolean searching = false;
       while (index < books.size()) {
           if (books.get(index).getCallNumber().equals(callNumber)) {
               searching = true;
               break;
           }
           index++;
       }

       if (searching) {
           return index;
       } else {
           return -1;
       }
   }

   public void removeAllGenreBooksByAuthor(String author) {
       Iterator<Book> it = books.iterator();
       while (it.hasNext()) {
           Book book = it.next();
           if (book.getAuthor().equalsIgnoreCase(author)) {
               System.out.println(book.getBookDetails());
               it.remove();
           }
       }
   }

   public void listAllGenreBooks() {
       if (books.isEmpty()) {
           System.out.println("NO Books to print");
       } else {
           System.out.println("BOOKS:");
           for (Book book : books) {
               System.out.println(book.getBookDetails());
           }
       }
   }

   public void listGenreBooksByAuthor(String author) {
       boolean found = false;
       for (Book book : books) {
           System.out.println("BOOKS BY AUTHOR " + author);
           if (book.getAuthor().equalsIgnoreCase(author)) {
               System.out.println(book.getBookDetails());
               found = true;
           }
       }

       if (!found) {
           System.out.println("NO books by author: " + author);
       }
   }

}

Library.java:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

public class Library {

   private Book bookOfTheWeek;
   private ArrayList<Genre> genres;

   public Library() {
       bookOfTheWeek = null;
       genres = new ArrayList<Genre>();
   }

   public int getNumberOfTotalBooks() {
       int numberOfTotalBooks = 0;
       for (Genre genre : genres)
           numberOfTotalBooks += genre.getNumberOfGenreBooks();
       return numberOfTotalBooks;
   }

   public void addGenre(Genre genre) {
       genres.add(genre);
   }

   public void addGenre(String genreName) {
       genres.add(new Genre(genreName));
   }

   public void removeGenre(String genreName) {
       boolean found = false;
       Iterator<Genre> currentGenre = genres.iterator();
       while (currentGenre.hasNext()) {
           Genre genre = currentGenre.next();
           if (genre.getGenreName().equalsIgnoreCase(genreName)) {
               System.out.println("Removing: " + genre.getGenreName());
               found = true;
               currentGenre.remove();
               break;
           }
       }

       if (!found) {
           System.out.println("Genre Not found: " + genreName);
       }
   }

   public void listAllGenres() {
       if (genres.size() == 0) {
           System.out.println("NO Genres");
       } else {
           System.out.print("THE GENRE NAMES: ");
           for (Genre genre : genres) {
               System.out.print(genre.getGenreName() + " ");
           }
       }
   }

   public void listAllLibraryBooks() {
       if (getNumberOfTotalBooks() == 0) {
           System.out.println("THERE IS NO BOOKS IN THE LIBRARY");
       } else {
           Iterator<Genre> curremtGenre = genres.iterator();
           while (curremtGenre.hasNext()) {
               Genre genre = curremtGenre.next();
               genre.listAllGenreBooks();
           }
       }

   }

   public void printBookOfTheWeek() {
       if (this.bookOfTheWeek == null) {
           System.out.println("There is NO Book of the Week");
       } else {
           System.out.println("BOOK OF THE WEEK: " + this.bookOfTheWeek.getBookDetails());
       }
   }

   public void pickBookOfTheWeek() {
       Random random = new Random();
       for (;;) {
           int genreIndex = (genres.size() - 1) - 0;
           int genre = random.nextInt(genreIndex + 1) + 0;
           Genre randomGenre = genres.get(genre);
           if (randomGenre.getNumberOfGenreBooks() == 0) {
               continue;
           } else {
               ArrayList<Book> booksList = randomGenre.getGenreBooks();
               int index = ((booksList.size() - 1) - 0);
               int bookIndex = random.nextInt(index + 1) + 0;
               bookOfTheWeek = booksList.get(bookIndex);
           }
       }
   }

}


Related Solutions

Create a class named Billing that includes three overloaded computeBill() methods for a photo book store....
Create a class named Billing that includes three overloaded computeBill() methods for a photo book store. • When computeBill() receives a single parameter, it represents the price of one photo book ordered. Add 8% tax, and return the total due. • When computeBill() receives two parameters, they represent the price of a photo book and the quantity ordered. Multiply the two values, add 8% tax, and return the total due. • When computeBill() receives three parameters, they represent the price...
Create a class named Billing that includes three overloaded computeBill() methods for a photo book store....
Create a class named Billing that includes three overloaded computeBill() methods for a photo book store. • When computeBill() receives a single parameter, it represents the price of one photo book ordered. Add 8% tax, and return the total due. • When computeBill() receives two parameters, they represent the price of a photo book and the quantity ordered. Multiply the two values, add 8% tax, and return the total due. • When computeBill() receives three parameters, they represent the price...
Create a class named “Car” which has the following fields. The fields correspond to the columns...
Create a class named “Car” which has the following fields. The fields correspond to the columns in the text file except the last one. i. Vehicle_Name : String ii. Engine_Number : String iii. Vehicle_Price : double iv. Profit : double v. Total_Price : double (Total_Price = Vehicle_Price + Vehicle_Price* Profit/100) 2. Write a Java program to read the content of the text file. Each row has the attributes of one Car Object (except Total_Price). 3. After reading the instances of...
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
This is 1 java question with its parts. Thanks! Create a class named Lease with fields...
This is 1 java question with its parts. Thanks! Create a class named Lease with fields that hold an apartment tenant’s name, apartment number, monthly rent amount, and term of the lease in months. Include a constructor that initializes the name to “XXX”, the apartment number to 0, the rent to 1000, and the term to 12. Also include methods to get and set each of the fields. Include a nonstatic method named addPetFee() that adds $10 to the monthly...
In java, create a class named Contacts that has fields for a person’s name, phone number...
In java, create a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. Include javadoc...
Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a...
Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a doubleannual sales amount. Methods include a constructor that requires values for both data fields, as well as get and set methods for each of the data fields. Write an application named DemoSalesperson that declares an array of 10 Salesperson objects. Set each ID number to 9999 and each sales value to zero. Display the 10 Salesperson objects. public class DemoSalesperson { public static void...
Create a class named CollegeCourse that includes data fields that hold the department (for example, ENG),...
Create a class named CollegeCourse that includes data fields that hold the department (for example, ENG), the course number (for example, 101), the credits (for example, 3), and the fee for the course (for example, $360). All of the fields are required as arguments to the constructor, except for the fee, which is calculated at $120 per credit hour. Include a display() method that displays the course data. Create a subclass named LabCourse that adds $50 to the course fee....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT