Page 501 - Computer science 868 Class 12
P. 501
Program 2 A register is an entity which can hold a maximum of 100 names. The register enables the
user to add and remove names from the topmost end only. Define a class Register with the
following details. [ISC 2018]
Class name : Register
Data Members
stud[] : an array to store the names of the students
cap : the maximum capacity of the string array
top : the index of the topmost element of the stack
Member Functions
Register (int max) : constructor to initialise cap = max and top = -1 and define the
string array
void push (String n) : to push name ‘n’ into the stack. If the stack is full, display the
message “overflow”
String pop() : to remove and return the names from the topmost location
of the register if any, else return “$$”
void display() : to display all the names in the register
Specify the class Register giving details of the constructor and the functions void push(String)
and String pop() and void display().
1 import java.util.*;
2 class Register
3 { String stud[]=new String[100];
4 int cap,top;
5 Register(int max)
6 { cap=max;
7 stud=new String[cap];
8 top=-1;
9 }
10 void push(String n)
11 { if(top==cap-1) // if stack is full
12 System.out.println("Overflow");
13 else
14 {
15 stud[++top]=n; // push element to stack
16 }
17 }
18 String pop()
19 { if(top==-1) // if stack is empty
20 return("$$");
499
Data Structures 499

