java homework, need help 1.Write the class header and the instance data variable
ID: 3792715 • Letter: J
Question
java homework, need help
1.Write the class header and the instance data variables for the Book class.A book is described by the title, author, publisher, and copyright year. 2.Write a constructor for the Book class. The constructor should initialize the instance data variables using parameters. 3.Write accessor and mutator (getter and setter) methods. Include validity checking where appropriate. 4.Write a toString method to return a nicely formatted, multi-line description of the book. 5.Write code that would go inside of a main method. The code should create two book objects and invoke at least two methods on each object. Also include a statement to print a text representation of each book to the console.
Explanation / Answer
public class Book {
String title, author, publisher,copyright_year;
Book(String t,String a,String p,String c)
{
title=t;
author=a;
publisher=p;
copyright_year=c;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Book b1=new Book("Data strcture","Sahani","Techmax","1996");
Book b2=new Book(" Operating network","Akamla","Technical","2000");
System.out.println("title of book 1 is "+b1.getTitle());
System.out.println("title of book 2 is "+b2.getTitle());
System.out.println("Author of book 1 is "+b1.getAuthor());
System.out.println("Author of book 2 is "+b2.getAuthor());
System.out.println(b1+" "+b2);
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getCopyright_year() {
return copyright_year;
}
public void setCopyright_year(String copyright_year) {
this.copyright_year = copyright_year;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Title "+title+" Author "+author+" publisher "+publisher+" copyright year "+copyright_year;
}
}
========================================================================
Output:
title of book 1 is Data strcture
title of book 2 is Operating network
Author of book 1 is Sahani
Author of book 2 is Akamla
Title Data strcture
Author Sahani
publisher Techmax
copyright year 1996
Title Operating network
Author Akamla
publisher Technical
copyright year 2000
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.