Create a class called Book that contains instance variables like
BKName, BKId and BKAuthor, a parameterized constructor to
initialize its instance variables, a method BKUpdateDetails(String
name, int id, String author), that accepts new values for name, Id and
author as parameters and updates the corresponding instance variable
values of that object and another method BKDisplay() to display the
book details. Create a class BookDemo and provide main method for
instantiate a Book object, display the original book details, update its
details with new values, and display the updated book details.
class Book {
private String BKName;
private int BKId;
private String BKAuthor;
/**
* Constructor
*
* @param BKName
* @param BKId
* @param BKAuthor
*/
public Book(String BKName, int BKId, String BKAuthor) {
this.BKName = BKName;
this.BKId = BKId;
this.BKAuthor = BKAuthor;
}
public void BKUpdateDetails(String name, int id, String author) {
this.BKName = name;
this.BKId = id;
this.BKAuthor = author;
}
public void BKDisplay() {
System.out.println("BKName: " + this.BKName);
System.out.println("BKId: " + this.BKId);
System.out.println("BKAuthor: " + this.BKAuthor);
}
}
public class BookDemo {
/**
* The start point of the program
*
* @param args
*
*/
public static void main(String[] args) {
// main method for instantiate a Book object, display the original book details,
// update its
Book newBook = new Book("C++", 45456, "Peter Smith");
// details with new values, and display the updated book details.
newBook.BKDisplay();
newBook.BKUpdateDetails("Java", 2326, "Mary Clark");
newBook.BKDisplay();
}
}
Comments
Leave a comment