Write a program to print the network using the given connections. The network nodes are letters like A, B, C, D etc. Every connection is defined by two nodes and the direction. The valid directions are L for Left, R for Right, T for Top and B for Bottom. If the connection is A-B-R, it means B is to the right of A, similarly C-E-B, it means E is to the bottom of C.
Input (List) | Printed Output |
---|---|
[A-B-R, B-C-R, C-D-R, D-E-R, E-F-R] | A----B----C----D----E----F |
[D-B-L, A-B-R, C-A-T, C-G-B, H-G-L, H-I-R, I-F-T] | A----B----D |
[A-B-R, C-A-T, C-D-R, E-D-L, E-G-B, G-F-L] | A----B |
[A-B-R, C-A-T, C-G-B, H-G-L, H-I-R, I-F-T] | A----B |
[A-B-R, C-A-T, C-G-B, C-M-R, B-Z-T, H-G-L, H-I-R, I-F-T] | Z |
class PrintNetwork
{ public static void main(String s[])
{
List connections = new ArrayList();
connections.add(new Connection('A', 'B', 'B'));
connections.add(new Connection('C', 'B', 'R'));
connections.add(new Connection('B', 'D', 'R'));
connections.add(new Connection('D', 'E', 'R'));
connections.add(new Connection('D', 'F', 'B'));
printConnections(connections);
}
public static void printConnections(List<Connection> connections) {
//Write a code here to print the network using the connections.
}
//If required write any additional methods here.
}
class Connection {
char start;
char end;
char direction;
public Connection(char start, char end, char direction) {
this.start = start;
this.end = end;
this.direction = direction;
}
@Override
public String toString() {
return start + "-" + end + "-" + direction;
}
}
//If required define any other classes here
Topic: Collection Framework In Java