Book, Dictionary and Novel Class in Java
Posted by Samath
Last Updated: January 17, 2024

Write a class named Book to represent a book. This class is to be the parent class for several types of books. The class should define the following methods:

a) A constructor that accepts the isbn number of a book (a string) and the number of pages in the book, in that order.

b) The getter methods getISBN, and getPages.

c) A setter method updatePages that accepts an integer as a parameter and sets the number of pages to the parameter value.

Write the class Dictionary which is a subclass of Book. A dictionary has an isbn number and a number of pages along with a count of the number of entries it contains. The class Dictionary shall have the following methods:

a) A constructor that accepts the the isbn number, number of entries, and number of pages (in that order).

b) A constructor that accepts the isbn number and number of pages (in that order) as parameters. This constructor shall set the number of entries for the dictionary to 0.

c) A method named setEntries that accepts an integer as it parameter and sets the number of entries to the value of the parameter.

d) A toString method that returns a string formatted as follows: ISBN: Entries per page: 

ISBN: <isbncode>    Entries per page: <calculate the ration entries / page>

Write the class Novel which is a subclass of the class Book. A novel has an isbn number, a number of pages, a title (a string) and an author (also a string). The class shall have the following methods:

a) A constructor that accepts the novel’s title, author, isbn, and number of pages, in that order.

b) A setter method updatePages that accepts an integer value and set the number of pages for the novel to the parameter value + 10 (to accommodate prologue and epilogue).

c) A toString method that returns a string formatted as follows:

Author: <author>      Title:<title>    ISBN:<isbn>   Pages:<pages>

4. Write code to test your classes by performing the following tasks:

a) Create a dictionary without specifying the number of entries.

b) Create a novel with data of your choice.

c) Create a second dictionary specifying the number of entries

d) Invoke the updatePages method with the value 100 on each of the object.

e) Print all the objects.

 

Book Class:

public class Book 
{
    protected String isbn_num;
    protected String page_num;
    
    public Book(String isbn, String page)
    {
        isbn_num = isbn;
        page_num = page;
    }
    
    public String getISBN()
    {
        return isbn_num;
    }
    
    public String getPages()
    {
        return page_num;
    }
    
    public void updatePages(String p)
    {
        page_num = p;
    }
}

 

Dictionary Class:

public class Dictionary extends Book
{
    private int entries_count;
    
    public Dictionary(String isbn, int en_count, String page)
    {
        super(isbn, page);
        entries_count = en_count;
    }
    
    public Dictionary(String isbn, String page)
    {
        super(isbn, page);
        entries_count = 0;
    }
    
    public void setEntries(int en)
    {
         entries_count = en;
    }
    
    public String toString()
    {
        String result;
        int enPerPage = (entries_count/Integer.parseInt(page_num));
        
        result = "ISBN: "+ isbn_num +" Entries per page: "+ enPerPage+"\n";
        return result;
    }
    
}

 

Novel Class:

public class Novel extends Book
{
    private String author;
    private String title;
    
    public Novel(String t, String aut, String isbn, String page)
    {
        super(isbn, page);
        author = aut;
        title = t;
    }
    
    public void updatePages(String p)
    {
        int temp = Integer.parseInt(p)+10;
        page_num = Integer.toString(temp);      
    }
    
    public String toString()
    {
        String result;
        result = "Author: "+author+" Title: "+title+" ISBN: "+ isbn_num +" Page: "+ page_num+"\n";
        return result;
    }
}

 

BookTest:

public class BookTest {
    public static void main(String[] args) {
        Dictionary dic = new Dictionary("63728","65");
        Novel nov = new Novel("UP and Running", "John Brown", "59876", "98");
        Dictionary dic2 = new Dictionary("85648",325,"65");
        
        dic.updatePages("100");
        nov.updatePages("100");
        dic2.updatePages("100");
        
        System.out.print(dic);
        System.out.print(nov.toString());
        System.out.print(dic2.toString());
        
  }
}

 

Related Content
Date Book Program in Java
Date Book Program in Java
rjndhl1 | Jan 23, 2015
Python add new item to a dictionary
Python add new item to a dictionary
Samath | Jan 13, 2017
Simple Data Class using C++
Simple Data Class using C++
Samath | Jan 07, 2021
Account Class in C++
Account Class in C++
Samath | Jan 07, 2021
Car Class using C++
Car Class using C++
Samath | Jan 07, 2021
Circle Class in C++
Circle Class in C++
Samath | Jan 07, 2021
Invoice class using Java
Invoice class using Java
Samath | Jan 17, 2024
Movie Class in C++
Movie Class in C++
Samath | Jan 20, 2024
Rectangle Class in C++
Rectangle Class in C++
Samath | Jan 17, 2024
Movie Class in Java
Movie Class in Java
Samath | Jan 20, 2024