Write the following program as a Java project with three jaya files. The three c
ID: 3581797 • Letter: W
Question
Write the following program as a Java project with three jaya files. The three classes are Book, Booklist, and TestBooks (the latter one includes main). Use these names for your classes. Book Class represents a book and can be online, digital, or hardcopy. Books have a title, author, number of pages, and a publication year (or year of last revision if online). Required items beyond the basics to set up a properly structured class): A constructor that takes input in order of title, author, publication year, and number of pages as two strings and two ints. 2. A getPages method that returns the number of pages in the book at this time. 3. A toString0 method that returns a String suitable for printing out the key information about the book. You decide the format but it should all be one line when the string is printed. Be sure to include the number of pages in the book in the returned string. 4. An updateBook method that works with an existing book to change its publication year and number of pages.Explanation / Answer
import java.util.ArrayList;
import java.util.List;
class Book{
private String title;
private String author;
private int totalPages;
private int publicationYear;
/**
* @param title
* @param author
* @param totalPages
* @param publicationYear
*/
public Book(String title, String author, int totalPages, int publicationYear) {
this.title = title;
this.author = author;
this.totalPages = totalPages;
this.publicationYear = publicationYear;
}
/**
* @return the totalPages
*/
public int getTotalPages() {
return totalPages;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "title=" + title + ", author=" + author + ", totalPages=" + totalPages + ", publicationYear="
+ publicationYear + "";
}
/**
*
* @param pages
* @param year
*/
public void updateBook(int pages,int year){
this.publicationYear=year;
this.totalPages=pages;
}
}
class BookList{
private List<Book>list=new ArrayList<>();
String name;
/**
* @param list
*/
public BookList(String name) {
this.name = name;
}
/**
*
* @param book
*/
public void addBook(Book book){
this.list.add(book);
}
/**
*
* @return
*/
public int numberOfBooks(){
return list.size();
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
String info=""+name+" : ";
for (Book book:list) {
info+=book+" ";
}
return info;
}
public int totalPages(){
int total=0;
for (Book book:list) {
total+=book.getTotalPages();
}
return total;
}
}
public class TestBooks {
public static void main(String[] args) {
Book book=new Book("Alice in wonderland", "Lewis carrol", 40, 1856);
Book book1=new Book("Jabberwocky", "Lewis carrol", 40, 1851);
Book book4=new Book("Jungle book", "Rudyard Keepling", 100, 1952);
BookList list=new BookList("Adventure List");
list.addBook(book);
list.addBook(book1);
list.addBook(book4);
book.updateBook(69, 1865);
book1.updateBook(80, 1871);
Book book2=new Book("Friends", "James", 28, 1972);
Book book3=new Book("Girls", "Joseph", 68, 1942);
BookList list1=new BookList("Comedy List");
list1.addBook(book2);
list1.addBook(book3);
System.out.println(list);
System.out.println("-------------------------------------------------------------");
System.out.println(list1);
}
}
-------------------------------------------------------------------------------output----------------------------------------------------------------
Adventure List :
title=Alice in wonderland, author=Lewis carrol, totalPages=69, publicationYear=1865
title=Jabberwocky, author=Lewis carrol, totalPages=80, publicationYear=1871
title=Jungle book, author=Rudyard Keepling, totalPages=100, publicationYear=1952
-------------------------------------------------------------
Comedy List :
title=Friends, author=James, totalPages=28, publicationYear=1972
title=Girls, author=Joseph, totalPages=68, publicationYear=1942
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.