Register Stack Java Program | ISC Computer Science 2018 Theory

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:

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 topmost location of the register if any, else returns “$$”
void display(): displays all the names in the register

(a) Specify the class Register giving details of the functions void push(String) and String pop(). Assume that the other functions have been defined.

import java.util.Scanner;
class Register{
    String stud[];
    int cap;
    int top;
    public Register(int max){
        cap = max;
        if(cap > 100)
            cap = 100;
        stud = new String[cap];
        top = -1;
    }
    public void push(String n){
        if(top + 1 == cap)
            System.out.println("OVERFLOW");
        else{
            stud[++top] = n;
            System.out.println(n + " PUSHED");
        }
    }
    public String pop(){
        if(top == -1)
            return "$$";
        return stud[top--];
    }
    public void display(){
        if(top == -1)
            System.out.println("STACK EMPTY");
        else{
            for(int i = 0; i <= top; i++)
                System.out.println(stud[i]);
        }
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Capacity: ");
        int cap = Integer.parseInt(in.nextLine());
        Register obj = new Register(cap);
        while(true){
            System.out.println("1. PUSH NAME");
            System.out.println("2. POP NAME");
            System.out.println("3. DISPLAY NAME");
            System.out.print("Enter your choice: ");
            int choice = Integer.parseInt(in.nextLine());
            switch(choice){
            case 1:
                System.out.print("Name to be pushed: ");
                String n = in.nextLine().toUpperCase();
                obj.push(n);
                break;
            case 2:
                n = obj.pop();
                if(n.equals("$$"))
                    System.out.println("UNDERFLOW");
                else
                    System.out.println(n + " POPPED");
                break;
            case 3:
                obj.display();
                break;
            default:
                System.out.println("Bye!");
                return;
            }
        }
    }
}

(b) Name the entity used in the above data structure arrangement.

Stack is the entity used in the above data structure arrangement.

Leave a Reply

Your email address will not be published. Required fields are marked *