‹ Back to the paper
Register is an entity which can hold a maximum of 100 names. The register enables the user to add…
Register is an entity which can hold a maximum of 100 names. The register enables the user to add and remove names from the top most end only.
Define a class Register with the following details:
Class name : Register
Data members / instance variables:
stud[ ] : array to store the names of the students
cap : stores the maximum capacity of the array
top : to point the index of the top end
Member functions:
Register (int max) : constructor to initialize the data member cap = max, top = −1 and create the string array
void push(String n) : to add names in the register at the top location if possible, otherwise display the message “OVERFLOW”
String pop( ) : removes and returns the names from the top most location of the register if any, else returns “\$\$”
void display( ) : displays all the names in the register
(a)[4.0]
Specify the class Register giving details of the functions void push(String) and String pop( ). Assume that the other functions have been defined.
The main function and algorithm need NOT be written.
(b)[1.0]
Name the entity used in the above data structure arrangement.
Answer
Answer (a)
AIvoid push(String n)
{
if (top == cap - 1)
System.out.println("OVERFLOW");
else
{
top++;
stud[top] = n;
}
}
String pop()
{
if (top == -1)
return "$$";
else
{
String val = stud[top];
top--;
return val;
}
}Explanation: push(String) checks whether the register is full (top has reached cap-1); if so it prints "OVERFLOW", otherwise it increments top and stores the new name at that top location. pop() checks whether the register is empty (top == -1), returning "$" in that case; otherwise it returns the name at the current top location and decrements top, so names are always added and removed from the same (topmost) end. Tested (run for real, capacity 3): after 3 successful push calls, a 4th correctly printed "OVERFLOW"; pop() correctly returned the names in LIFO order (ROHIT, SUMIT, AMIT) and finally returned "$" once emptied.Answer (b)
AIThe entity used is a Stack, which works on the LIFO (Last In First Out) principle - names are added and removed only from the same end (the top), so the last name pushed in is the first one popped out.
From ISC 2018 Computer Science Paper 1, question 11.