Q1 Book Hierarchy Abstract Class and Inheritance 1 Write cla
Q1) Book Hierarchy (Abstract Class and Inheritance)
1. Write class called Publisher. Include attributes name (String) and state (String).
2. Create an abstract class named Book.
a. Include fields for title (String), publisher (Publisher), isbn (String), date of publishing (Date), and price (double).
b. Include a constructor that requires the book title, publisher, isbn, and date of publishing.
c. Add get methods to return title, publisher, price, and date of publishing.
d. Include set methods for title, publisher, price, and date of publishing. Set method for price is an abstract method.
e. Include an abstract method calculateCharge that accepts quantity sold and returns total charge.
f. Include toString() method.
3. Create two sub classes of Book: Fiction and NonFiction.
a. Each must include a setPrice() method that sets the price.
b. Include calculateCharge method in each of the subclasses that implements calculateCharge from Book class
c. Include toString() in each class.
d. Write a constructor for each subclass, and include a call to setPrice() within each.
4. Interface: Create an interface called Comparable.
a. This interface contains only one method called isEqual
b. Implement the interface in subclasses Fiction and NonFiction.
c. isEqual method accepts a book object and compares the isbn number of the current object with that of the incoming object and returns true if both are same.
5. Create a clone method in subclasses that returns a copy of the current book object.
6. Write a demo program called BooksDemo.
a. Create one fiction and one non-fiction book and display all fields using toString() methods.
b. Demonstrate equals method
c. Demonstrate clone method
Solution
public class Publisher{
    String name;
    String state;
    public Publisher(String name, String state) {
        this.name = name;
        this.state = state;
    }
 }
import java.util.Date;
public abstract class Book{
    void setTitle(String title) {
        this.title = title;
    }
    void setPublisher(Publisher publisher) {
        this.publisher = publisher;
    }
    void setIsbn(String isbn) {
        this.isbn = isbn;
    }
    void setPublishing(Date publishing) {
        this.publishing = publishing;
    }
   abstract void setPrice(double price);
   
    double getPrice() {
        return price;
    }
    String getTitle() {
        return title;
    }
    Publisher getPublisher() {
        return publisher;
    }
    String getIsbn() {
        return isbn;
    }
    Date getPublishing() {
        return publishing;
    }
    String title;
    Publisher publisher;
    String isbn;
    Date publishing;
    double price;
    public Book(String title, Publisher publisher, String isbn, Date publishing) {
        this.title = title;
        this.publisher = publisher;
        this.isbn = isbn;
        this.publishing = publishing;
    }
    abstract double calculateCharge(int quantity);
    public String toString() {
        return \"Book [title=\" + title + \", publisher=\" + publisher + \", isbn=\"
                + isbn + \", publishing=\" + publishing + \", price=\" + price
                + \"]\";
    }
 }
import java.util.Date;
public class Fiction extends Book implements Comparable{
    public Fiction(String title, Publisher publisher, String isbn, Date publishing, double price) {
        super(title, publisher, isbn, publishing);
        setPrice(price);
    }
   void setPrice(double price) {
        this.price = price;
    }
   double calculateCharge(int quantity) {
        return quantity * price;
    }
   public String toString() {
        return \"Fiction [title=\" + title + \", publisher=\" + publisher
                + \", isbn=\" + isbn + \", publishing=\" + publishing + \", price=\"
                + price + \"]\";
    }
   public boolean isEqual(Book b) {
        return this.getIsbn() == b.getIsbn();
    }
    public Book clone(){
        Book copy = this;
        return copy;
    }
 }
import java.util.Date;
public class NonFiction extends Book implements Comparable{
    public NonFiction(String title, Publisher publisher, String isbn, Date publishing, double price) {
        super(title, publisher, isbn, publishing);
        setPrice(price);
    }
   void setPrice(double price) {
        this.price = price;
       
    }
   double calculateCharge(int quantity) {
        return quantity * price;
    }
   public String toString() {
        return \"NonFiction [title=\" + title + \", publisher=\" + publisher
                + \", isbn=\" + isbn + \", publishing=\" + publishing + \", price=\"
                + price + \"]\";
    }
   public boolean isEqual(Book b) {
        return this.getIsbn() == b.getIsbn();
    }
    public Book clone(){
        Book copy = this;
        return copy;
    }
 }
public interface Comparable{
    boolean isEqual(Book b);
 }
import java.util.Date;
public class BooksDemo {
    public static void main(String args[]){
        String title1 = \"Abcd\", title2 = \"Pqrs\";
        Publisher p1 = new Publisher(\"P1\", \"S1\");
        Publisher p2 = new Publisher(\"P1\", \"S1\");
        String isbn1 = \"Abcd\", isbn2 = \"Pqrs\";
        Date d1 = new Date();
        Date d2 = new Date();
        Fiction book1 = new Fiction(title1, p1, isbn1, d1, 100);
        Fiction book2 = new Fiction(title2, p2, isbn2, d2, 200);
        System.out.println(\"Book 1\");
        System.out.println(book1);
        System.out.println(\"Book 2\");
        System.out.println(book2);
        System.out.println(\"\ \" + book1.equals(book2));
        System.out.println(\"\ \" + book1.clone());
    }
 }




