Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
305 views2 pages

Program 9

The document defines a Book class to represent a bookshelf that stores books using a LIFO system. The Book class has data members to store the book names, the index of the top book, and the maximum shelf capacity. Member functions add a book if possible or display a message if the shelf is full, display the top book and remove it or a message if the shelf is empty, and display all the books. The main function demonstrates creating a Book object, adding a book, and displaying the books.

Uploaded by

Nishant Jaiswal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
305 views2 pages

Program 9

The document defines a Book class to represent a bookshelf that stores books using a LIFO system. The Book class has data members to store the book names, the index of the top book, and the maximum shelf capacity. Member functions add a book if possible or display a message if the shelf is full, display the top book and remove it or a message if the shelf is empty, and display all the books. The main function demonstrates creating a Book object, adding a book, and displaying the books.

Uploaded by

Nishant Jaiswal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

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.
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 entered last.If there is no book left
in the
shelf,displays "Shelf Empty".
void add(String v) : adds the name of the book to the
shelf if possible otherwise displays "Shelf full".
void display() : displays all the names of
the books available in the shelf.
Specify the class Book giving details of only the functions void tell() and void
add(String).
The main() function needs not to be written.

import java.io.*;
import java.util.*;
class Book
{
String name[];
int point,max;
Book(int cap)
{
point=-1;
max=cap;
name=new String[max];
}
void add(String v)
{
if(point==max-1)
System.out.println("Shelf is full");
else
{
point=point+1;
name[point]=v;
}
}
void tell()
{
if(point==-1)
{
System.out.println("Shelf is empty");
}
else
{
System.out.println("Topmost book:"+name[point]);
point=point-1;
}
}
void display()
{
System.out.println("Arrangement of the book:");
if(point==-1)
System.out.println("Shelf is empty");
else
{
for(int i=point;i>=0;i--)
System.out.println(name[i]);
}
}
public static void main(String args[])throws IOException
{
Scanner inp=new Scanner(System.in);
System.out.println("Enter maximum capacity");
int a=inp.nextInt();
System.out.println("Enter name of the book");
String b=inp.nextLine();
Book obj=new Book(a);
obj.tell();
obj.add(b);
obj.display();
}
}

You might also like