1 A class named Book contains the following i A string insta
1. A class named Book contains the following:
i. A string instance variable named title for the book\'s title
ii. A string instance variable named author for the book\'s author.
iii. A string instance variable named ISBN for the book\'s ISBN number.
iv. An int instance variable named yearPublished for the year the book being published.
v. A double instance variable named price for the price of the book.
vi. Aconstructor that creates book information with specified title, author, ISBN, yearPublished and price.
vii. The accessor methods for all attributes.
viii. The mutator methods for all attributes.
a. Write the java class named Book with the specified instance variables and methods.
b. Write the test program (i.e. Java application) named BookApplication that creates an array of 5 book objects with the values you set it
and call the following tatic methods:
- displayBooks() displaythe information of all the books.
- calcTotalPrice() returned a double data type which represents the total price of all the books
Solution
package d1;
public class book{
String title;
String author;
String isbn;
int yearpurchased;
double price;
double totalprice;
book(){
}
book(String title,String author,String isbn,int yearpurchased,double price){
Settitle(title);
setauthor(author);
setisbn(isbn);
setyearpurchased(yearpurchased);
setprice(price);
}
public String gettitle(){
return title;
}
public String getauthor(){
return author;
}
public String getisbn(){
return isbn;
}
public int getyearpurchased(){
return yearpurchased;
}
public double getprice(){
return price;
}
public void setprice(double price) {
this.price=price;
}
public void setyearpurchased(int yearpurchased) {
this.yearpurchased=yearpurchased;
}
public void setisbn(String isbn) {
this.isbn=isbn;
}
public void setauthor(String author) {
this.author=author;
}
public void Settitle(String title) {
this.title=title;
}
public String tostring(){
return \"author:\"+author+\"\\ttitle:\"+title+\"\\tisbn:\"+isbn+\"\\tyearpurchased:\"+yearpurchased+\"\\tprice\"+price;
}
public void displaybooks(){
System.out.println(tostring());
}
public double calctotalprice(){
totalprice=price;
return totalprice;
}
}
main class;
package d1;
public class BookApplication {
public static void main(String[] args) {
double totalprice=0.0;
double total=0.0;
book b[]=new book[5];
b[0]=new book(\"swa\",\"ban\",\"jyo\",2322,1.0);
b[1]=new book(\"ajd\",\"ssn\",\"jkach\",032,1.0);
b[2]=new book(\"raj\",\"zenn\",\"midt\",44,3.0);
b[3]=new book(\"seshu\",\"ban\",\"tomob\",234,2.0);
b[4]=new book(\"jphn\",\"ban\",\"kitur\",323,2.0);
for(int i=0;i<5;i++)
b[i].displaybooks();
for(int i=0;i<5;i++){
totalprice= b[i].calctotalprice();
total+=totalprice;
}
System.out.println(\"totalprice of books:\"+total);
}
}
output:
author:ban title:c isbn:jyo yearpurchased:2322 price1.0
author:joh title:java isbn:jkach yearpurchased:26 price1.0
author:zenn title:oracle isbn:midt yearpurchased:844 price3.0
author:lary title:os isbn:tomob yearpurchased:23894 price2.0
author:abc title:database isbn:kitur yearpurchased:33 price2.0
totalprice of books:9.0


