Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class Book that a) han the following instance variables authori String

ID: 3891763 • Letter: C

Question

Create a class Book that a) han the following instance variables authori String titier string cost: double b) has setter and getter for all the instance variables. a) has 2 constructors initialize author to TBD and other variables to their default values initialize all object variables to their corresponding parameter values Book(string authorx, String titlex, int, pages, double price) t prints to the monitor the values of each object variable Layout View--/ Sec 11. Pages: 1011- Words: 43 of 168- MacBook Pro ?0 a F3 F6 F7 F8 3 4 9

Explanation / Answer

Since no language is mentioned, I am doing this in Java.

I have made 3 classes: Book, Library, Test.

Book.java:

public class Book{
   String author;
   String title;
   int numberOfPages;
   double cost;


   Book(){
       this.author = "TBD";
       this.title = "None";
       this.numberOfPages = 0;
       this.cost = 0;
   }

   Book(String authorx,String titlex, int pages,double price){
       this.author = authorx;
       this.title = titlex;
       this.numberOfPages = pages;
       this.cost = price;
   }

   public String getAuthor(){
       return this.author;
   }
   public String getTitle(){
       return this.title;
   }
   public int getNumberOfPages(){
       return this.numberOfPages;
   }
   public double getCost(){
       return this.cost;
   }

   public void setAuthor(String author){
       this.author = author;
   }
   public void setTitle(String title){
       this.title = title;
   }
   public void setNumberOfPages(int numberOfPages){
       this.numberOfPages = numberOfPages;
   }
   public void    setCost(double cost){
       this.cost = cost;
   }
   public void printMe(){
       System.out.println("Author: "+author);
       System.out.println("Title: "+title);
       System.out.println("Number Of Pages: "+numberOfPages);
       System.out.println("Cost: "+cost);

   }
}

Library.java:

import java.util.*;

public class Library{
   ArrayList<Book> bookCollection;
   Library(){
       bookCollection = new ArrayList<Book>();
   }

   public void addBook(Book b){
       this.bookCollection.add(b);
   }

   public boolean removeBook(String author, String title){

       for(Book book:bookCollection){
           if(book.author == author && book.title == title){
               bookCollection.remove(book);
               return true;
           }
       }
       return false;
   }

   public boolean removeAllBooks(String author){

       int flag = 0;
       ArrayList<Book> new_BookCollection = new ArrayList<Book>();
       for(Book book:bookCollection){
           if(book.author.equals(author)){

               flag=1;
           }
           else{
               new_BookCollection.add(book);
           }
       }

       bookCollection = new_BookCollection;
       if(flag==0){
           return false;
       }
       else {
           return true;
       }  
      
   }

   public ArrayList<Book> findAllBook(String author){

       ArrayList<Book> booksOfAuthor = new ArrayList<Book>();
       for (Book book:bookCollection){
           if(book.author == author){
               booksOfAuthor.add(book);
           }
       }

       return booksOfAuthor;

   }

   public void printLibrary(){
       for(Book book:bookCollection){
           book.printMe();
           System.out.println("");
       }
   }

}

Test.java:

import java.util.*;

public class Test{

   public static void main(String[] args){

      
       Book book = new Book();       //Book() constructor


       book.printMe();               //printMe() method
       System.out.println();
       book.setAuthor("William Shakespeare");        //setAuthor() method
       book.setTitle("The Hamlet");           //setTitle() method
       book.setNumberOfPages(150);               //setNumberOfPages() method
       book.setCost(50);                       //setCost() method
       book.printMe();
       System.out.println();
      
      
       Book book1 = new Book("William Shakespeare","Romeo and Juliet",200,100);            //Book(author,title,pages,cost) constructor used
       book1.printMe();


       Book book3 = new Book("Agatha Christie","And Then There Were None",500,200);
       book3.printMe();

       Book book4 = new Book("Agatha Christie","The ABC Murders",500,200);
       book4.printMe();

       Book book5 = new Book("Agatha Christie","Crooked House",500,200);
       book5.printMe();

       /*Getter methods of all variables of Book */
       System.out.println("Getting details of Book1:");
       System.out.println("Book1's Author:"+book1.getAuthor());
       System.out.println("Book1's Title:"+book1.getTitle());
       System.out.println("Book1's Number of Pages:"+book1.getNumberOfPages());
       System.out.println("Book1's Cost:"+book1.getCost());

       Library library = new Library();        //Library() constructor used


       /*adding books to the library*/
       library.addBook(book);
       library.addBook(book1);
       library.addBook(book3);
       library.addBook(book4);
       library.addBook(book5);
      
       System.out.println(" Books in Library:");
      
       library.printLibrary();                   //printLibrary() method

       Boolean flag = library.removeBook("William Shakespeare","Romeo and Juliet");       //removeBook() method
       System.out.println("Romeo and Juliet of William Shakespeare removed from Library? Ans:"+flag);


       System.out.println(" Books in Library after removing Romeo and Juliet:");
       library.printLibrary();      


       Boolean flag1 = library.removeAllBooks("Agatha Christie");       //removeAllBooks() method
       System.out.println(" Removed all books of Agatha Christie from library? Ans:"+flag1);
       System.out.println(" Books in Library after removing all Agatha Christie books:");
       library.printLibrary();      

       library.addBook(book3);
       library.addBook(book4);

       System.out.println(" Books in Library after adding 2 new books:");
       library.printLibrary();      

       System.out.println("Finding books of Agatha Christie in the library:");
       ArrayList<Book> books = library.findAllBook("Agatha Christie");           //findAllBook() method

       for(Book book_1:books){
           book_1.printMe();
       }

   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote