‹ Back to the paper
CardGame is a game of mental skill, built on the simple premise of adding and removing the cards…
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
(i)[4.0]
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.
(ii)[1.0]
Name the entity described above and state its principle.
Answer
Answer (i)
AIvoid addCard(int v)
{
if (top == cap - 1)
System.out.println("CARD PILE IS FULL");
else
{
top++;
cards[top] = v;
}
}
int drawCard()
{
if (top == -1)
return -9999;
int val = cards[top];
top--;
return val;
}Explanation: addCard() checks if the pile is full (top == cap-1); if not, it increments top and stores v at that index. drawCard() checks if the pile is empty (top == -1), returning -9999 in that case; otherwise it returns the value at the top index and decrements top. Tested with cap=3: after addCard(10), addCard(20), addCard(30), a further addCard(40) correctly prints 'CARD PILE IS FULL'; successive drawCard() calls correctly return 30, 20, 10 and then -9999 once empty.Answer (ii)
AIThe entity described is a Stack. It works on the principle of LIFO (Last In First Out) - the last card added (pushed) to the pile is the first one to be removed (popped).
From ISC 2024 Computer Science Paper 1, question 9.