English

CardGame is a game of mental skill, built on the simple premise of adding and removing the cards from the top of the card pile. The details of the class Card Game are given below. Class name CardGam - Computer Science (Theory)

Advertisements
Advertisements

Question

CardGame is a game of mental skill, built on the simple premise of adding and removing the cards from the top of the card pile.

The details of the class CardGame are given below.

Class name CardGame
Data members/instance variables:
cards[] array to store integers as cards
cap to store the maximum capacity of array
top to store the index of the topmost element of the array
Methods/Member functions:
CardGame(int cc) constructor to initialise cap=cc and top= -1
void addCard(int v) to add the card at the top index if possible, otherwise display the message “CARD PILE IS FULL”
int drawCard( ) to remove and return the card from the top index of the card pile, if any, else return the value -9999
void display( ) to display all the cards of card pile
  1. Specify the class CardGame giving details of the functions void addCard(int) and int drawCard( ). Assume that the other functions have been defined.
    The main() function and algorithm need NOT be written.
  2. Name the entity described above and state its principle.
Answer in Brief

Solution

(i)

class CardGame
{
  int cards[]; 
  int cap,top; 

  public CardGame(int cc) 
  {
    cap= Math.abs(cc); 
    top=-1; 
    cards=new int[cap]; 
  }

  void addCard(int v)  
  {
    top++;
    if(top==cap)
    {
     System.out.println("CARD PILE IS FULL");
     top--; 
    }
    else
    cards[cap]=v; 
  }

  public int drawCard() 
  {
   if(top==-1) 
   {
     System.out.println("CARD PILE IF EMPTY");
     return-9999;
   }
   else
   return cards[cap--];
  }

  public void display() 
  {
   if(top==-1)
     System.out.println("EMPTY CARD PILE");
   else
   {
     for(int i=top; i>=0; i--)
     System.out. println(cards[i]);
   }
  }
}

(ii) The above entity is a Stack and its principle is Last In First Out (LIFO).

shaalaa.com
Implementation Directly Through Classes
  Is there an error in this question or solution?
2023-2024 (February) Official
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×