Hello. I\'m struggling with an assignment. I\'m not sure if I\'m just confused w
ID: 3792097 • Letter: H
Question
Hello. I'm struggling with an assignment. I'm not sure if I'm just confused with what is being asked, or if I just don't know how to do it. I have 2 classes listed below. I'll just write down what is being asked of the assignment. "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 write the code for the BookV2 class by re-using the Author class by composition. The class BookV2 should at least have:
-An alternate constructor that takes an array of Author (i.e., Author[]), instead of an Author instance. 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."
So I'll post my 2 classes I have made already. Thank you in advance for the help!
---Author.java---
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;
}
@Override
public String toString() {
return name + "(" + gender + ") " + "at " + email;
}
}
------
---Book.java---
package book;
public class Book {
private String name;
private Author author;
private double price;
public Book(String name, Author author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
public String getName() {
return name;
}
public Author getAuthor() {
return author;
}
public double getPrice() {
return price;
}
public void setPrice(double newPrice) {
price = newPrice;
}
@Override
public String toString() {
return name + " by " + author + " for $" + price;
}
}
------
Explanation / Answer
As mentioned in question, author class is correct. Your confusion is regarding how to related authors to books. In your book Class, there has to be many authors for one book. So you can take array of author class and use in BookV2 class. please see below classes. I hope it clears your doubts.
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;
}
@Override
public String toString() {
return name + "(" + gender + ") " + "at " + email;
}
}
package book;
public class BookV2
{
String name;
private Author[] authors = new Author[10];
double price;
public BookV2(String name, double price)
{
this.name = name;
this.price = price;
}
public String getName()
{
return this.name;
}
public Author[] getAuthors()
{
return this.authors;
}
public double getPrice()
{
return this.price;
}
public void setPrice(double newPrice)
{
this.price = newPrice;
return;
}
public String toString()
{
String ret = "'" + this.name + "' by " + authors.length + " author(s)";
return ret;
}
public void printAuthors()
{
for (int i = 0; i < this.getAuthors().length - 1; ++i)
{
if (this.getAuthors()[i] != null)
{
System.out.print(this.getAuthors()[i]);
if (this.getAuthors()[i + 1] != null)
{
System.out.print(", ");
}
}
else
{
System.out.print("");
}
}
if (this.getAuthors()[this.getAuthors().length - 1] != null)
{
System.out.print(this.getAuthors()[this.getAuthors().length - 1]);
}
else
{
System.out.print("");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.