Java program to print book details.

java

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.
Note:
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

Book.java :-
public class Book {

	private String bookName;
	private int bookPrice;
	private String authorName;
	public void setBookName(String bookName) {
		this.bookName=bookName;
	
	}
	public void setBookPrice(int bookPrice) {
		this.bookPrice=bookPrice;
	
	}
	public void setAuthorName(String authorName) {
		this.authorName=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;
	}
	

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

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

	}

}

Leave a Comment

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