The document describes a lab assignment to create a Book class with instance variables for the book name, ISBN number, author, and publisher. The Book class includes getter and setter methods for each instance variable as well as a method to return a string with all the book details. A test application BookTest is created to demonstrate the Book class by initializing an array of 30 Book objects and displaying the book information.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
85 views7 pages
Lab Assignment - 3: Fall Semester 2020-21
The document describes a lab assignment to create a Book class with instance variables for the book name, ISBN number, author, and publisher. The Book class includes getter and setter methods for each instance variable as well as a method to return a string with all the book details. A test application BookTest is created to demonstrate the Book class by initializing an array of 30 Book objects and displaying the book information.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7
Lab Assignment – 3
Fall Semester 2020-21
Name – Tanay Dubey
Registration Number – 19BCT0185 Course Code and Name – CSE1007 Java Programming Faculty Name – Gopichand G Slot – L27 + L28 Question -: Create a class called Book to represent a book. A Book should include four pieces of information as instance variables‐a book name, an ISBN number, an author name and a publisher. Your class should have a constructor that initializes the four instance variables. Provide a mutator method and accessor method (query method) for each instance variable. Inaddition, provide a method named getBookInfo that returns the description of the book as a String (the description should include all the information about the book). You should use this keyword in member methods and constructor. Write a test application named BookTest to create an array of object for 30 elements for class Book to demonstrate the class Book's capabilities. Solution -: Code import java.io.*; class Book{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String book_name; int isbn; String author; String publisher;
System.out.println("Enter book name"); book_name = br.readLine(); } public void getisbn()throws IOException{ System.out.println("Enter isbn number "); isbn = Integer.parseInt(br.readLine()); } public void getauthor()throws IOException { System.out.println("Enter author name"); author = br.readLine(); } public void getpublisher()throws IOException{ System.out.println("Enter publisher name"); publisher = br.readLine(); } public void setbook_name(String book) { this.book_name=book; } public void setisbn(int isbn) { this.isbn=isbn; } public void setauthor(String author) { this.author=author; } public void setpublisher(String publisher) { this.publisher=publisher; }
public void displayinfo()
{ System.out.println("\n Book name "+book_name); System.out.println("\n ISBN number "+isbn); System.out.println("\n Author name "+author); System.out.println("\n Publisher name "+publisher); } }
public class TestBook
{ public static void main(String [] args)throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Book [] book = new Book [30]; int i=0; System.out.println(" How many books info you want to store "); int n = Integer.parseInt(br.readLine()); System.out.printf("you want to give the values by get method or set method \nEnter 1 for get method \n 2 for set method\n"); int ch = Integer.parseInt(br.readLine()); try { if(ch==1) { for(i=0;i<n;i++) { System.out.printf("\n Enter %d book Details\n",i+1); book[i]=new Book(); book[i].getbook_name(); book[i].getisbn(); book[i].getauthor(); book[i].getpublisher(); } } else { book[i]=new Book(); book[i].setbook_name("DS And Algo"); book[i].setisbn(1234567890); book[i].setauthor("CLRS"); book[i].setpublisher("Pearson"); } } catch(Exception ex) { System.out.println(ex); } for(i=0;i<n;i++) { book[i].displayinfo(); System.out.println(); } } }