‹ Back to the paper
Write the program described below.
Specify the class Register giving details of the functions void push(String) and String pop( )…
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.
Answer
Answer
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.From ISC 2018 Computer Science Paper 1, question 11(a).