Iterator Lab 1

Objective: Understand and implement the iterator design pattern in Java. The iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.

Problem Statement: You are required to create a simple BookShelf application.

Lab Tasks:

Create the necessary Java Classes

You will need to create the following classes:

  • Iterator.java: This is the interface that will contain the hasNext() and next() methods.

  • Book.java: This class will represent a book with properties like 'title' and 'author'.

  • BookShelf.java: This class will represent a collection of books.

  • BookIterator.java: This class will implement the Iterator interface for the BookShelf class.

Implement the Iterator Interface

In the BookIterator class, implement the Iterator interface and override the hasNext() and next() methods.

Implement the Book and BookShelf Classes

The Book class should have appropriate constructors, getters, and setters. The BookShelf class should have methods to add a book, remove a book, get a book based on index, and return an iterator for its books. (Hint: you might want to use a List in your Bookshelf - private List<Book> books;)

Test Your Implementation

Create a Main class that will create a BookShelf, add some Book instances to it, and then use a BookIterator to iterate through the books and print out their details.

Example output: