logo

Crowdly

Browser

Додати до Chrome

---------------------------------- Data.java import java.time.LocalDate; ...

✅ Перевірена відповідь на це питання доступна нижче. Наші рішення, перевірені спільнотою, допомагають краще зрозуміти матеріал.

---------------------------------- Data.java

import java.time.LocalDate;

class Data {

int value; //value of transmitted data

LocalDate time; //time of sending/receiving

Data(int value) {

this.value = value;

this.time = LocalDate.now();

}

}

---------------------------------- Network.java

import java.time.LocalDate;

class Network {

class Node {

char id; //Node identifier

final Node[] ports; // array of nodes the node is connected to

Node(int size, char id) {

this.ports = new Node[size];

this.id = id;

}

boolean send(Data data, int port) { //sends data to node on specified port

if (ports[port] == null) return false;

ports[port].receive(data); // data sent by calling receive from receiver node

return true;

}

int broadcast(Data data) { //sends datagram to all ports to which a Node is connected

for (int i = 0; i < ports.length; i++) {

if (ports[i] != null) send (data, i);

} return data.value;

}

void receive(Data data) { //when Node receives data, it logs the event to the console

data.time = LocalDate.now();

System.out.println("Node " + id +" has received one transmission with content "+ data.value + " on " + data.time.toString());

}

}

}

---------------------------------- Main.java

public class Main {

public static void main(String[] args) {

//build Network

Network n = new Network();

Node nodeA = n.new Node(3, 'A');

Node nodeB = n.new Node(3, 'B');

Node nodeC = n.new Node(3, 'C');

//connect nodes

nodeA.ports[0] = nodeB;

nodeA.ports[1] = nodeC;

nodeB.ports[0] = nodeC;

nodeC.ports[0] = nodeA;

//create Data

Data fromA = new Data(1906); //Birth year of Grace Hopper

Data fromB = new Data(1912); //Birth year of Alan Turing

Data fromC = new Data(1815); //Birth year of Ada Lovelace

//broadcasts: send Data to all connnected nodes

nodeA.broadcast(fromA);

nodeB.broadcast(fromB);

nodeC.broadcast(fromC);

}

}

What is the issue that prevents this code from working (assume that all classes are in the same package)?

1) We cannot simply access the ports array using the indices - we would need an Iterator to get the values in the array.

2) Node is an inner class of Network - in order to use it as a data type in Main, we need to refer to it as Network.Node.

3) Broadcasts cannot work as the ports array contents is not visible.

4) It is not possible to define class Node within the scope of the class Network.

Більше питань подібних до цього

Хочете миттєвий доступ до всіх перевірених відповідей на moodle.jku.at?

Отримайте необмежений доступ до відповідей на екзаменаційні питання - встановіть розширення Crowdly зараз!

Browser

Додати до Chrome