Question
Using a class Author , define a class Book , which contains: Three private instance variables: name (String), author (of the class Author you defined,
Using a class Author, define a class Book, which contains:
Three private instance variables: name (String), author (of the class Author you defined, assume that each book has one and only one author), and price (double);
One constructor:
public Book (String name, Author author, double price)
Public methods:
getName(), getAuthor(), getPrice(), setPrice()
toString() that returns "'bookName' by authorName (gender) at email". (Take note that the Author's toString() method returns "authorName (gender) at email".)
Define a driver class (with main method) called TestBook to test the class Book. Notice that you have to construct an instance of Author before you can construct an instance of Book. You are asked to do the following things in main of TestBook:
Create an author instance: William Collins, collinsw@lafayette.edu, m (male)
Create a book instance written by William Collins: Data Structures and the Java Collections Framework, $112.89
Print the above book and its authors information (test toString method)
Change the authors email to collinsw@cs.lafayette.edu from the book instance created in b)
Print the (updated) email of the author from the book instance created in b)
In the earlier defined class Book, a book is written by one and only one author. In reality, a book can be written by one or more authors. Define another class BookV2 to support one or more authors by using an Author array as instance variable.
You are required to:
1) Write the code for the BookV2 class. You shall re-use the Author class by composition. The class BookV2 should at least have:
An alternate constructor that takes an array of Author (i.e., Author[]). You need to do deep copy with the array of Author. You are allowed to add a copy constructor in the class Author if you need.
The toString() method that returns "bookName by n authors", where n is the number of authors.
A method printAuthors() to print the names of all the authors.
2) Define another driver class (with main method) called TestBookV2 to test the BookV2 class:
a) Create a book instance Head First Object-Oriented Analysis & Design, $59.99 written by three authors: (test constructor)
Brett McLaughlin, brett@oreilly.com, m (male)
Gary Police, gpolice@cs.wpi.edu, m (male)
David West, dwest@ivarjacobson.com, m (male)
b) Print the above book name and number of authors (test toString() method)
c) printing the names of all the authors of the book instance just created (test printAuthors() method)
CLASS AUTHOR*****
package book;
public class Author {
private String name;
private String email;
private char gender;
public Author (String name, String email, char gender)
{
this.name = name;
this.email = email;
this.gender = gender;
}
public String getName()
{
return name;
}
public String getEmail()
{
return email;
}
public void setEmail (String newEmail)
{
email = newEmail;
}
public char getGender()
{
return gender;
}
public String toString()
{
return name + "(" + gender + ")" + "at" + email;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started