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()); } }
Hi Admin,
ReplyDeleteInformative 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
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.
ReplyDeleteI'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
ReplyDeletethank you very useful cse projects in chennai embedded projects in chennai internship in chennai seo company in chennai java training in chennai
ReplyDeleteSME 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/
ReplyDeleteMadhuvahini 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.
ReplyDeleteNeeded to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
ReplyDeleteHadoop Training in Chennai
Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.AWS Training in chennai
ReplyDeleteWow 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.
ReplyDeleteBlueprism 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
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.
ReplyDeleteclick here
Selenium Training in Chennai
Selenium Training in Bangalore
Marvelous and fascinating article. Incredible things you've generally imparted to us. Much obliged. Simply keep making this kind out of the post.
ReplyDeleteOracle DBA Training
Oracle PLSQL Training
Oracle Performance Tunning Training
Core Java Training
such a good blog the more informative things are enabled in this contents...keep sharing
ReplyDeletejava training in chennai
selenium training in chennai
I’m surprised at how fast your blog loaded on my cell phone. I’m not even using WIFI, just 3G. Anyways, awesome blog!
ReplyDeleteiosh course in chennai
Thanks for the article ,it is very useful to me while am practicing the programs.
ReplyDeleteLivewire Velachery: 9384409662
Great share !!
ReplyDeleteieee final year projects in chennai
embedded projects in chennai
iot projects in chennai
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?
ReplyDeletesafety course in chennai
Thinking how to make money? Come to us and win now good slot online Play and win always and with us.
ReplyDeleteWell written article and a well maintained blog. I am really enjoying your writing skills. Please keep writing.
ReplyDeleteDevOps training in Chennai with Placement
Angular 6 training in Chennai with Placement
Automation Anywhere training in Chennai with Placement
You are doing a great job. I would like to appreciate your work for good accuracy
ReplyDeleteDotnet Training in Chennai
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.
ReplyDeleteI 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
ReplyDeleteSuch a great information for blogger i am a professional blogger thanks…
ReplyDeleteStart 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.
very informative post shared by you. Thanks for sharing
ReplyDeletejava training course in Delhi
java training course in Noida
Your article about software testing is awesome. It helped me to understand the career prospects in software testing industry.thanks a lt
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
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
ReplyDeleteHere I found the biggest solution for creating books in java. Every java developer gets satisfied after reading this post.
ReplyDeleteWeb Designing Company in Madurai
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
ReplyDeleteInformative content, keep sharing with us.
ReplyDeleteTypes of GST
How Does GST Works
Thank you so much for sharing this with us. Do visit college projects in Chennai
ReplyDeleteBest Accounting Software Services:-
ReplyDeleteBest Busy & Tally Accounting Software Services buy now for your business tax invoicing , data backup etc; we are certified partner.
Website Link:- https://dssoftweb.com
Very informative post .Thanks for sharing Java Course in Pune
ReplyDeleteThanks 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.
ReplyDeletefor my website.
Thanks for sharing such a wonderful post.
ReplyDeleteJava Classes in Nagpur
ReplyDeleteThis 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
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.
ReplyDeleteThe 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.
ReplyDeleteNice blog.... Thanks for sharing
ReplyDeletebest project center in chennai| best java project center in Chennai| Best cse project center in Chennai| Best python project center in Chennai| best matlab project center in Chennai
That was great to read such an useful post.
ReplyDeleteJava course in Pune