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

Books and Textbooks Create a class called Book, which has the following variable

ID: 3567201 • Letter: B

Question

Books and Textbooks

Create a class called Book, which has the following variables:

a) Write a constructor for the class Book that takes in as parameters Strings representing the title and author of the book. Test this method by creating three different instances of type Book.

b) Write the method toString() for the class Book which prints out the title of the Book, followed by " by ", and then the author. If the title of the book is "Never Cry Wolf", and the author of the book is "Farley Mowat", then its toString() method would return the String "Never Cry Wolf by Farley Mowat". Test this method by printing out the information for the three instance of Book created in part (a).

c) Create a subclass of Book called Textbook. This subclass should have one additional variable: subject, which is a String representing the subject of the textbook. Write the constructor forTextbook that takes in as its parameters Strings representing the title, author, and subject of the TextBook. Test this method by creating three different instances of type Textbook.

d) Write a toString() method that returns a String containing information (title, author, subject) about a Textbook. You can decide how to format the String. Test this method by printing out the information for the three instance of TextBook created in part (a). e) Write a method which takes in an array of Books and returns an array of TextBooks that contains exactly thoseBooks in the input array which are also TextBooks.

Explanation / Answer

Here you go :)

Please comment if you have any doubts

//Book Class

public class Book
{
   private String author,title;
   public Book(String title,String author)
   {
       this.author=author;
       this.title=title;
   }
   public String toString()
   {
       return this.title+" by "+this.author;
   }
   public static void main(String[] args) {
       Book b1=new Book("1", "2");
       System.out.println(b1.toString());
   }
}

//Textbook class

//BandTB method checks whether a book is textbook or not

import java.util.HashSet;

public class Textbook extends Book
{
   private String subject;
   public Textbook(String title, String author) {
       super(title, author);
   }
   public Textbook(String title, String author,String subject)
   {
       super(title, author);
       this.subject=subject;
   }
   public String toString()
   {
       return super.toString()+" of "+this.subject;
   }
  
   public static Textbook[] BandTB(Book[] b)
   {
       HashSet<Textbook> h=new HashSet<Textbook>();
       int length=0;
       for(int i=0;i<b.length;i++)
       {
           if(b[i].getClass().getName().equals("Textbook"))
           {
               h.add((Textbook)b[i]);
               length++;
           }
       }
       Textbook k[]=new Textbook[length];
       return h.toArray(k);
   }
   public static void main(String[] args)
   {
       Textbook tb1=new Textbook("a", "b","c");
       Book b2=new Book("z", "x");
       System.out.println(b2.toString());
       System.out.println(tb1.toString());
       Book[] b=new Book[3];
       b[0]=new Book("a", "b");
       b[1]=new Book("c", "d");
       b[2]=new Textbook("e", "f","g");
      
       System.out.println(BandTB(b).length);
      
   }

}

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