You are going to model a Book in a library. The Book class should contain a titl
ID: 3594538 • Letter: Y
Question
You are going to model a Book in a library. The Book class should contain a title and an author (exactly 1 author is OK). Also, a book can be checked out and returned by a student. A Book object should be able to identify the student who currently holds the book.
1.Design the public interface (public constructor and public methods) for a class Book. You can use String to represent title, author, and student.
2. Implement the class Book (instance variables and constructor+method bodies) from the previous question.
3.Suppose the library also has other items that can be checked out (e.g., DVDs). Thus we introduce a base class for all items that can be checked out.
public class LibraryItem { ... }
and thus Book can inherit from LibraryItem.
public class Book extends LibraryItem { ... }
Which of the methods of Book's public interface would you move to LibraryItem's public interface?
4.
Extend the Book class and implement a method
boolean equals(Object o) { /* YOUR CODE */ }
that tests whether a book has the same title and author than some other Book.
Explanation / Answer
Below are the required classes for the given problem:
LibraryItem.java:
public class LibraryItem {
private String student, returnDate;
private boolean isCheckedOut;
public String getStudent() {
return student;
}
public void setStudent(String student) {
this.student = student;
}
public String getReturnDate() {
return returnDate;
}
public void setReturnDate(String returnDate) {
this.returnDate = returnDate;
}
public boolean isCheckedOut() {
return isCheckedOut;
}
public void setCheckedOut(boolean isCheckedOut) {
this.isCheckedOut = isCheckedOut;
}
}
Book.java:
public class Book extends LibraryItem{
private String title,author;
public Book(String title,String author) {
this.title = title;
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
//equals to find whether a book has the same title and author than some other Book
@Override
public boolean equals(Object obj) {
//if obj is instance of Book then only we can compare the author and title
if(obj instanceof Book) {
Book temp = (Book)obj;
if(this.title.equals(temp.getTitle()) && this.author.equals(temp.getAuthor())){
return true;
}
}
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.