Prashnikaप्रश्निका
Code
About
‹ Back to the paper

Question 9(i)

4 marks

Computer ScienceISC 2026 Computer Science Paper 1ProgramData structures

Write the program described below.

Specify the class Browser giving details of the functions void visit(String) and String back( )…

Specify the class Browser giving details of the functions void visit(String) and String back( ). Assume that the other functions have been defined.
Show the case
In any internet browser, a user can visit new webpages and go back to previously visited webpages. Each new webpage URL is stored in browser's memory such that when the user clicks 'Back' button, the previous webpage gets displayed. The details of the members of the class are given below: Class name : Browser Data members/instance variables: pages[ ] : an array to hold the URLs of visited webpages max : to store the maximum capacity of the array top : to point to the index of the last visited webpages Methods/Member functions: Browser(int cap) : constructor to assign max = cap and top = -1 void visit(String url) : to add URL of a new webpage if possible, else display the message "Browser history full" String back( ) : to remove and return the last visited webpage URL, if present, else to return the message "No previous browser history"

Answer

Answer

void visit(String url)
{
    if (top == max - 1)
    {
        System.out.println("Browser history full");
    }
    else
    {
        top++;
        pages[top] = url;
    }
}

String back()
{
    if (top == -1)
    {
        return "No previous browser history";
    }
    else
    {
        String url = pages[top];
        top--;
        return url;
    }
}