Write the following program as a Java project with four -java files. The four cl
ID: 3721370 • Letter: W
Question
Write the following program as a Java project with four -java files. The four classes are Book, Booklist Person, and TestBooks (the latter one includes main). Use these names for your classes. Use the provided Person class as is (you do not need to modify it) Book Class: represents a book. Books have a title, author, number of pages, and a publication year (or ycar of last revision if online). The author is an object of class Person (use the class provided). Required items (beyond setting up a properly structured class) 1. A constructor that takes input (in order) of title, author,_number of pages, and publication ycar as a String, a Person, and two ints. The author input parameter must bc a Person object. 2. A getPages method that returns the number of pages in thc 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 as part of the returned string 4. An updateBook method that works with the existing book to change its publication year and number of pages (get new values as parameters) No need to do get and set methods for cach piece of data unless you need them for other required work Booklist Class: used to keep track of some number of books. A book can be on zero, one, or more lists at any time. Each BookList object has a name of the list and knows or calculates the number of books in the list. Be sure Bookist objects can be printed from main. Required items (beyond setting up a properly structured class) 1. A constructor that takes input of a list name and a maximum number of books to be on the list as a String and an int. You can ignore the maximum number of items if you don't need but still allow it to be specified in the constructor call. You can decide how you wish to store the books in the list. it in your program 2. A addBook method that takes as input a Book object and adds it to the list. The input parameter must be a Book object that already exists. You do not need to worry about books that are possibly duplicated in the list. Each one counts as a book on the list. Think of it as keeping track of how many times I have read this book for example. 3. A number OfBooks method that returns as an int the actual number of books currently in the list (not the maximum number possible)Explanation / Answer
The code was not having Person class mentioned, So I have not added Person class. If you can provide Person class, then I'll add it.. else comment , I'll help you..
Book.java
package books;
class Book {
private String title;
private Person author;
private int totalPages;
private int publicationYear;
/**
* @param title
* @param author
* @param totalPages
* @param publicationYear
*/
public Book(String title, Person author, int totalPages, int publicationYear) {
this.title = title;
this.author = author;
this.totalPages = totalPages;
this.publicationYear = publicationYear;
}
/**
* @return the totalPages
*/
public int getPages() {
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;
}
}
BookList.java
package books;
class BookList {
private List<Book> list = new ArrayList<>();
String name;
/**
* @param list
*/
public BookList(String name,int numOfBooks) {
this.name = name;
}
/**
*
* @param book
*/
public void addBook(Book book) {
this.list.add(book);
}
/**
*
* @return
*/
public int numberOfBooks() {
return list.size();
}
public String getListName() {
return name;
}
/*
* (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.getPages();
}
return total;
}
}
TestBooks.java
package books;
public class TestBooks {
public static void main(String[] args) {
BookList list = new BookList("Adventure List",20);
BookList list1 = new BookList("Comedy List",10);
Person per = new Person("Lewis","carrol",26,3,1992);
Person per1 = new Person("Rudyard","Keepling",19,3,1984);
Book book = new Book("Alice in wonderland", per, 40, 1856);
Book book1 = new Book("Jabberwocky", per, 40, 1851);
Book book4 = new Book("Jungle book", per1, 100, 1952);
list.addBook(book);
list.addBook(book1);
list.addBook(book4);
book.updateBook(69, 1865);
book1.updateBook(80, 1871);
Book book2 = new Book("Friends", per1, 28, 1972);
Book book3 = new Book("Girls", per, 68, 1942);
list.addBook(book2);
list.addBook(book3);
System.out.println(list);
System.out.println("-------------------------------------------------------------");
System.out.println(list1);
System.out.println("total number of pages on first list : "+list.totalPages());
System.out.println("total number of pages on second list : "+list1.totalPages());
}
}
Output
Adventure List :
title=Alice in wonderland, author=Person LEWIS,carrol/1992-03-26/, totalPages=69, publicationYear=1865
title=Jabberwocky, author=Person LEWIS,carrol/1992-03-26/, totalPages=80, publicationYear=1871
title=Jungle book, author=Person RUDYARD,Keepling/1984-03-19/, totalPages=100, publicationYear=1952
title=Friends, author=Person RUDYARD,Keepling/1984-03-19/, totalPages=28, publicationYear=1972
title=Girls, author=Person LEWIS,carrol/1992-03-26/, totalPages=68, publicationYear=1942
-------------------------------------------------------------
Comedy List :
total number of pages on first list : 345
total number of pages on second list : 0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.