Challenge: Implementing BrowserHistory
Task
In the previous chapter, you implemented a system with two buttons, forward and backward, using the obsolete Stack data structure, which is not recommended for use in modern Java development.
In this chapter, your task is to recall the logic you used to implement the BrowserHistory class and implement it using the Deque data structure.
Try not to peek at the previous chapter and complete this task on your own—so to speak, "recode the code." This is also excellent practice with GitHub, which will be beneficial for you.
An interface with all the methods has been created in the code available on GitHub via the link below. Your task is to create a class implementation and implement this interface by overriding all its methods.
Once you finish, run the tests that I have written for you; it will automatically check your solution.
You can also perform intermediate tests in the main class. Overall, I rely on your honesty and independence. A true programmer should be able to understand code independently!
- Start by creating three attributes in this class:
backStack,forwardStack, andcurrentUrl. - Next, use an auto-generator to create the constructor for this class.
- Use the implements keyword and override all methods of the
BrowserHistoryinterface. - In the methods, utilize the
DequeorStackmethods you've learned earlier. Remember thatDequeis a modern and superior replacement for theStackdata structure.
package codefinity.Task;
import java.util.Deque;
public class BrowserHistoryImpl implements BrowserHistory {
private Deque<String> backStack;
private Deque<String> forwardStack;
private String currentUrl;
public BrowserHistoryImpl(Deque<String> backStack, Deque<String> 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;
}
}
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Awesome!
Completion rate improved to 4
Challenge: Implementing BrowserHistory
Sveip for å vise menyen
Task
In the previous chapter, you implemented a system with two buttons, forward and backward, using the obsolete Stack data structure, which is not recommended for use in modern Java development.
In this chapter, your task is to recall the logic you used to implement the BrowserHistory class and implement it using the Deque data structure.
Try not to peek at the previous chapter and complete this task on your own—so to speak, "recode the code." This is also excellent practice with GitHub, which will be beneficial for you.
An interface with all the methods has been created in the code available on GitHub via the link below. Your task is to create a class implementation and implement this interface by overriding all its methods.
Once you finish, run the tests that I have written for you; it will automatically check your solution.
You can also perform intermediate tests in the main class. Overall, I rely on your honesty and independence. A true programmer should be able to understand code independently!
- Start by creating three attributes in this class:
backStack,forwardStack, andcurrentUrl. - Next, use an auto-generator to create the constructor for this class.
- Use the implements keyword and override all methods of the
BrowserHistoryinterface. - In the methods, utilize the
DequeorStackmethods you've learned earlier. Remember thatDequeis a modern and superior replacement for theStackdata structure.
package codefinity.Task;
import java.util.Deque;
public class BrowserHistoryImpl implements BrowserHistory {
private Deque<String> backStack;
private Deque<String> forwardStack;
private String currentUrl;
public BrowserHistoryImpl(Deque<String> backStack, Deque<String> 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;
}
}
Takk for tilbakemeldingene dine!