Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Desafío (Práctica de Deque) | Estructuras de Datos Adicionales
Java Data Structures

Desafío (Práctica de Deque)Desafío (Práctica de Deque)

Tarea

En el capítulo anterior, implementamos un sistema con dos botones, forward y backward, utilizando la estructura de datos deprecated Stack.

En este capítulo, tu tarea es recuperar la lógica que usamos para implementar la clase BrowserHistory e implementarla usando la estructura de datos Deque.

Intenta no echar un vistazo al capítulo anterior y completa esta tarea por tu cuenta, por así decirlo, "recodifica el código". Esto también es una excelente práctica con GitHub, que te será beneficiosa.

Una interfaz con todos los métodos ha sido creada en el código disponible en GitHub a través del siguiente enlace. Tu tarea es crear una implementación de clase e implementar esta interfaz overriding todos sus métodos.

Una vez que termines, ejecuta las pruebas que he escrito para ti; comprobará automáticamente tu solución.

También puedes realizar pruebas intermedias en la clase main. En general, confío en tu honestidad e independencia. Un verdadero programador debe ser capaz de entender el código de forma independiente. Mucha suerte.

Link to the Task: GitHub
  • Start by creating three attributes in this class: backStack, forwardStack, and currentUrl;
  • Next, use an auto-generator to create the constructor for this class;
  • Use the implements keyword and override all methods of the BrowserHistory interface;
  • In the methods, utilize the Deque or Stack methods you've learned earlier. Remember that Deque is a modern and superior replacement for the Stack data structure.
  • 
    package codefinity.Task;
    
    import java.util.Deque;
    
    public class BrowserHistoryImpl implements BrowserHistory {
        private Deque backStack;
        private Deque forwardStack;
        private String currentUrl;
    
        public BrowserHistoryImpl(Deque backStack, Deque forwardStack, String currentUrl) {
            this.backStack = backStack;
            this.forwardStack = forwardStack;
            this.currentUrl = currentUrl;
        }
    
        @Override
        public void visitPage(String url) {
            backStack.push(currentUrl);
            forwardStack.clear();
            currentUrl = url;
        }
    
        @Override
        public void goBack() {
            if (!backStack.isEmpty()) {
                forwardStack.push(currentUrl);
                currentUrl = backStack.pop();
            } else {
                System.out.println("Cannot go back. Already at the beginning.");
            }
        }
    
        @Override
        public void goForward() {
            if (!forwardStack.isEmpty()) {
                backStack.push(currentUrl);
                currentUrl = forwardStack.pop();
            } else {
                System.out.println("Cannot go forward. Already at the latest page.");
            }
        }
    
        @Override
        public String getCurrentPage() {
            return currentUrl;
        }
    }      

    ¿Todo estuvo claro?

    Sección 2. Capítulo 5
    course content

    Contenido del Curso

    Java Data Structures

    Desafío (Práctica de Deque)Desafío (Práctica de Deque)

    Tarea

    En el capítulo anterior, implementamos un sistema con dos botones, forward y backward, utilizando la estructura de datos deprecated Stack.

    En este capítulo, tu tarea es recuperar la lógica que usamos para implementar la clase BrowserHistory e implementarla usando la estructura de datos Deque.

    Intenta no echar un vistazo al capítulo anterior y completa esta tarea por tu cuenta, por así decirlo, "recodifica el código". Esto también es una excelente práctica con GitHub, que te será beneficiosa.

    Una interfaz con todos los métodos ha sido creada en el código disponible en GitHub a través del siguiente enlace. Tu tarea es crear una implementación de clase e implementar esta interfaz overriding todos sus métodos.

    Una vez que termines, ejecuta las pruebas que he escrito para ti; comprobará automáticamente tu solución.

    También puedes realizar pruebas intermedias en la clase main. En general, confío en tu honestidad e independencia. Un verdadero programador debe ser capaz de entender el código de forma independiente. Mucha suerte.

    Link to the Task: GitHub
  • Start by creating three attributes in this class: backStack, forwardStack, and currentUrl;
  • Next, use an auto-generator to create the constructor for this class;
  • Use the implements keyword and override all methods of the BrowserHistory interface;
  • In the methods, utilize the Deque or Stack methods you've learned earlier. Remember that Deque is a modern and superior replacement for the Stack data structure.
  • 
    package codefinity.Task;
    
    import java.util.Deque;
    
    public class BrowserHistoryImpl implements BrowserHistory {
        private Deque backStack;
        private Deque forwardStack;
        private String currentUrl;
    
        public BrowserHistoryImpl(Deque backStack, Deque forwardStack, String currentUrl) {
            this.backStack = backStack;
            this.forwardStack = forwardStack;
            this.currentUrl = currentUrl;
        }
    
        @Override
        public void visitPage(String url) {
            backStack.push(currentUrl);
            forwardStack.clear();
            currentUrl = url;
        }
    
        @Override
        public void goBack() {
            if (!backStack.isEmpty()) {
                forwardStack.push(currentUrl);
                currentUrl = backStack.pop();
            } else {
                System.out.println("Cannot go back. Already at the beginning.");
            }
        }
    
        @Override
        public void goForward() {
            if (!forwardStack.isEmpty()) {
                backStack.push(currentUrl);
                currentUrl = forwardStack.pop();
            } else {
                System.out.println("Cannot go forward. Already at the latest page.");
            }
        }
    
        @Override
        public String getCurrentPage() {
            return currentUrl;
        }
    }      

    ¿Todo estuvo claro?

    Sección 2. Capítulo 5
    some-alt