如何修改下面的代码,在线等?
澳门博彩导航网址
澳门博彩导航网址
当前位置 : 澳门博彩导航网址 > 澳门博彩导航

如何修改下面的代码,在线等?

我需要把下面这个有缺失的代码实现,

  1. 在这里,这个代码的构造函数是不完整的,如何合理的完成构造函数的填充

  2. 在完成构造函数部分之后,我需要在主函数中创建几个节点, 然后使用findChild方法来找到每个节点的名字。怎么把findChild函数填充完整,这样程序可以顺利进行下面是代码

 package directory_explorer;

import java.util.Map;
import java.util.Collection;
import java.util.HashMap;

/**
 * The root of a tree representing a directory structure.
 */
public class FileNode {

    /** The name of the file or directory this node represents. */
    private String name;
    /** Whether this node represents a file or a directory. */
    private FileType type;
    /** This nodes parent. */
    private FileNode parent;
    /**
     * This nodes children, mapped from the file names to the nodes. If type is
     * FileType.FILE, this is null.
     */
    private Map<String, FileNode> children;

    /**
     * A node in this tree.
     *
     * @param name
     *            the file
     * @param parent
     *            the parent node.
     * @param type
     *            file or directory
     * @see buildFileTree
     */
    public FileNodeString name, FileNode parent, FileType type {
        this.name = name;
        // TODO: complete this method.
    }

    /**
     * Find and return a child node named name in this directory tree, or null
     * if there is no such child node.
     *
     * @param name
     *            the file name to search for
     * @return the node named name
     */
    public FileNode findChildString name {
        FileNode result = null;
        // TODO: complete this method.
        return result;
    }

    /**
     * Return the name of the file or directory represented by this node.
     *
     * @return name of this Node
     */
    public String getName {
        return this.name;
    }

    /**
     * Set the name of the current node
     *
     * @param name
     *            of the file/directory
     */
    public void setNameString name {
        this.name = name;
    }

    /**
     * Return the child nodes of this node.
     *
     * @return the child nodes directly underneath this node.
     */
    public Collection<FileNode> getChildren {
        return this.children.values;
    }

    /**
     * Return this nodes parent.
     * 
     * @return the parent
     */
    public FileNode getParent {
        return parent;
    }

    /**
     * Set this nodes parent to p.
     * 
     * @param p
     *            the parent to set
     */
    public void setParentFileNode p {
        this.parent = p;
    }

    /**
     * Add childNode, representing a file or directory named name, as a child of
     * this node.
     * 
     * @param name
     *            the name of the file or directory
     * @param childNode
     *            the node to add as a child
     */
    public void addChildString name, FileNode childNode {
        this.children.putname, childNode;
    }

    /**
     * Return whether this node represents a directory.
     * 
     * @return whether this node represents a directory.
     */
    public boolean isDirectory {
        return this.type == FileType.DIRECTORY;
    }

    /**
     * This method is for code that tests this class.
     * 
     * @param args
     *            the command line args.
     */
    public static void mainString[] args {
        System.out.println"Testing FileNode";
        FileNode f1 = new FileNode"top", null, FileType.DIRECTORY;
        if !f1.getName.equals"top" {
            System.out.println"Error: " + f1.getName + " should be " + "top";
        }

    }

}
 package test;

import java.util.Map;
import java.security.KeyStore.Entry;
import java.util.Collection;
import java.util.HashMap;

/**
 * The root of a tree representing a directory structure.
 */
public class FileNode {

    /** The name of the file or directory this node represents. */
    private String name;
    /** Whether this node represents a file or a directory. */
    private FileType type;
    /** This nodes parent. */
    private FileNode parent;
    /**
     * This nodes children, mapped from the file names to the nodes. If type is
     * FileType.FILE, this is null.
     */
    private Map<String, FileNode> children;

    /**
     * A node in this tree.
     *
     * @param name
     *            the file
     * @param parent
     *            the parent node.
     * @param type
     *            file or directory
     * @see buildFileTree
     */
    public FileNodeString name, FileNode parent, FileType type {
        this.name = name;
        this.parent = parent;
        this.type = type;
    }

    /**
     * Find and return a child node named name in this directory tree, or null
     * if there is no such child node.
     *
     * @param name
     *            the file name to search for
     * @return the node named name
     */
    public FileNode findChildString name {
        FileNode result = null;
        forString str: children.keySet {
            ifstr.equalsname {
                return children.getstr;
            }
        }
        return result;
    }

    /**
     * Return the name of the file or directory represented by this node.
     *
     * @return name of this Node
     */
    public String getName {
        return this.name;
    }

    /**
     * Set the name of the current node
     *
     * @param name
     *            of the file/directory
     */
    public void setNameString name {
        this.name = name;
    }

    /**
     * Return the child nodes of this node.
     *
     * @return the child nodes directly underneath this node.
     */
    public Collection<FileNode> getChildren {
        return this.children.values;
    }

    /**
     * Return this nodes parent.
     * 
     * @return the parent
     */
    public FileNode getParent {
        return parent;
    }

    /**
     * Set this nodes parent to p.
     * 
     * @param p
     *            the parent to set
     */
    public void setParentFileNode p {
        this.parent = p;
    }

    /**
     * Add childNode, representing a file or directory named name, as a child of
     * this node.
     * 
     * @param name
     *            the name of the file or directory
     * @param childNode
     *            the node to add as a child
     */
    public void addChildString name, FileNode childNode {
        this.children.putname, childNode;
    }

    /**
     * Return whether this node represents a directory.
     * 
     * @return whether this node represents a directory.
     */
    public boolean isDirectory {
        return this.type == FileType.DIRECTORY;
    }

    /**
     * This method is for code that tests this class.
     * 
     * @param args
     *            the command line args.
     */
    public static void mainString[] args {
        System.out.println"Testing FileNode";
        FileNode f1 = new FileNode"top", null, FileType.DIRECTORY;
        if !f1.getName.equals"top" {
            System.out.println"Error: " + f1.getName + " should be " + "top";
        }

    }

}
public class FileNode {
    
    String name;
    FileNode parent;
    List<FileNode> children;
    
    FileNodeString name, FileNode parent, FileType type {
        this.children = new ArrayList<>;
        this.name = name;
        this.setParentparent;
    }
    
    public void setParentFileNode parent {
        if this.parent != null {
            this.parent.children.removethis;
        }
        this.parent = parent;
        this.parent.addChildthis;
    }
    
    public FileNode findChildString name {
        for FileNode node : this.children {
            if node.name.equalsname {
                return node;
            }
        }
        return null;
    }
    
    public void addChildFileNode node {
        if !this.children.containsnode {
            this.children.addnode;
        }
    }
    
    @Override
    public int hashCode {
        return this.name.hashCode;
    }
    
    @Override
    public boolean equalsObject obj {
        if obj instanceof FileNode {
            FileNode node = FileNode obj;
            if node.name.equalsthis.name {
                return true;
            }
        }
        return false;
    }
}
public FileNode findChildString name{
    ifthis.children!=null{
        ifname!=null{
            FileNode fileNode = null ;
            //此处每个节点 
            for String key : children.keySet {
                ifkey.equalsname{
                    fileNode = children.getkey;
                }
            }
            return fileNode;
        }else{
            return null;
        }
        
    }else{
        return null;
    }
}

栏目列表

广告位

澳门博彩导航网址