Creating Book Class in Java

Problem:

Write a class named Book that keeps track of book objects such that the instance data contains the book’s title and author and a unique identification number, say id that starts form 1 for the first book object and is incremented by 1 each time a new book with at least the title specified is created. The required methods in the Book class are as follows:

  • 3 constructors: One without parameters that sets the title and author to “unknown” and id to 0; one with a single parameter that sets the title to the given value as parameter, sets the author to “unknown”, increments the sequence by 1 and sets id to this sequence number; one with two parameters for title and author, setting the corresponding instance data, and id as in the second constructor.
  • The setter methods for title and author.
  • The getter methods for title, author, and id.
  • The equals method that compares the current book object with another Book object given as a parameter and returns true if both objects have the same title and author properties, and false otherwise.
  • The toString method that returns a text including the book’s title, author and id. Refer to the sample execution window for the test program for details.
  • The getInitials method that returns the initial letters of the author’s first name(s) and last name, if the author’s name is known (not equal to “unknown”, or not null).Assume that there can be at most two names and one surname separated by a single blank. Refer to the sample execution window for the test program for details.

Write a driver class that tests the Book class. Allow the user to enter as many book objects as s/he wants. Take care of necessary object initializations. Store the first and last book objects separately. Print each book object. Compare the first and last book objects, if any, and display a proper message if they are the same. Display the last book’s author’s initials, if the user has input any valid book object.

Sample execution windows:
To end the input process bypass each question by typing the enter key!
Type the title of the book:
Type the name of the author:
Press any key to continue...

To end the input process bypass each question by typing the enter key!
Type the title of the book: Windows NT Server 4.0
Type the name of the author: Russel
Book No: 1 entitled "Windows NT Server 4.0" written by Russel
Type the title of the book:
Type the name of the author:
First and last books are same
Last book's author has the initials, R.
Press any key to continue...

To end the input process bypass each question by typing the enter key!
Type the title of the book: Java Software Solutions
Type the name of the author: Lewis Loftus
Book No: 1 entitled "Java Software Solutions" written by Lewis Loftus
Type the title of the book: Introduction to Java Programming with JBuilder
Type the name of the author: Yvet Daniel Liang
Book No: 2 entitled "Introduction to Java Programming with JBuilder" written by
Yvet Daniel Liang
Type the title of the book:
Type the name of the author:
Last book's author has the initials, Y.D.L.
Press any key to continue...

Solution:

public class Book
{
 private String author, title;
 private int id;
 private static int identification =0;
 
 public Book()
 {
  title = author = "unknown";
  id = 0;
 }
 public Book(String title)
 {
  this.title = title;
  author = "unknown";
  identification++;
  id = identification;
 }
 public Book (String title, String author)
 {
  this.title = title;
  this.author = author;
  identification++;
  id = identification;
 }
 
 public void setTitle(String title)
 {
  this.title = title;
 }
 
 public void setAuthor(String author)
 {
  this.author = author;
 }
 
 public String getTitle()
 {
  return title;
 }
 public String getAuthor()
 {
  return author;
 }
 public int getId()
 {
  return id;
 }
 
 public boolean equals(Book other)
 {
  boolean enough = true;
  String otherAuthor = other.getAuthor();
  String otherTitle = other.getTitle();
  if(otherAuthor.equals(author) && otherTitle.equals(title))
   enough = true;
  else 
   enough = false;
  return enough;
 }
 public String getInitials ()
 {
  String output = "";
  if (!(author.equals("")) && !
          (author.equals("unknown")) && !(author.equals(null))) {
   output += author.charAt(0);
   output += ". ";
   if (author.indexOf(" ") != -1) {
   output += author.charAt(author.indexOf(" ") + 1);
   output += ". "; }
   
   if (author.indexOf(" ") != -1) {
   output += author.charAt((author.indexOf(" ") 
                         + author.indexOf(" ")) +4 );
   output += ". "; }
  }
  return output;
 }
 public String toString()
 {
  return "Book No: " + id + " entitled \"" 
                + title + "\"" + " written by " + author;
 }
}
import java.util.Scanner;
public class BookTest
{
 public static void main (String[] args)
 {
  Book firstBook = new Book();
        Book lastBook = new Book();
        Book aBook = new Book();
        
  Scanner scan = new Scanner (System.in);
  System.out.println("To end the input " +
       "process bypass each question by typing the enter key!");
  System.out.print("Type the title of the book: ");
  String title = scan.nextLine();
  System.out.print("Type the name of the author: ");
  String author = scan.nextLine();
  
  aBook = new Book(title, author);

  if (!(title.equals("") && author.equals("")))
  System.out.println(aBook);
  
  while (!(title.equals("") && author.equals("")))
  {
    if (aBook.getId() == 1)
     firstBook = aBook;
    System.out.print("Type the title of the book: ");
    title = scan.nextLine();
    System.out.print("Type the name of the author: ");
    author = scan.nextLine();
    if (!(title.equals("") && author.equals(""))) {  
     aBook = new Book(title, author);
    System.out.print(aBook);
    System.out.println(); }
  }
  lastBook = aBook;
  if (firstBook.equals(lastBook))
   System.out.println("First and last books are the same");
  System.out.println(lastBook.getInitials());
    
  
 }
}


38 comments :

  1. Hi Admin,
    Informative content you have shared in your post, In near future, big data handling and processing is going to the future of IT industry Hadoop Training in Chennai | Hadoop Training Chennai

    ReplyDelete
  2. If you want to lean any technology, you should have a basic knowledge in JAVA, better you start learning Best JAVA Training in Chennai to get a successful career.

    ReplyDelete
  3. I'm B.E Graduation students. I move that my career was IT Industries. I Learning out Java Course. I was learning time I prepared start Java Interview Question. I searching that time all read our blog site.. We haven't posted that many no interview question. That question really useful to my career job preparation. Keep on updating yours with such awesome information. We Provide that digital marketing related courses visit us :- Digital Marketing Training in Chennai | Digital Marketing Course in Chennai | SEO Training in Chennai

    ReplyDelete
  4. SME Business Management Consultancy is a Certified Management Consultancy in Dubai-UAE provides you best practice of delivering quality services. This management field has qualified professionals in finance management also they are well experts in accounting, auditing and tax services.Have a look on https://www.smebusinessonline.com/

    ReplyDelete
  5. Madhuvahini Houseboat Service is the new initiative being launched by Malabar tourism management @ Bekal, Nileshwar, Kasaragod, Kannur, Calicut, Manglore. 1HR, 2HR, Sunset, Dinner, Overnight, Day Night Cruise Available. Life is Better on Boats to explore the area of backwaters and offer Kerala traditional Home and Boat Stay Service.

    ReplyDelete
  6. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
    Hadoop Training in Chennai

    ReplyDelete
  7. Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.AWS Training in chennai

    ReplyDelete
  8. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
    Blueprism training in Chennai

    Blueprism training in Bangalore

    Blueprism training in Pune

    Blueprism training in tambaram

    Blueprism training in annanagar

    Blueprism training in velachery

    Blueprism training in marathahalli

    ReplyDelete
  9. A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts read this.

    click here

    Selenium Training in Chennai

    Selenium Training in Bangalore

    ReplyDelete
  10. Marvelous and fascinating article. Incredible things you've generally imparted to us. Much obliged. Simply keep making this kind out of the post.

    Oracle DBA Training
    Oracle PLSQL Training
    Oracle Performance Tunning Training
    Core Java Training

    ReplyDelete
  11. such a good blog the more informative things are enabled in this contents...keep sharing

    java training in chennai

    selenium training in chennai

    ReplyDelete
  12. I’m surprised at how fast your blog loaded on my cell phone. I’m not even using WIFI, just 3G. Anyways, awesome blog!
    iosh course in chennai

    ReplyDelete
  13. Thanks for the article ,it is very useful to me while am practicing the programs.
    Livewire Velachery: 9384409662

    ReplyDelete
  14. Hey there! I know this is kind of off-topic, but I’d figured I’d ask. Would you be interested in exchanging links or maybe guest authoring a blog post or vice-versa?
    safety course in chennai

    ReplyDelete
  15. Thinking how to make money? Come to us and win now good slot online Play and win always and with us.

    ReplyDelete
  16. You are doing a great job. I would like to appreciate your work for good accuracy
    Dotnet Training in Chennai

    ReplyDelete
  17. Free internship in chennai offers summer best internship,inplant and ipt training for Engineering UG,PG Students like BE/BTECH/ME/MTECH/ BCA/BSC/MCA/MSC.

    ReplyDelete
  18. I enjoyed reading your post. I will share it with my other friends as the information is really very useful. Keep sharing your excellent work. Best Advanced Java Course in Delhi

    ReplyDelete
  19. Such a great information for blogger i am a professional blogger thanks…

    Start your journey with Best SAP Course and get hands-on Experience with 100% Placement assistance from experts Trainers @Softgen Infotech Located in BTM Layout Bangalore. Expert Trainers with 8+ Years of experience, Free Demo Classes Conducted.

    ReplyDelete
  20. Your contents are completely awesome and share worthy. I really appreciate your efforts that you put on this. Keep sharing. For more Java Programming For Kids related information visit Techno Smart Kids

    ReplyDelete
  21. Here I found the biggest solution for creating books in java. Every java developer gets satisfied after reading this post.
    Web Designing Company in Madurai

    ReplyDelete
  22. I always check this type of advisory post and I found your article which is related to my interest. This is a great way to increase knowledge for us. Thanks for sharing an article like this.Python Program for Kids in Usa

    ReplyDelete
  23. Best Accounting Software Services:-
    Best Busy & Tally Accounting Software Services buy now for your business tax invoicing , data backup etc; we are certified partner.
    Website Link:- https://dssoftweb.com

    ReplyDelete
  24. Thanks for sharing this wonderful list of blog comments. Almost every link is live and active. And I hope this will help me get the highest quality Backlinks.
    for my website.

    ReplyDelete

  25. This is an excellent post - thank you for sharing it! I always relish the opportunity to read such superb content filled with valuable information. The ideas presented here are truly best and exceptionally captivating, making the post thoroughly enjoyable. Please continue with your fantastic work.
    visit: Unveiling the Power of Java Streams API

    ReplyDelete
  26. People want to see knockouts Davis Cup Final live online and excitement says Wilder so the Parker fight on December 23 should be the main event.

    ReplyDelete
  27. The heavyweight bout between WBC champion Tyson Fury and former UFC champion and boxing Makhmudov vs Kabayel live rookie Francis Ngannou took place in the nation last month.

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact