Using Java to create the following classes below. 1. Create a Book class: a) The
ID: 3860339 • Letter: U
Question
Using Java to create the following classes below.
1. Create a Book class:
a) There are fields for the author’s first and last names (use a Name class you create), the title of the book (make this protected, not private), and the year published (an int).
b) Create a constructor that accepts parameters for all four fields.
c) The constructor must call its own mutator methods for the following behaviors (d, e, below).
d) The setYearPublished mutator must throw an InvalidBookDateException (you will have to create this class) if the date parameter is greater than 2016.
e) The setFirstName, setLastName, and setTitle mutators must throw an InvalidArgumentException (you will have to create this class) if their parameter is null or an empty string. Call these methods from the constructor.
f) Provide accessors, mutators, equals(), hashCode(), and a toString() method. The accessors and mutators are final.
g) Implement Comparable; more-recent books are bigger.
2. Create a Bookstore class:
a) There is a single field which is an ArrayList<Book>.
b) Implement an addBook() method which tries to create a new Book and adds it to the Bookstore. The addBook() method should accept parameters for the new Book author’s first and last names, the title of the book, and the year published; it must also catch any thrown Exceptions.
c) Implement a displayBooks() method which prints books before and after sorting them.
3. Create a Biography class, which is final. It extends Book. It adds a field called subject which is a Name object. Override equals again; books are equal if they are biographies of the same subject.
And Obviously a Main class(tester) if it needs one.
Please follow all the instructions above to be rated.
Explanation / Answer
Please contact if need any further help
Code
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class Book implements Comparable {
protected String firstName;
protected String lastName;
protected String title;
protected int year_published;
public Book(String aFirstName, String aLastName, String aTitle, int aYear_published)
throws InvalidBookDateException, InvalidArgumentException {
this.setFirstName(aFirstName);
this.setLastName(aLastName);
this.setTitle(aTitle);
this.setYear_published(aYear_published);
}
public final String getFirstName() {
return firstName;
}
public final void setFirstName(String aFirstName) throws InvalidArgumentException {
if (aFirstName == null || aFirstName.isEmpty())
throw new InvalidArgumentException("First name is null or empty");
else
firstName = aFirstName;
}
public final String getLastName() {
return lastName;
}
public final void setLastName(String aLastName) throws InvalidArgumentException {
if (aLastName == null || aLastName.isEmpty())
throw new InvalidArgumentException("Last name is null or empty");
else
lastName = aLastName;
}
public final String getTitle() {
return title;
}
public final void setTitle(String aTitle) throws InvalidArgumentException {
if (aTitle == null || aTitle.isEmpty())
throw new InvalidArgumentException("Title name is null or empty");
else
title = aTitle;
}
public final int getYear_published() {
return year_published;
}
public final void setYear_published(int aYear_published) throws InvalidBookDateException {
if (aYear_published > 2016)
throw new InvalidBookDateException("Year greater than 2016");
else
year_published = aYear_published;
}
@Override
public String toString() {
return "Book [firstName=" + firstName + ", lastName=" + lastName + ", title=" + title + ", year_published="
+ year_published + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
result = prime * result + ((title == null) ? 0 : title.hashCode());
result = prime * result + year_published;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Book other = (Book) obj;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
if (year_published != other.year_published)
return false;
return true;
}
@Override
public int compareTo(Object aArg0) {
Book b = (Book) aArg0;
return this.year_published - b.year_published;
}
}
class BookStore {
ArrayList<Book> al;
public BookStore() {
al = new ArrayList<Book>();
}
void addBook(String firstName, String lastName, String title, int year_published)
throws InvalidBookDateException, InvalidArgumentException {
Book b = new Book(firstName, lastName, title, year_published);
al.add(b);
}
void displayBooks() {
System.out.println("Before sorting");
System.out.println(al);
Collections.sort(al);
System.out.println("After sorting");
System.out.println(al);
}
}
final class Biography extends Book {
String subject;
public Biography(String aSubject,String aFirstName, String aLastName, String aTitle, int aYear_published)
throws InvalidBookDateException, InvalidArgumentException {
super(aFirstName, aLastName, aTitle, aYear_published);
subject = aSubject;
}
@Override
public boolean equals(Object obj) {
Biography b = (Biography) obj;
if (this.subject.equals(b.subject))
return true;
return false;
}
}
public class Book_Tester {
public static void main(String[] args) {
// Scanner in = new Scanner(System.in);
// System.out.println("Enter first name of author");
// String fn = in.next();
// System.out.println("Enter last name of author");
// String ln = in.next();
// System.out.println("Enter title of the book");
// String t = in.next();
// System.out.println("Enter year of publication");
// int y = in.nextInt();
// code to verify constructor for null entries
// Book b = new Book(fn,ln,t,y);
try {
Book b;
// uncomment intantiation line to verify
//System.out.println("checking with first name as null");
//b = new Book("", "ln", "t", 2015);
//System.out.println("checking with last name as null");
// b = new Book("fn", "", "t", 2015);
//System.out.println("checking with title as null");
// b = new Book("fn", "ln", "", 2015);
//System.out.println("checking with publication year greater than 2016");
// b = new Book("fn", "ln", "t", 2020);
// to verify to string method
b = new Book("fn","ln","t",2014);
System.out.println(b.toString());
//to verify equals method
Book b1 = new Book("fn","ln","t",2014);
System.out.println(b.equals(b1));
Book b2 = new Book("fn1","ln","t",2014);
System.out.println(b.equals(b2));
//toverify hashcode method
System.out.println(b.hashCode());
System.out.println(b1.hashCode());
System.out.println(b2.hashCode());
//to verify bookstore methods and accessors of Book class
BookStore bs = new BookStore();
bs.addBook(b.getFirstName(), b.getLastName(), b.getTitle(), b.getYear_published());
bs.addBook("fn1", "ln1", "t1", 2015);
bs.addBook("fn2", "ln2", "t2", 2010);
bs.displayBooks();
//to verify Biography
Biography bg1 = new Biography("history","fn1", "ln1", "t1", 2015);
Biography bg2 = new Biography("history","fn1", "ln2", "t1", 2014);
Biography bg3 = new Biography("economics","fn1", "ln1", "t1", 2015);
System.out.println(bg1.equals(bg2));
System.out.println(bg1.equals(bg3));
} catch (InvalidBookDateException | InvalidArgumentException e) {
System.out.println(e.getMessage());
}
}
}
Sample output
Book [firstName=fn, lastName=ln, title=t, year_published=2014]
true
false
101728421
101728421
-1267494556
Before sorting
[Book [firstName=fn, lastName=ln, title=t, year_published=2014], Book [firstName=fn1, lastName=ln1, title=t1, year_published=2015], Book [firstName=fn2, lastName=ln2, title=t2, year_published=2010]]
After sorting
[Book [firstName=fn2, lastName=ln2, title=t2, year_published=2010], Book [firstName=fn, lastName=ln, title=t, year_published=2014], Book [firstName=fn1, lastName=ln1, title=t1, year_published=2015]]
true
false
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.