Browser Stack Program | ISC Computer Science 2026 Theory
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 URL's 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"
(i) Specify the class Browser giving details of the functions void visit(String) and String back(). Assume that the other functions have been defined.
class Browser{
String pages[];
int max;
int top;
public Browser(int cap){
max = cap;
top = -1;
pages = new String[max];
}
public void visit(String url){
if(top + 1 == pages.length)
System.out.println("Browser history full");
else
pages[++top] = url;
}
public String back(){
if(top == -1)
return "No previous browser history";
return pages[top--];
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Capacity: ");
int c = Integer.parseInt(in.nextLine());
Browser obj = new Browser(c);
while(true){
System.out.println("1. New page");
System.out.println("2. Previous page");
System.out.print("Enter your choice: ");
int choice = Integer.parseInt(in.nextLine());
switch(choice){
case 1:
System.out.print("Enter URL: ");
String url = in.nextLine();
obj.visit(url);
break;
case 2:
url = obj.back();
System.out.println(url);
break;
default:
System.out.println("Bye!");
return;
}
}
}
}
(ii) Name the entity described above and state its principle.
The entity described above is the Stack Data Structure. It is based on the LIFO (Last In First Out) principle.
Comments
Post a Comment