112k views
2 votes
Write a class named Book that has fields to hold the following data: Title Author Year published ISBN number In the Book class, also include A mutator method for each field to set the value for the field An accessor method for each field to get the value for the field Write a separate demo program that Utilizies the class by creating three instances of the class.

User BobBrez
by
3.3k points

1 Answer

3 votes

Answer:

public class Book {

private String title;

private String author;

private int yearPublished;

private long ISBN;

//The Constructor for all the feilds

public Book(String title, String author, int yearPublished, long ISBN) {

this.title = title;

this.author = author;

this.yearPublished = yearPublished;

this.ISBN = ISBN;

}

//Mutator methods (set) to Set values of all the fields

public void setTitle(String title) {

this.title = title;

}

public void setAuthor(String author) {

this.author = author;

}

public void setYearPublished(int yearPublished) {

this.yearPublished = yearPublished;

}

public void setISBN(long ISBN) {

this.ISBN = ISBN;

}

//Accessor Methods (getters) for all the feilds

public String getTitle() {

return title;

}

public String getAuthor() {

return author;

}

public int getYearPublished() {

return yearPublished;

}

public long getISBN() {

return ISBN;

}

}

// The Demo Class creating three instances of this class

class BookDemo{

public static void main(String[] args) {

Book bookOne = new Book("The Lion and the Princess","David A.",2000,12334556);

Book bookTwo = new Book ("Me and You", "Jane G.", 2001, 4567342);

Book bookThree = new Book("together forever", "Baneely H.",2019,34666);

}

}

Step-by-step explanation:

  • Please pay attention to the comments provided in the solution
  • Using Java Programming Language
  • The class Book is created with the required fields
  • The constructor is created to initialize the values of the fields
  • Getter methods are created
  • Setter Methods are Created
  • A demo Class BookDemo that creates three objects of the Book class is the created (This can be in the same file as the Book class but cannot start with the keyword public or in a separate file with the keyword public class BookDemo)
  • Inside BookDemo class, three instances of the Book class are created and initialized.
User Saif Hamed
by
4.2k points