New assignment that I need help. I need to modify the Book class to accommodate
ID: 3552466 • Letter: N
Question
New assignment that I need help. I need to modify the Book class to accommodate multiple authors using one of the components from the Java Collection Framework. Can you help? Thanks! (Please only answer in Java codes) I just noticed that I need to revise the boolean equals. and validate part since they are not right? I'm so lost!!!
Here is the codes that I already have for class Book:
package book;
public class Book {
private String author;
private String title;
private String isbn;
public Book() {
super();
isbn="";
author="";
title = "";
}
public Book(String author, String title, String isbn) {
this.author = author;
this.title = title;
this.isbn = isbn;
}
public String getAuthor(){
return this.author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle(){
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getIsbn() {
return this.isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public boolean equals(Object obj) {
Book book = (Book) obj;
return this.getAuthor().equals(getAuthor()) && (this.getIsbn().equals(getIsbn())) && (this.getTitle().equals(getTitle()));
}
public boolean validate(){
return (this.getIsbn().trim()!= null) && (this.getAuthor().trim()!= null) && (this.getTitle().trim()!=null);
}
}
Explanation / Answer
public class Book {
private String[] author;
private String title;
private String isbn;
private int n;// no.of authors
public Book() {
super();
isbn="";
author= null;
title = "";
}
public Book(String autho[],String title, String isbn, int k) {// take input as array of strings of authors and then k gives the no. of authors
this.author = new String[k];
for(int i=0; i<k;i++){this.author[i]=autho[k];}
this.title = title;
this.isbn = isbn;
this.n= k;
}
public String[] getAuthor(){
return this.author;// after returning this.author you can find the length of the string array using string.lenth
// and then print all the authors
}
public void setAuthors(String[] author, int k) {//take the input as string array of authors and then set
this.n=k;
this.author= new String[k];
for(int i=0; i<k;i++){this.author[i] = author[i];}
}
public String getTitle(){
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getIsbn() {
return this.isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public boolean equals(Book obj) {
return this.getAuthor().equals(obj.getAuthor()) && (this.getIsbn().equals(obj.getIsbn())) && (this.getTitle().equals(obj.getTitle()));
}
public boolean validate(){
return (this.getIsbn().trim()!= null) && (this.getAuthor()!= null) && (this.getTitle().trim()!=null);
}
}