Bookshelf Stack ISC 2016

A bookshelf is designed to store the books in a stack with LIFO (Last In First Out) operation. Define a class Book with the following specifications:

Class name: Book
Data members/instance variables:
name[]: stores the names of the books.
point: stores the index of the topmost book.
max: stores the maximum capacity of the bookshelf.
Methods/Member functions:
Book(int cap): constructor to initialize the data members max = cap and point = -1.
void tell(): displays the name of the book which was last entered in the shelf. If there is no book left in the shelf, displays the message “SHELF EMPTY”
void add(String v): adds the name of the book to the shelf if possible, otherwise displays the message “SHELF FULL”.
void display(): displays all the names of the books available in the shelf.

Specify the class Book giving the details of only the functions void tell() and void add(String). Assume that the other functions have been defined.

The main function need not be written.

class Book{
    public void tell(){
        if(point == -1)
            System.out.println("SHELF EMPTY");
        else
            System.out.println(name[point]);
    }
    public void add(String v){
        if(point + 1 < max)
            name[++point] = v;
        else
            System.out.println("SHELF FULL");
    }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.