icse-promo

Question

WordPile is an entity which can hold maximum of 20 characters .The restriction is that a character can be added or removed from one end only.Some of the members of classes are given below:
Classname:WordPile
Data members/instance variables:
ch[ ]:character array to hold the character elements
capacity:integer variable to store the maximum capacity
top:to point to the index of the top most element
Methods / Member functions:
WordPile( int cap):constructor to initialise the data member capacity=cap , top=-1 and create the WordPile
void pushChar(char v):adds the character to the top of WordPile if possible,otherwise output a message ”WordPile
is full”
char popChar():returns the deleted character from the top of the WordPile if possible,otherwise it returns ‘\\’
(a)Specify the class WordPile giving the details of the constructor, void pushChar(char) and char popChar().
THE MAIN() FUNCTION AND ALGORITHM NEED NOT BE WRITTEN.

Share code with your friends

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

Code

				
					public class WordPile
{
    char ch[]=new char[20];
    int capacity,top;
    WordPile(int cap)
    {    
        capacity=cap;
        top=-1;
        ch=new char[capacity];
    }

    void pushChar(char v)
    {
        if(top< capacity-1)
        {
            top=top+1;
            ch[top]=v;
        }
        else
        {   
            System.out.println("WordPile is full");
        }
    }

    char popChar()
    {
        if(top>=0)
        {
            return ch[top--];
        }
        else
        {
            return'\\';
        }
    }
}


				
			

Coding Store

Leave a Reply

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