Java Exercise 1.1
You are tasked with designing a system for managing a library's collection of items. Implement the following classes and interfaces:
Interface: Readable
- This interface should define at least one method related to reading or accessing information.
Class: Item
- This class should represent an item in the library collection.
- It should have appropriate instance variables and methods based on the requirements of the system.
Create two additional classes that implement the Readable
interface.
- Each class should have its own unique set of instance variables and methods, based on the type of item it represents.
In a Main
class, create instances of the implemented classes and demonstrate their usage by invoking the methods defined in the Readable
interface.
Note: You have the freedom to decide the specific requirements, instance variables, and methods for each class, as long as they align with the concept of objects and interfaces.
Example Solution
interface Readable {
void read();
}
class Item {
private String title;
private boolean available;
public Item(String title) {
this.title = title;
this.available = true;
}
public String getTitle() {
return title;
}
public boolean isAvailable() {
return available;
}
public void setAvailable(boolean available) {
this.available = available;
}
}
class Book extends Item implements Readable {
private String author;
private boolean read;
public Book(String title, String author) {
super(title);
this.author = author;
this.read = false;
}
@Override
public void read() {
if (isAvailable()) {
System.out.println("Reading the book: " + getTitle());
read = true;
} else {
System.out.println("The book is not available.");
}
}
@Override
public boolean isRead() {
return read;
}
@Override
public String toString() {
return "Book - Title: " + getTitle() + ", Author: " + author + ", Read: " + read;
}
}
class Magazine extends Item implements Readable {
private int issueNumber;
private boolean read;
public Magazine(String title, int issueNumber) {
super(title);
this.issueNumber = issueNumber;
this.read = false;
}
@Override
public void read() {
if (isAvailable()) {
System.out.println("Reading the magazine: " + getTitle());
read = true;
} else {
System.out.println("The magazine is not available.");
}
}
@Override
public boolean isRead() {
return read;
}
@Override
public String toString() {
return "Magazine - Title: " + getTitle() + ", Issue Number: " + issueNumber + ", Read: " + read;
}
}
public class Main {
public static void main(String[] args) {
// Create instances of Book and Magazine
Book book = new Book("The Great Gatsby", "F. Scott Fitzgerald");
Magazine magazine = new Magazine("National Geographic", 2023);
// Demonstrate reading the items
book.read();
magazine.read();
// Print out the string representation of each object
System.out.println(book);
System.out.println(magazine);
}
}
In this solution, we have an Item
class that represents a general item in the library collection. The Book
and Magazine
classes inherit from Item
and implement the Readable
interface.
The Book
class has additional instance variables author
and read
to represent the book's author and whether it has been read. It overrides the read()
method to print a message when reading the book and updates the read
flag accordingly. It also overrides the isRead()
method to return the value of the read
flag. The toString()
method is overridden to provide a string representation of the book.
Similarly, the Magazine
class has additional instance variables issueNumber
and read
to represent the magazine's issue number and whether it has been read. It overrides the read()
method and the isRead()
method, and also overrides the toString()
method.
In the Main
class, we create instances of Book
and Magazine
and demonstrate their usage by invoking the read()
method. We then print out the string representation of each object using the toString()
method.