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本人不是很懂,不懂的人就别乱回了,谢谢
运行满足要求给分
(注意要求中要有结构问题,自己构建结构)
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本人不是很懂,不懂的人就别乱回了,谢谢
运行满足要求给分
(注意要求中要有结构问题,自己构建结构)
解决方案 »
- 有没有知道java反射及类加载细节的前辈呢?
- 求助,java环境变量中的path和classpath什么关系,各自都引到的是谁的路径
- javaMail中遇到530-5.5.1 Authentication Required.
- JAVA中的CheckBox类的常用方法
- 开发一个考试系统,如何控制考生的时间,比如:45分钟强制交卷
- JMF能不能在程序里动态刷新设备
- Java中有现成的计算椭圆周长的方法吗?
- 这样做总觉得不舒服,帮我看看
- java可以从串口读数据吗,如果能怎么读用那个类库?(在线等,谢谢!)
- 请问如何判断一个字符串是否为空呀?
- 那位大哥手头有 lws格式的3d文件 发我分
- 请教高手一个排序问题
数据结构帮你设计好了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;
}
}
只好写了一个比较笨的,只在这两个地方加代码,但用到了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
这里应该去掉E:\\我测试图方便,直接扔E盘了,应该把文件和编译好的类文件放在一起。
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>();
感觉没什么结构性,而且添加减少连接也不是很方便,不过还是谢谢了:)
话说你随便搜一下就知道 hashtable怎么用了。
话说你随便搜一下就知道 hashtable怎么用了。对比一个hashmap你就更清楚了,vector现在来说已经不提倡用了
object?String就是个Object咧。基本上所有的实体类的super class 都是java.lang.Object
Hashtable不能用?因为考虑到你这里要比对key,才用的hashtable,要用Vector也行的。please wait....
ps:我E文不咋的,4级第二次才过,考了61分,555555
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