Java program to print book details.


Problem statement:

Create a class Book with the following private member variables
• String bookName

• int bookPrice

• String authorName

Include the appropriate getters and setters method.

Create a class TestBook which has the main method. Get the details as shown in the sample input. Create an object for book class and assign the value for its attributes using the setters. Print the output as shown in the sample output using the getters method.
All classes and methods should be declared as public and attributes should be private.
Use the same attribute names as given in the question and camel case notation for naming. Name of book and author can have space in between. Sample Input 1:
Enter the Book name: Java Enter the price: 500 Enter the Author name: Einstein

Sample Output 1:
Book Details

Book Name :Java

Book Price :500

Author Name :Einstein

Solution :-
public class Book {

	private String bookName;
	private int bookPrice;
	private String authorName;
	public void setBookName(String bookName) {
	public void setBookPrice(int bookPrice) {
	public void setAuthorName(String authorName) {
	public String getBookName() {
		return this.bookName;
	public int getBookPrice() {
		return this.bookPrice;
	public String getAuthorName() {
		return this.authorName;
	public String toString() {
		return "Book Details\n"+"Book Name:"+bookName+"\n"+"Book Price:"+bookPrice+"\n"+"Author Name:"+authorName;

} :-
import java.util.Scanner;
public class TestBook extends Book {

	public static void main(String[] args) {
		Scanner sc = new Scanner(;
		Book b1=new Book();
		System.out.println("Enter the book name");
		System.out.println("Enter the price");
		System.out.println("Enter the author name");



Leave a Comment

Your email address will not be published. Required fields are marked *