Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using Java to create the following classes below. 1. Create a Book class: a) The

ID: 3859054 • 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 the instructions above for a good rate.

Explanation / Answer

import java.util.*;

/**
*
* @author Deepak
*/
class InvalidArgumentException extends Exception {
  
}

class InvalidBookDateException extends Exception {
  
}

public class Book {
String firstName;// members mentioned in problem
String lastName;
String title;
int year;
  

// constructor for book
Book(String firstName_, String lastName_, String title_, int year_) throws InvalidArgumentException
{
//
try
{
setFirstName(firstName_);
setLastName(lastName_);
setTitle(title_);
}
catch (InvalidArgumentException e)
{
System.out.println("Caught exepction: " + e);
}
try
{
setYearPublished(year_);
}
catch (InvalidBookDateException e)// catch excepion
{
System.out.println("Caught Exception: " + e);
}
  
}
  
  
  
void setFirstName(String firstName_) throws InvalidArgumentException
{
if (firstName_ != null || firstName_ == "")
firstName = firstName_;
else
throw new InvalidArgumentException();
}
  
void setLastName(String lastName_) throws InvalidArgumentException
{
if (lastName_ != null || lastName == "")
lastName = lastName_;
else
throw new InvalidArgumentException();
}
  
void setTitle(String title_)throws InvalidArgumentException
{
if (title_ != null || title_ == null)
title = title_;
else
throw new InvalidArgumentException();
}
void setYearPublished(int year_)throws InvalidBookDateException
{
if (year <= 2016)
year = year_;
else
throw new InvalidBookDateException();
  
}
  
public boolean equal(Book book)
{
return this.equal(book);
}
  
public String toString()
{
return this.toString();
}
  
public int hashCode()
{
return this.hashCode();
}
  
public boolean Comparable(Book book)
{
return this.year > book.year;
}
  
}

class Bookstore// make list of books
{
ArrayList<Book> list = new ArrayList<Book>();
  
void addBook(String firstName, String lastName, String title, int year)
{
try
{
  
Book book = new Book(firstName, lastName, title, year);
  
}
catch(Exception e)
{
System.out.println("Caught Exception: " + e);
}
}
}

class Biography extends Book
{
String subject;
//Biography()
public Biography(String firstName_, String lastName_, String title_, int year_) throws Exception
{
super(firstName_, lastName_, title_, year_);
}
  
boolean equals(Biography book)
{
return this.subject == book.subject;
}   
}

To test the code you can use this test class

public class Test
{
static void main(String[] args) throws Exception
{
//{
// static void main(String[] args) throws Exception
// {
Book book1 = new Book("Bill", "Clinton", "War", 1990);
Book book2 = new Book("George", "Bush", "War", 1991);
System.out.println(book1.firstName + " " + book1.lastName + " " + book1.title + " " + book1.year);
System.out.println(book2.firstName + " " + book2.lastName + " " + book2.title + " " + book2.year);

// }
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote