Question

You are going to model a Book in a library. The Book class should contain a...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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;

}

}

Add a comment
Know the answer?
Add Answer to:
You are going to model a Book in a library. The Book class should contain a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You need to program a simple book library system. There are three java classes Book.java   //...

    You need to program a simple book library system. There are three java classes Book.java   // book object class Library.java   //library class A2.java //for testing The Book.java class represents book objects which contain the following fields title: a string which represents the book title. author: a string to hold the book author name year: book publication year isbn: a string of 10 numeric numbers. The book class will have also 3 constructors. -The default no argument constructor - A constructor...

  • Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use impl...

    Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...

  • Suppose we have the following book base class: class book { public: book(char *, char *,...

    Suppose we have the following book base class: class book { public: book(char *, char *, int); void show_book(); private: char title[64]; char author[64]; int pages; }; Suppose that we need to derive a new class, called libraryCard class, which adds the following members to the book class: char catalog[64]; int checked out; // 1 if checked out, otherwise 0 if available libraryCard(char *, char *, int, char *, int); void show_card(); 1. Derive the new class from the base...

  • C++ program Suppose we have the following book base class: class book { public: book(char *,...

    C++ program Suppose we have the following book base class: class book { public: book(char *, char *, int); void show_book(); private: char title[64]; char author[64]; int pages; }; Suppose that we need to derive a new class, called libraryCard class, which adds the following members to the book class: char catalog[64]; int checked out; // 1 if checked out, otherwise 0 if available libraryCard(char *, char *, int, char *, int); void show_card(); 1. Derive the new class from...

  • Subject: Java Program You are writing a simple library checkout system to be used by a...

    Subject: Java Program You are writing a simple library checkout system to be used by a librarian. You will need the following classes: Patron - A patron has a first and last name, can borrow a book, return a book, and keeps track of the books they currently have checked out (an array or ArrayList of books). The first and last names can be updated and retrieved. However, a patron can only have up to 3 books checked out at...

  • Please use C++ Suppose we have the following book base class: class book { public: book(char*,...

    Please use C++ Suppose we have the following book base class: class book { public: book(char*, char *, int); void show_book(); private: char title[64]; char author[64]; int pages; }; Suppose that we need to derive a new class, called libraryCard class, which adds the following members to the book class: char catalog[64]; int checked_out; // 1 if checked out, otherwise 0 if available libraryCard(char*, char *, int, char *, int); void show_card(); 1. Derive the new class from the base...

  • Create an Item class, which is the abstract super class of all Items.           Item class...

    Create an Item class, which is the abstract super class of all Items.           Item class includes an attribute, description of type String for every item. [for eg. Book]                                                             A constructor to initialize its data member of Item class.               Override toString() method to return details about the Item class. Create the ExtraCharge interface with the following details.                       Declare a constant RATE with value 0.25   Declare a method called calculateExtraCharge(), which returns a double value. Create the...

  • Programming language: Java Design an application that has three classes. The first one, named Book describes...

    Programming language: Java Design an application that has three classes. The first one, named Book describes book object and has title and number of pages instance variables, constructor to initialize their values, getter methods to get their values, method to String to provide String representation of book object, and method is Long that returns true if book has over 300 pages, and returns false othewise. Provide also method char firstChard) that returns first cahracter of the book's title. The second...

  • In Java Create an interface called Amount that includes a method called setPrice (). Create an a...

    In Java Create an interface called Amount that includes a method called setPrice (). Create an abstract class named Book that inherits from the interface Amount. Include a String field for the book’s title and a double field for the book’s Price . Within the class, include a constructor that requires the book title and add two getter methods — one that returns the title and one that returns the price. Include an abstract method named setPrice (). Create a...

  • in java Assignment 2 is the first in a multipart series of assignments. The overall topic...

    in java Assignment 2 is the first in a multipart series of assignments. The overall topic of the assignments will be the design and implementation of a system for keeping track of the holdings of a library This first assignment focuses on the development of a custom class to represent a book that belongs to the library. The class should be named "Book" and be based on the corresponding real-world concept of a book, and be able to represent the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT