Results 1 to 6 of 6
  1. #1
    Zahara is offline Member
    Join Date
    Feb 2012
    Location
    Scotland, United Kingdom
    Posts
    14
    Rep Power
    0

    Default implement a dynamic data structure that mimics simple web browsing

    Hi, Here's my question;

    Step1
    Complete the setting up of the site by implementing
    the Page class
    the addPage method of the Site class
    the displayCurrentPage method of the Site class
    + any attributes/methods to support displaying a page
    the result of displaying the current page should be
    current page name: Home
    has links to
    Shops
    News
    Products

    And here's my answer:
    Java Code:
    public class Site
    	{
    
    public class PageNode
    	{
    		private Page page;
    	    private PageNode child;
    	    private PageNode parent;
    	    private PageNode nextsibling;
    
    	public PageNode()
    	{
    	    this.child = null;
    	    this.parent = null;
    	    this.nextsibling = null;
    	}
    
    	public PageNode(String PageName)
    	{
    	    this.child = null;
    	    this.parent = null;
    	    this.nextsibling = null;
    	}
    
    	public String toString()
    	{
    	    return ""+page;
    	}
    
    	}
    	private PageNode currentPage;
    	private PageNode homePage;
    	
    	public Site(){
    		this.homePage=new PageNode();
    		this.homePage.page=new Page("Home");
    		this.currentPage=this.homePage;
    		
    		PageNode shops=this.addPage("Shops",this.homePage);
    		this.addPage("News",this.homePage);
    		PageNode products=this.addPage("Products",this.homePage);
    		
    		this.addPage("Paisley",shops);
    		this.addPage("Hamilton",shops);
    		
    		PageNode kitchen=this.addPage("Kitchen",products);
    		this.addPage("Bedroom",products);
    		
    		this.addPage("Kettles",kitchen);
    		this.addPage("Cookers",kitchen);
    		this.addPage("Toasters",kitchen);
    	}
    	
    	private PageNode addPage(String PageName, PageNode ParentNode)
    	{
    	            ParentNode = new PageNode();
    	            ParentNode.page=new Page(PageName);
    
    	    if (this.currentPage.page.compareTo(ParentNode.page)==0)
    	    {
    	        System.out.println("attempt to insert a duplicate");
    	    }
    	    else
    	                    if (ParentNode.page.compareTo(currentPage.page)<0)
    
    	                        if(currentPage.child == null)
    	                        {
    	            currentPage.child=ParentNode;
    	                            ParentNode.child = new PageNode();
    	                            ParentNode.child.page = new Page(PageName);
    	                        }
    	                            else if(currentPage.nextsibling == null)
    	                            {
    	                                currentPage.nextsibling=ParentNode;                        
    	                                ParentNode.nextsibling = new PageNode();
    	                                ParentNode.nextsibling.page = new Page(PageName);
    	                            }
    	            return ParentNode;
    	}
    	
    	public void displayCurrentPage()
    	{
    	                if (this.homePage!=null)
    	    {
    	        this.displayBranches(this.homePage);
    	    }
    	    else
    	        System.out.println("tree is empty");
    	    }
    	    private void displayBranches(PageNode ParentNode)
    	    {
    	    if (ParentNode!=null)
    	    {
    	        System.out.println(ParentNode.page+"  ");
    	        System.out.print("    left:  ");
    	        if (ParentNode.child!=null)
    	            System.out.println(ParentNode.child.page);
    	        else
    	            System.out.println("null");
    	        System.out.print("    right: ");
    	        if (ParentNode.nextsibling!=null)
    	            System.out.println(ParentNode.nextsibling.page);
    	        else
    	            System.out.println("null");
    	                    displayBranches(ParentNode.child);
    	        displayBranches(ParentNode.nextsibling);
    	        }
    	    }
    	
    	public void selectLink(){
    		// add code for step2
    	}
    	
    	public void moveUp() {
    		// add code for step3
    	}
    
    	public void displaySiteMap(){
    		// add code for step4
    	}
    
    	
    }
    And this is what I see in my output:
    1: display current page
    2: select link
    3: move up
    4: display site map
    0: quit
    select option: 1
    Home
    left: Shops
    right: News
    Shops
    left: Shops
    right: null
    Shops
    left: null
    right: null
    News
    left: null
    right: News
    News
    left: null
    right: null
    1: display current page
    2: select link
    3: move up
    4: display site map
    0: quit
    select option:

    But I want my output to be as follows:
    current page name: Home
    has links to
    Shops
    News
    Products

    Can someone please help me out in altering my code?
    I'm going crazy

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: implement a dynamic data structure that mimics simple web browsing

    Try debugging the code by adding println statements to it that show the execution flow and the values of variables as the code executes. If you know what the code is supposed to do, when you see what it is doing you will be able to find the places where it needs to be changed.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Zahara is offline Member
    Join Date
    Feb 2012
    Location
    Scotland, United Kingdom
    Posts
    14
    Rep Power
    0

    Default Re: implement a dynamic data structure that mimics simple web browsing

    Hi Norm, Sorry for late reply.. I made a few changes to the println statements and got the right output! thanks :)

  4. #4
    Kay-b is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Re: implement a dynamic data structure that mimics simple web browsing

    Hi Zahara, Can you post a solution to the changes you made? I am working on a similar program and Im getting some errors. By referring to yours, I will be able to work on mine as well. Thanks.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: implement a dynamic data structure that mimics simple web browsing

    Kay-b Please start your own thread.

  6. #6
    Zahara is offline Member
    Join Date
    Feb 2012
    Location
    Scotland, United Kingdom
    Posts
    14
    Rep Power
    0

    Default Re: implement a dynamic data structure that mimics simple web browsing

    Quote Originally Posted by Kay-b View Post
    Hi Zahara, Can you post a solution to the changes you made? I am working on a similar program and Im getting some errors. By referring to yours, I will be able to work on mine as well. Thanks.
    Hi Kay
    Hope this helps
    public class Site{

    public class PageNode{
    private Page page;
    private PageNode child;
    private PageNode parent;
    private PageNode nextsibling;
    private PageNode thirdchild;


    public PageNode(){
    this.child = null;
    this.parent = null;
    this.nextsibling = null;
    this.thirdchild = null;
    }
    public String toString(){
    return ""+page;
    }
    }
    private PageNode currentPage;
    private PageNode homePage;

    public Site(){
    this.homePage=new PageNode();
    this.homePage.page=new Page("Home");
    this.currentPage=this.homePage;

    PageNode shops=this.addPage("Shops",this.homePage);
    this.addPage("News",this.homePage);
    PageNode products=this.addPage("Products",this.homePage);


    this.addPage("Paisley",shops);
    this.addPage("Hamilton",shops);

    PageNode kitchen=this.addPage("Kitchen",products);
    this.addPage("Bedroom",products);

    this.addPage("Kettles",kitchen);
    this.addPage("Cookers",kitchen);
    this.addPage("Toasters",kitchen);
    }

    private PageNode addPage(String PageName, PageNode ParentNode)
    {
    ParentNode = new PageNode();
    ParentNode.page=new Page(PageName);

    if (this.currentPage.page.compareTo(ParentNode.page)= =0)
    {
    System.out.println("attempt to insert a duplicate");
    }
    else

    if (ParentNode.page.compareTo(currentPage.page)<0)
    if(currentPage.child == null)
    {
    currentPage.child=ParentNode;
    ParentNode.child = new PageNode();
    ParentNode.child.page = new Page(PageName);
    }
    else if(currentPage.nextsibling == null)
    {
    currentPage.nextsibling=ParentNode;
    ParentNode.nextsibling = new PageNode();
    ParentNode.nextsibling.page = new Page(PageName);
    }
    else if (currentPage.thirdchild == null)
    {
    currentPage.thirdchild=ParentNode;
    ParentNode.thirdchild = new PageNode();
    ParentNode.thirdchild.page = new Page(PageName);
    }
    return ParentNode;
    }

    public void displayCurrentPage()
    {
    if (this.homePage!=null)
    {
    System.out.print("current page name: ");
    this.displayBranches(this.homePage);

    }
    else
    System.out.println("tree is empty");
    }
    private void displayBranches(PageNode ParentNode)
    {
    if (ParentNode!=null)
    {
    System.out.println(ParentNode.page+" ");
    System.out.println(" has links to ");
    System.out.print(" ");
    if (ParentNode.child!=null)
    System.out.println(ParentNode.child.page);
    else
    System.out.println("null");
    System.out.print(" ");
    if (ParentNode.nextsibling!=null)
    System.out.println(ParentNode.nextsibling.page);
    else
    System.out.println("null");
    System.out.print(" ");
    if (ParentNode.thirdchild!=null)
    System.out.println(ParentNode.thirdchild.page);
    else
    System.out.println("null");


    }
    }

    public void selectLink(){
    // add code for step2
    }

    public void moveUp() {
    // add code for step3
    }

    public void displaySiteMap(){
    // add code for step4
    }


    }

Similar Threads

  1. Replies: 8
    Last Post: 10-17-2011, 09:17 AM
  2. Problem--How to create dynamic tree structure in jsp using javascript
    By natu in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-11-2010, 08:11 AM
  3. data structure and data base??
    By ahmed13 in forum Advanced Java
    Replies: 8
    Last Post: 03-27-2009, 05:48 AM
  4. Replies: 1
    Last Post: 10-20-2008, 12:58 PM
  5. Replies: 0
    Last Post: 07-11-2008, 04:43 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •