5.8k views
3 votes
1. Create a class named MyBookShelf. The class contains two private instance variables (class variables) named catalog and bookCount. Catalog is an instance variable that references an array of elements of class Book of length 10. bookCount is an int variable counts the number of books on the shelf (number of created instances from this class). The class MyBookShelf should contain the following methods: add book (String title, String author): adds a new book (by title and author) to the inventory. This method checks if the number of books in the inventory is greater than 10. If so, it will print out on the screen "Sorry, the shelf is full". Otherwise, a new instance is created from the class Book and added as the last entry to the array catalog. Lastly the method increases the bookCount variable by 1, indicating that a new instance is created.

User Opike
by
8.2k points

1 Answer

5 votes

Final answer:

The MyBookShelf class holds Book objects and manages the addition of new books with the add book method, while keeping track of the number with bookCount. If there is space, a book is added; if not, 'Sorry, the shelf is full' is printed.

Step-by-step explanation:

Implementing the MyBookShelf Class

To create a class MyBookShelf that holds an array of Book instances, we start by defining two private instance variables: catalog, which is an array of Book objects with a fixed length of 10, and bookCount, an integer that tracks the number of books currently on the shelf. The class includes a method add book that takes a book's title and author as parameters. When invoked, this method checks if bookCount is less than 10, indicating there is space on the shelf. If there is room, it creates a new Book instance with the provided title and author, adds it to the catalog array, and increments bookCount by 1. If the shelf is full, meaning bookCount is 10 or more, the method prints out "Sorry, the shelf is full." By managing books this way, the MyBookShelf class mimics a real-world bookshelf with a limited capacity, helping students learn about object-oriented programming and array management in a practical context.

User Guido Hendriks
by
8.3k points