Question: Hello! I am currently working on a Java program that deals with composition and arrays. I was able to do the first three parts fine

Hello!

I am currently working on a Java program that deals with composition and arrays. I was able to do the first three parts fine (hopefully they are correct), but I do not really know what to do when it comes to the fourth part since it deals with arrays. I will post the objectives of the assignment and the sample code that I have done. Thank you so much!

Assignment #2 Objectives:

Create a Java project named CS235PA2_YourName. Right click src to import the jar file Book (General > Archive File). You will find a class called Author is 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)

Sample Code that I have:

package book;

public class Author {

//Instance Variables

private String name;

private String email;

private char gender;

//Alternate Constructor

public Author (String name, String email, char gender)

{

this.name = name;

this.email = email;

this.gender = gender;

}

//Getter for name

public String getName()

{

return name;

}

//Getter for email

public String getEmail()

{

return email;

}

//Setter for email

public void setEmail (String newEmail)

{

email = newEmail;

}

//Getter for gender

public char getGender()

{

return gender;

}

//toString Method

public String toString()

{

return name + "(" + gender + ")" + " at " + email;

}

} //End of Author class

package book;

public class Book {

//Instance Variables

private Author authorname;

private String bookname;

private double price;

//Alternate Constructor

public Book (Author author, String name, double price) {

this.authorname = author;

this.bookname = name;

this.price = price;

}

//Getter for author

public Author getAuthor(){

return this.authorname;

}

//Getter for name

public String getName(){

return this.bookname;

}

//Getter for price

public double getPrice(){

return this.price;

}

//Setter for price

public void setPrice(double newPrice) {

this.price = newPrice;

}

//toString method

public String toString() {

return bookname + "written by " + authorname.toString() + "for $" + price;

}

}//End of Book class

package book;

public class TestBook {

public static void main(String[] args) {

//Create an author instance

Author auth = new Author("Williams Collins ", "collinsw@lafayette ", 'm');

//Create a book instance

Book b1 = new Book(auth, " Data Structures and the Java Collections Framework ", 112.89);

//Print out book and author's information

System.out.println(b1.toString());

//Change the author's e-mail

auth.setEmail("collinsw@cs.lafayette.edu");

//Print out the updated e-mail address

System.out.println("New e-mail: " + auth.getEmail());

}

}//End of TestBook class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!