In this post, we will learn Book Detail Program in java Programming language.
Question:
Create a class Book with the following private member variables
- String bookName
- int bookPrice
- String authorName
Include 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 attrbutes using the setters. Print the output as shown in the sample output using the getters method.
Note: Use the same attribute names as given in the question and camel case notation for methods. 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
CODE:–
public class Book { private String bookName; private int bookPrice; private String authorName; public void setBookName(String bookName) { this.bookName=bookName; } public String getBookName() { return this.bookName; } public void setBookPrice(int bookPrice) { this.bookPrice=bookPrice; } public int getBookPrice() { return this.bookPrice; } public void setAuthorName(String authorName) { this.authorName=authorName; } public String getAuthorName() { return this.authorName; } }
import java.util.Scanner; public class TestBook { public static void main (String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the Book name:"); String bookname=sc.nextLine(); System.out.println("Enter the price:"); int price=sc.nextInt(); sc.nextLine(); System.out.println("Enter the Author name:"); String authorname=sc.nextLine(); Book obj=new Book(); obj.setBookName(bookname); obj.setBookPrice(price); obj.setAuthorName(authorname); System.out.println("Book Details"); System.out.println("Book Name :"+obj.getBookName()); System.out.println("Book Price :"+obj.getBookPrice()); System.out.println("Author Name :"+obj.getAuthorName()); } }