Create an advance data type to represent web page history Na
Solution
import java.util.Date;
public class WebPage {
Date visitedOn;
String url;
String content;
/**
* @param visitedOn
* @param url
* @param content
*/
public WebPage(Date visitedOn, String url, String content) {
this.visitedOn = visitedOn;
this.url = url;
this.content = content;
}
/**
* @return the visitedOn
*/
public Date getVisitedOn() {
return visitedOn;
}
/**
* @param visitedOn
* the visitedOn to set
*/
public void setVisitedOn(Date visitedOn) {
this.visitedOn = visitedOn;
}
/**
* @return the url
*/
public String getUrl() {
return url;
}
/**
* @param url
* the url to set
*/
public void setUrl(String url) {
this.url = url;
}
/**
* @return the content
*/
public String getContent() {
return content;
}
/**
* @param content
* the content to set
*/
public void setContent(String content) {
this.content = content;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return \"WebPage [visitedOn=\" + visitedOn + \", url=\" + url
+ \", content=\" + content + \"]\";
}
}
import java.util.Arrays;
public class WebHistory {
int totalPagesVisited;
WebPage[] pages;
/**
* @param totalPagesVisited
* @param pages
*/
public WebHistory(int totalPages) {
this.totalPagesVisited = 0;
this.pages = new WebPage[totalPages];
}
public boolean Add(WebPage p) {
if (totalPagesVisited < pages.length) {
pages[totalPagesVisited] = p;
totalPagesVisited++;
return true;
} else
return false;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return \"WebHistory [totalPagesVisited=\" + totalPagesVisited
+ \", pages=\" + Arrays.toString(pages) + \"]\";
}
}
import java.util.Date;
public class TestWebHistory {
public static void main(String[] args) {
WebHistory myhist = new WebHistory(2);
Date today = new Date(10, 10, 2016);
Date yesterday = new Date(9, 10, 2016);
WebPage p1 = new WebPage(today, \"www.google.com\",
\"This is Google\'s webpage\");
WebPage p2 = new WebPage(yesterday, \"www.yahoo.com\",
\"This is Yahoo\'s webpage\");
myhist.Add(p1);
myhist.Add(p2);
System.out.println(myhist);
}
}
OUTPUT:
WebHistory [totalPagesVisited=2, pages=[WebPage [visitedOn=Mon May 08 00:00:00 IST 1916, url=www.google.com, content=This is Google\'s webpage], WebPage [visitedOn=Sun May 09 00:00:00 IST 1915, url=www.yahoo.com, content=This is Yahoo\'s webpage]]]


