Untitled

 avatar
user_0781376
plain_text
14 days ago
4.3 kB
33
Indexable
program 1---manual implementation
=========================================
package Day2;

import java.util.Scanner;

class BrowserHistory1 {
    private String[] history; // Array to store history
    private int top;
    private int capacity;

    // Constructor to initialize stack
    public BrowserHistory1(int size) {
        capacity = size;
        history = new String[capacity];
        top = -1;
    }

    // Push (Visit a new page)
    public void push(String url) {
        if (top == capacity - 1) {
            System.out.println("Stack overflow. Cannot visit more pages.");
            return;
        }
        history[++top] = url;
        System.out.println("Visited: " + url);
    }

    // Pop (Go back to the previous page)
    public void pop() {
        if (isEmpty()) {
            System.out.println("No pages in history to go back to.");
            return;
        }
        System.out.println("Going back from: " + history[top--]);
    }

    // Peek (Get the current page)
    public void peek() {
        if (isEmpty()) {
            System.out.println("No pages in history.");
            return;
        }
        System.out.println("Current page: " + history[top]);
    }

    // isEmpty (Check if history is empty)
    public boolean isEmpty() {
        return top == -1;
    }
}

public class BrowserHistoryManual {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        BrowserHistory1 history = new BrowserHistory1(5);

        System.out.println("Welcome to the Browser History Manager!");
        System.out.println("Commands: push <URL>, pop, peek, isEmpty, exit");

        while (true) {
            System.out.print("> ");
            String command = scanner.next();

            if (command.equals("push")) {
                String url = scanner.next();
                history.push(url);
            } else if (command.equals("pop")) {
                history.pop();
            } else if (command.equals("peek")) {
                history.peek();
            } else if (command.equals("isEmpty")) {
                System.out.println(history.isEmpty() ? "History is empty." : "History has pages.");
            } else if (command.equals("exit")) {
                break;
            } else {
                System.out.println("Invalid command. Try again.");
            }
        }

        scanner.close();
    }
}
====================================
program 2:using built in
==========================
package Day2;

import java.util.Scanner;
import java.util.Stack;

public class BrowserHistory {
    public static void main(String[] args) {
        Stack<String> history = new Stack<>();
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Welcome to the Browser History Manager!");
        System.out.println("Commands: push <URL>, pop, peek, isEmpty, exit");
        
        while (true) {
            System.out.print("> ");
            String command = scanner.next();
            
            if (command.equals("push")) {
                String url = scanner.next();
                history.push(url);
                System.out.println("Visited: " + url);
            } else if (command.equals("pop")) {
                if (history.isEmpty()) {
                    System.out.println("No pages in history to go back to.");
                } else {
                    System.out.println("Going back from: " + history.pop());
                }
            } else if (command.equals("peek")) {
                if (history.isEmpty()) {
                    System.out.println("No pages in history.");
                } else {
                    System.out.println("Current page: " + history.peek());
                }
            } else if (command.equals("isEmpty")) {
                System.out.println(history.isEmpty() ? "History is empty." : "History has pages.");
            } else if (command.equals("exit")) {
                break;
            } else {
                System.out.println("Invalid command. Try again.");
            }
        }
        scanner.close();
    }
}
=======================================



Editor is loading...
Leave a Comment