Assignment of a college student (3)-request modification for the first reply

Source: Internet
Author: User

College students:

 

The Code has been run, which is good and fully meets the requirements.

But there is still a small problem ~~ That is, hashtable has never been learned, and today the teacher told me that it can be implemented using vector. Can I change the code?

Another point is the English syntax. 1. Networks-list all networks connected to
A special node is used to list a network that allows all the connections to be displayed. It seems that you and 2 are reversed, but you don't need to change it. I can change it myself, just by the way ~~

 

There is still a small problem ~~

Today, the teacher specifically said that the object is to be used, that is to say, the node and network types should be object, and it seems that you are using string. So you need to change it ~~ Thanks

 

 

Reply:

Object? String is an object. Basically, the super class of all object classes is Java. Lang. object...
Why can't hashtable be used? Because you need to compare the key here, hashtable is used, and vector is also used. Please wait ....
PS: I did not know exactly what I did. I did this for the second time in Level 4. I scored 61 points, 555555 ......

 

Modified with vector. If you have not understood the English language, please try again ....

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
* Quit
*/
Public static void options (){

Export keyboard = new partition (system. In );
System. Out. println ("which search do you require? ");
System. Out
. Println ("1. Networks-list all networks connected to a participant node ");
System. Out
. Println ("2. nodes-list all the nodes connected to a participant 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 participant node ");
System. Out
. Println ("2. nodes-list all the nodes connected to a participant 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 participant node ");
System. out
. Println ("2. nodes-list all the nodes connected to a participant 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 wocould be right ");
System. out. println ("Which search do you require? ");
System. out
. Println ("1. networks-list all networks connected to a participant node ");
System. out
. Println ("2. nodes-list all the nodes connected to a participant 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.