information on the connectivity of nodes and networks. Nodes may be connected
to varying numbers of networks and networks may be connected to varying
numbers of nodes. You may assume a maximum of 20 networks and 20 nodes at
one time will need to be represented.
Read the supplied file of connect statements (input.txt) of the form:
connect(node, network)
populating your data structure as you read down the file.
Your data structure should be designed for efficient retrieval of connection
information. Numerous design choices are available but do not store the data as a
simple copy of the input file. Try to build a model of the network structure rather
than directly of the input file. For example, try to use Node and Network classes
as part of your solution.
Devise an interactive facility to query the stored topology, offering the user the
following menu of choices:
Which search do you require?
1. networks – list all networks connected to a particular node
2. nodes – list all the nodes connected to a particular network
3. quit.
Selecting item 1 or 2, should cause the user to be prompted to input a node name
or network name, respectively, to be used as the basis for the search. Continue the
user dialogue, enabling the user to undertake further searches, until the quit option
is selected. Just use simple console io for user interactions.input.txt内容如下
connect(piquard, ethernet)
connect(piquard, firewire)
connect(worf, ethernet)
connect(worf, bluetooth)
connect(riker, ethernet)
connect(riker, wi-fi)
connect(crusher, firewire)
connect(laforge, ethernet)
connect(laforge, firewire)
connect(data, ethernet)
connect(data, wi-fi)
connect(guinan, wi-fi)
connect(o'brien, ethernet)
connect(o'brien, bluetooth)
connect(troy, wi-fi)
connect(troy, firewire)main函数也给了一部分
import java.io.*;
import java.util.*;/**
 * Exercise 29
 * 
 */
class Main { public static void main(String args[]) throws IOException { // read input file and build data structure in model FileReader reader = new FileReader("input.txt");
BufferedReader bReader = new BufferedReader(reader); String line = bReader.readLine(); while (line != null) { // check for eof
try {
StringTokenizer parseString = new StringTokenizer(line); String delimiters = " (),\t\n"; String connectKeyword = parseString.nextToken(delimiters);
String nodeName = parseString.nextToken(delimiters);
String netName = parseString.nextToken(delimiters); // expand here to build data structure .............
} catch (NoSuchElementException e) {
System.out.println("Invalid input line: " + line
+ ". Skipping ....");
} line = bReader.readLine();
} // allow user to query data structure
options();
} /**
 * Prompt users with options to either search for node, 
 * or network, or to quit
 */
public static void options() { Scanner keyboard = new Scanner(System.in); // expand here ............. } // end options
} // end class本人不是很懂,不懂的人就别乱回了,谢谢
运行满足要求给分
(注意要求中要有结构问题,自己构建结构)

解决方案 »

  1.   

    option你自己写吧
    数据结构帮你设计好了class Main { private static List<Name> ethernetNodeList = new ArrayList<Name>();
    private static List<Name> firewireNodeList = new ArrayList<Name>();
    private static List<Name> bluetoothNodeList = new ArrayList<Name>();
    private static List<Name> wfNodeList = new ArrayList<Name>(); private static Map<String, List<Name>> dataMap = new HashMap<String, List<Name>>();
    public static void main(String args[]) throws IOException {

    FileReader reader = new FileReader("input.txt");
    BufferedReader bReader = new BufferedReader(reader); String line = bReader.readLine(); while (line != null) { // check for eof
    try {
    StringTokenizer parseString = new StringTokenizer(line); String delimiters = " (),\t\n";
    // String delimiters = " ()"; String connectKeyword = parseString.nextToken(delimiters); // unnecessary
    String nodeName = parseString.nextToken(delimiters); // unnecessary
    String netName = parseString.nextToken(delimiters);
    System.out.println(netName); // expand here to build data structure .............
    if (netName == "ethernet") {
    ethernetNodeList.add(new Name(netName));
    } else if (netName == "firewire") {
    firewireNodeList.add(new Name(netName));
    } else if (netName == "bluetooth") {
    bluetoothNodeList.add(new Name(netName));
    } else if (netName == "wi-fi") {
    wfNodeList.add(new Name(netName));
    }
    } catch (NoSuchElementException e) {
    System.out.println("Invalid input line: " + line
    + ". Skipping ....");
    } line = bReader.readLine();
    }
    dataMap.put("ethernet", ethernetNodeList);
    dataMap.put("firewire", firewireNodeList);
    dataMap.put("bluetooth", bluetoothNodeList);
    dataMap.put("wi-fi", wfNodeList); // allow user to query data structure
    options(); //move this function into a thread
    } /**
     * Prompt users with options to either search for node, or network, or to
     * quit
     */
    public static void options() {  Scanner keyboard = new Scanner(System.in);  // expand here .............  } // end options } // end classclass Name { private String name = null; public Name(String n) {
    this.name = n;
    } public String getName() {
    return this.name;
    } public void setName(String n) {
    this.name = n;
    }
    }
      

  2.   

    代码说明:由于你提供的Main里面只有两个地方可以 // expand here ............. 
    只好写了一个比较笨的,只在这两个地方加代码,但用到了java.io.*和java.util.*,因为你Main里面已经import了,所以应该不算违规。另外两个类不在 //expand那里,是因为你要求要自定义class的import java.io.*;
    import java.util.*;/**
     * Exercise 29
     * 
     */
    public class Main { public static void main(String args[]) throws IOException { // read input file and build data structure in model FileReader reader = new FileReader("E:\\input.txt");
    BufferedReader bReader = new BufferedReader(reader); String line = bReader.readLine(); while (line != null) { // check for eof
    try {
    StringTokenizer parseString = new StringTokenizer(line); String delimiters = " (),\t\n"; String connectKeyword = parseString.nextToken(delimiters);
    String nodeName = parseString.nextToken(delimiters);
    String netName = parseString.nextToken(delimiters); Node.getInstance().addNodes(nodeName, netName);
    Network.getInstance().addNetworks(netName, nodeName);
    // expand here to build data structure .............
    } catch (NoSuchElementException e) {
    System.out.println("Invalid input line: " + line
    + ". Skipping ....");
    } line = bReader.readLine();
    } // allow user to query data structure
    options();
    } /**
     * Prompt users with options to either search for node, or network, or to
     * quit
     */
    public static void options() { Scanner keyboard = new Scanner(System.in);
    System.out.println("Which search do you require?");
    System.out
    .println("1. networks – list all networks connected to a particular node");
    System.out
    .println("2. nodes – list all the nodes connected to a particular network");
    System.out.println("3. quit. ");
    System.out.println(); while (keyboard.hasNext()) {
    String s = keyboard.next();
    if (s.equals("1")) {
    System.out.println("please type a network name, all name is:");
    System.out.println(Network.getInstance().getAllNames());
    System.out.println();
    while (keyboard.hasNext()) {
    String name = keyboard.next();
    if (Network.getInstance().getNetworkByName(name) == null) {
    System.out
    .println("please type a network name, all name is:");
    System.out.println(Network.getInstance().getAllNames());
    System.out.println();
    } else {
    System.out.println(Network.getInstance()
    .getNetworkByName(name));
    System.out.println("Which search do you require?");
    System.out
    .println("1. networks – list all networks connected to a particular node");
    System.out
    .println("2. nodes – list all the nodes connected to a particular network");
    System.out.println("3. quit. ");
    System.out.println();
    break;
    }
    }
    } else if (s.equals("2")) {
    System.out.println("please type a node name, all name is:");
    System.out.println(Node.getInstance().getAllNames());
    while (keyboard.hasNext()) {
    String name = keyboard.next();
    if (Node.getInstance().getNodeByName(name) == null) {
    System.out
    .println("please type a network name, all name is:");
    System.out.println(Node.getInstance().getAllNames());
    System.out.println();
    } else {
    System.out.println(Node.getInstance().getNodeByName(
    name));
    System.out.println("Which search do you require?");
    System.out
    .println("1. networks – list all networks connected to a particular node");
    System.out
    .println("2. nodes – list all the nodes connected to a particular network");
    System.out.println("3. quit. ");
    System.out.println();
    break;
    }
    }
    } else if (s.equals("3")) {
    System.out.println("quit");
    System.out.println();
    System.exit(0);
    } else {
    System.out.println("only 1,2,3 would be right");
    System.out.println("Which search do you require?");
    System.out
    .println("1. networks – list all networks connected to a particular node");
    System.out
    .println("2. nodes – list all the nodes connected to a particular network");
    System.out.println("3. quit. ");
    System.out.println();
    } }
    // expand here ............. } // end options static class Node {
    private Hashtable<String, String> nodes = new Hashtable<String, String>(); private static Node instance; private Node() {
    } public static Node getInstance() {
    if (instance == null) {
    instance = new Node();
    }
    return instance;
    } public String getNodeByName(String name) {
    return nodes.get(name);
    } public String getAllNames() {
    String names = "";
    try {
    Enumeration<String> keys = nodes.keys();
    while (keys.hasMoreElements()) {
    String key = keys.nextElement();
    names += key + ",";
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return names.equals("") ? names : names.substring(0,
    names.length() - 1);
    } public void addNodes(String nodeName, String netName) {
    if (nodes.containsKey(nodeName)) {
    nodes.put(nodeName, nodes.get(nodeName) + "," + netName);
    } else {
    nodes.put(nodeName, netName);
    } } } static class Network {
    private Hashtable<String, String> networks = new Hashtable<String, String>(); private static Network instance; private Network() {
    } public static Network getInstance() {
    if (instance == null) {
    instance = new Network(); }
    return instance;
    } public String getNetworkByName(String name) {
    return networks.get(name);
    } public String getAllNames() { String names = "";
    try {
    Enumeration<String> keys = networks.keys();
    while (keys.hasMoreElements()) { String key = keys.nextElement();
    names += key + ",";
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return names.equals("") ? names : names.substring(0,
    names.length() - 1);
    } public void addNetworks(String netName, String nodeName) {
    if (networks.containsKey(netName)) {
    networks.put(netName, networks.get(netName) + "," + nodeName);
    } else {
    networks.put(netName, nodeName);
    } }
    }
    } // end class
      

  3.   

    FileReader reader = new FileReader("E:\\input.txt"); 
    这里应该去掉E:\\我测试图方便,直接扔E盘了,应该把文件和编译好的类文件放在一起。
      

  4.   

    3楼的代码运行过了,很好,完全满足要求啦。谢谢:)不过还有一点小问题~~就是Hashtable 没学过,而且今天老师上课讲了下说可以用Vector实现。 能不能再改下代码呢?谢谢啦如果改起来很麻烦那就讲解一下Hashtable 怎么用还有和Vector的区别吧再次感谢,救命恩人啊~~恩还有一点,就是英语语法问题了1. networks – list all networks connected to a particular node 是指列出一点让然后显示所有联系的网络,貌似你和2刚好写反了,不过这个不用改了,我自己能改,只是顺便说一下~~
      

  5.   

    二楼代码上来的
        private static List<Name> ethernetNodeList = new ArrayList<Name>();
        private static List<Name> firewireNodeList = new ArrayList<Name>();
        private static List<Name> bluetoothNodeList = new ArrayList<Name>();
        private static List<Name> wfNodeList = new ArrayList<Name>();
    感觉没什么结构性,而且添加减少连接也不是很方便,不过还是谢谢了:)
      

  6.   

    3楼还有一点小问题~~今天老师特意说了要用到object,就是说node和network的type都应该是object的,貌似你用的是String的。 所以还要改一下~~谢啦
      

  7.   


    话说你随便搜一下就知道 hashtable怎么用了。
      

  8.   


    话说你随便搜一下就知道 hashtable怎么用了。对比一个hashmap你就更清楚了,vector现在来说已经不提倡用了
      

  9.   


    object?String就是个Object咧。基本上所有的实体类的super class 都是java.lang.Object
    Hashtable不能用?因为考虑到你这里要比对key,才用的hashtable,要用Vector也行的。please wait....
    ps:我E文不咋的,4级第二次才过,考了61分,555555
      

  10.   

    用Vector的改好了。英文没读懂的搞反了的地方,还是请你自己改下吧。import java.io.*;
    import java.util.*;/**
     * Exercise 29
     * 
     */
    public class Main { public static void main(String args[]) throws IOException { // read input file and build data structure in model FileReader reader = new FileReader("E:\\input.txt");
    BufferedReader bReader = new BufferedReader(reader); String line = bReader.readLine(); while (line != null) { // check for eof
    try {
    StringTokenizer parseString = new StringTokenizer(line); String delimiters = " (),\t\n"; String connectKeyword = parseString.nextToken(delimiters);
    String nodeName = parseString.nextToken(delimiters);
    String netName = parseString.nextToken(delimiters); Node.getInstance().addNodes(nodeName, netName);
    Network.getInstance().addNetworks(netName, nodeName);
    // expand here to build data structure .............
    } catch (NoSuchElementException e) {
    System.out.println("Invalid input line: " + line
    + ". Skipping ....");
    } line = bReader.readLine();
    } // allow user to query data structure
    options();
    } /**
     * Prompt users with options to either search for node, or network, or to
     * quit
     */
    public static void options() { Scanner keyboard = new Scanner(System.in);
    System.out.println("Which search do you require?");
    System.out
    .println("1. networks – list all networks connected to a particular node");
    System.out
    .println("2. nodes – list all the nodes connected to a particular network");
    System.out.println("3. quit. ");
    System.out.println(); while (keyboard.hasNext()) {
    String s = keyboard.next();
    if (s.equals("1")) {
    System.out.println("please type a network name, all name is:");
    System.out.println(Network.getInstance().getAllNames());
    System.out.println();
    while (keyboard.hasNext()) {
    String name = keyboard.next();
    if (Network.getInstance().getNetworkByName(name) == null) {
    System.out
    .println("please type a network name, all name is:");
    System.out.println(Network.getInstance().getAllNames());
    System.out.println();
    } else {
    System.out.println(Network.getInstance()
    .getNetworkByName(name));
    System.out.println("Which search do you require?");
    System.out
    .println("1. networks – list all networks connected to a particular node");
    System.out
    .println("2. nodes – list all the nodes connected to a particular network");
    System.out.println("3. quit. ");
    System.out.println();
    break;
    }
    }
    } else if (s.equals("2")) {
    System.out.println("please type a node name, all name is:");
    System.out.println(Node.getInstance().getAllNames());
    while (keyboard.hasNext()) {
    String name = keyboard.next();
    if (Node.getInstance().getNodeByName(name) == null) {
    System.out
    .println("please type a network name, all name is:");
    System.out.println(Node.getInstance().getAllNames());
    System.out.println();
    } else {
    System.out.println(Node.getInstance().getNodeByName(
    name));
    System.out.println("Which search do you require?");
    System.out
    .println("1. networks – list all networks connected to a particular node");
    System.out
    .println("2. nodes – list all the nodes connected to a particular network");
    System.out.println("3. quit. ");
    System.out.println();
    break;
    }
    }
    } else if (s.equals("3")) {
    System.out.println("quit");
    System.out.println();
    System.exit(0);
    } else {
    System.out.println("only 1,2,3 would be right");
    System.out.println("Which search do you require?");
    System.out
    .println("1. networks – list all networks connected to a particular node");
    System.out
    .println("2. nodes – list all the nodes connected to a particular network");
    System.out.println("3. quit. ");
    System.out.println();
    } }
    // expand here ............. } // end options static class Node {
    private Vector<String[]> nodes = new Vector<String[]>(); private static Node instance; private Node() {
    } public static Node getInstance() {
    if (instance == null) {
    instance = new Node();
    }
    return instance;
    } public String getNodeByName(String name) {
    for(int i=0;i<nodes.size();i++){
    if(nodes.get(i)[0].equals(name)){
    return nodes.get(i)[1];
    }
    }
    return "";
    } public String getAllNames() {
    String names = "";
    try {
    for(int i=0;i<nodes.size();i++){
    String key = nodes.get(i)[0];
    names += key + ",";
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return names.equals("") ? names : names.substring(0,
    names.length() - 1);
    } public void addNodes(String nodeName, String netName) {
    boolean isAdd = false;
    for(int i=0;i<nodes.size();i++){
    if(nodes.get(i)[0].equals(nodeName)){
    nodes.get(i)[1] += "," + netName;
    isAdd = true;
    break;
    }
    }
    if(!isAdd){
    nodes.add(new String[]{nodeName,netName});
    }
    } } static class Network {
    private Vector<String[]> networks = new Vector<String[]>(); private static Network instance; private Network() {
    } public static Network getInstance() {
    if (instance == null) {
    instance = new Network(); }
    return instance;
    } public String getNetworkByName(String name) {
    for(int i=0;i<networks.size();i++){
    if(networks.get(i)[0].equals(name)){
    return networks.get(i)[1];
    }
    }
    return "";
    } public String getAllNames() { String names = "";
    try {
    for(int i=0;i<networks.size();i++){
    String key = networks.get(i)[0];
    names += key + ",";
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return names.equals("") ? names : names.substring(0,
    names.length() - 1);
    } public void addNetworks(String netName, String nodeName) {
    boolean isAdd = false;
    for(int i=0;i<networks.size();i++){
    if(networks.get(i)[0].equals(netName)){
    networks.get(i)[1] += "," + nodeName;
    isAdd = true;
    break;
    }
    }
    if(!isAdd){
    networks.add(new String[]{netName,nodeName});
    } }
    }
    } // end class