------Explain in detail what each line of code is doing/being used for------ pac
ID: 3840270 • Letter: #
Question
------Explain in detail what each line of code is doing/being used for------
package finalProject;
import java.io.*;
public class Browser {
private boolean browsing;
private WebVisit currentVisit;
private String home;
public static void main(String[] args) {
Browser b = new Browser();
b.run();
}
public Browser() {
browsing = true;
currentVisit = null;
System.out.println("Welcome to FCS Browser v0.01");
}
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("Enter a homepage url.");
String newHomepage = br.readLine();
setHomepage(newHomepage);
System.out.println("Thank you, your homepage has been set.");
}catch(IOException e) {
System.out.println("You failed at setting your homepage.");
}
while (browsing) {
try {
promptUser();
String cmd = br.readLine();
interpretAndExecute(cmd);
} catch (IOException e) {
System.out.println("Oops! There was an error reading your command.");
}
}
}
private void promptUser() {
System.out.println(" Browser ready. (Commands: visit *web url*; visit home; history; back; quit; forward; home;)");
}
private void interpretAndExecute(String cmd) {
String[] commands = cmd.split(" ");
if (cmd.equals("back")) {
goBack();
} else if (cmd.equals("history")) {
viewHistory();
} else if (commands[0].equals("visit")) {
visitPage(commands[1]);
} else if (cmd.equals("quit")) {
quit();
} else if (cmd.equals("forward")){
goForward();
} else if(cmd.equals("refresh"))
{
refresh();
} else if(cmd.equals("home"))
{
home();
} else {
System.out.println("Command not recognized");
}
}
private void visitPage(String url) {
System.out.println("Now going to visit "+url);
WebVisit wv = new WebVisit(url, url, currentVisit, null);
if(url.equals("home"))
wv = new WebVisit(home, home, currentVisit, null);
else if (currentVisit != null) {
currentVisit.setNextNode(wv);
}
currentVisit = wv;
System.out.println(currentVisit);
}
private void goBack() {
if (currentVisit != null) {
System.out.println("Going back...");
currentVisit = currentVisit.getPreviousNode();
if (currentVisit != null) {
System.out.println(currentVisit);
}
} else {
System.out.println("No web visits in browser history");
}
}
private void goForward() {
if (currentVisit != null) {
System.out.print("Going forward...");
currentVisit = currentVisit.getNextNode();
if (currentVisit != null) {
System.out.println(currentVisit);
} else {
System.out.println("No next page to show.");
}
}
}
private void viewHistory() {
System.out.println("Showing browser history...");
WebVisit tmp = currentVisit;
int counter = 0;
while (tmp != null) {
System.out.println(" ~"+tmp);
tmp = tmp.getPreviousNode();
counter++;
}
System.out.println(" "+counter+" total");
}
private void refresh()
{
if(currentVisit != null)
{
System.out.println("Refreshing the page...");
}
else
System.out.println("No URL to refresh.");
}
private void home()
{
if(currentVisit != null)
{
System.out.println("Going to homepage...");
currentVisit.getHomepage();
if(currentVisit != null)
System.out.println(currentVisit);
else
System.out.println("You have not set a homepage.");
}
}
private void setHomepage(String hp)
{
visitPage(hp);
home = hp;
}
private void quit() {
System.out.println("Quitting now...");
browsing = false;
}
}
Explanation / Answer
package browser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Browser {
//boolean variable browsing is created to check the current condition whether browisng or not
private boolean browsing;
//instance of WebVisit is created to use the functions/Variables in WebVisit class
private WebVisit currentVisit;
//String variable with home is created to give any name in string format to home
private String home;
//Main method to run the application
public static void main(String[] args) {
//browser object is created using default constructor and run method is called.
Browser b = new Browser();
b.run();
}
//Constructor of this class
public Browser() {
//When the Browser object is created using this constructed below parameters are set
browsing = true;
currentVisit = null;
System.out.println("Welcome to FCS Browser v0.01");
}
//run method returns nothing and does not take any parameter
public void run() {
//BufferedReader Reads and stores the entered text from the console
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Enter a homepage url.");
//Reades line entered in the consle and assigns to String variable newHomePage
String newHomepage = br.readLine();
//setHomepage method is called passing value entered in the text
setHomepage(newHomepage);
System.out.println("Thank you, your homepage has been set.");
} catch (IOException e) {
System.out.println("You failed at setting your homepage.");
}
//when browsing is true promptUser() method is called to prmpt the user to enter the required command
while (browsing) {
try {
promptUser();
String cmd = br.readLine();
//entered command from commands list is passed to interpretAndExecute method
interpretAndExecute(cmd);
} catch (IOException e) {
System.out.println("Oops! There was an error reading your command.");
}
}
}
private void promptUser() {
System.out.println(
" Browser ready. (Commands: visit *web url*; visit home; history; back; quit; forward; home;)");
}
//Calls appropriate method based on the command passed in the console
private void interpretAndExecute(String cmd) {
String[] commands = cmd.split(" ");
if (cmd.equals("back")) {
goBack();
} else if (cmd.equals("history")) {
viewHistory();
} else if (commands[0].equals("visit")) {
visitPage(commands[1]);
} else if (cmd.equals("quit")) {
quit();
} else if (cmd.equals("forward")) {
goForward();
} else if (cmd.equals("refresh")) {
refresh();
} else if (cmd.equals("home")) {
home();
} else {
System.out.println("Command not recognized");
}
}
//Takes url as input and opens the page using WebVist object
private void visitPage(String url) {
System.out.println("Now going to visit " + url);
WebVisit wv = new WebVisit(url, url, currentVisit, null);
//If the url entered is home, opens the home page else directs to next page using setNextNode method
if (url.equals("home"))
wv = new WebVisit(home, home, currentVisit, null);
else if (currentVisit != null) {
currentVisit.setNextNode(wv);
}
currentVisit = wv;
System.out.println(currentVisit);
}
//This method goes to precious node if the currentpage is in nextnode else prints the same page
private void goBack() {
if (currentVisit != null) {
System.out.println("Going back...");
currentVisit = currentVisit.getPreviousNode();
if (currentVisit != null) {
System.out.println(currentVisit);
}
} else {
System.out.println("No web visits in browser history");
}
}
//Checks if the page has next node, if it has directs to next node and prints the contents in the next node else prints message as no next page to show
private void goForward() {
if (currentVisit != null) {
System.out.print("Going forward...");
currentVisit = currentVisit.getNextNode();
if (currentVisit != null) {
System.out.println(currentVisit);
} else {
System.out.println("No next page to show.");
}
}
}
//prints all the previous nodes available for the currentVisit and total number of nodes
private void viewHistory() {
System.out.println("Showing browser history...");
WebVisit tmp = currentVisit;
int counter = 0;
while (tmp != null) {
//prints each webVisit seperated by tab and ~
System.out.println(" ~" + tmp);
//assigns previous node to tmp
tmp = tmp.getPreviousNode();
//counter to calculate total number of nodes
counter++;
}
System.out.println(" " + counter + " total");
}
//If the currentVisit is not null refreshes the page otherwise shows message as No URL to refresh
private void refresh() {
if (currentVisit != null) {
System.out.println("Refreshing the page...");
} else
System.out.println("No URL to refresh.");
}
//This method redirects to home page if currentVisit object is not null
private void home() {
if (currentVisit != null) {
System.out.println("Going to homepage...");
currentVisit.getHomepage();
if (currentVisit != null)
System.out.println(currentVisit);
else
System.out.println("You have not set a homepage.");
}
}
//Sets home page with given string
private void setHomepage(String hp) {
visitPage(hp);
home = hp;
}
//Shows the message as quitting and sets the browsing to false
private void quit() {
System.out.println("Quitting now...");
browsing = false;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.