icse-promo

Question

A bookshelf is designed to store the books in a stack with LIFO(LastInFirstOut) operation. Define a class Book with the following specifications:
Classname:Book
Data members/instance variables:

name[ ]:stores the names of the books
point:stores the index of the top most book

max:stores the maximum capacity of the bookshelf
Methods/Member functions:
Book(int cap):constructor to initialise 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.

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					public class Book
{
    String name[];
    int point,max;
    Book(int cap)
    {
        max=cap;
        point=-1;
        name=new String[max];
    }
    
    void tell()
    {
        if(point< 0)
        {
            System.out.println("SHELF EMPTY");
        }
        else
        {
            System.out.println(name[point]);
        }
    }

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

    void display()
    {
        for(int i=point;i>=0;i--)
        {
            System.out.println(name[i]);
        }
    }
}

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *