(java help ) In Project 1 we laid the foundation of what will become a simple ne
ID: 3725963 • Letter: #
Question
(java help ) In Project 1 we laid the foundation of what will become a simple network packet transmission simulation. This project will build upon this foundation, utilizing a queue structure to store atomic packets of data which will travel across the network. The advantage of a queue is that, once a routing system is constructed in Project 3, latency will naturally be minimized as packets are essentially prioritized by wait time. Please construct the following class, defined in a file named Packet.java. Packet This class should contain the following private variables. You should define public functions which get and set the values of these variables. • An integer called targetID • An integer called sourceID • A String called data Network Object This class (from Project 1) should be modified to contain a private queue variable called packets. How you decide to implement the queue is up to you, so long as it functions according to the normal FIFO expectations of a queue and stores references to packet objects. We will use this queue of packets in Project 3 to begin routing packets between network objects and begin to simulate communication between network objects. project 1 :class NetworkObject{ private int objectld; private NetworkObject nextNetworkObject; public NetworkObject(int x){ objectld=x; } public int getObjectld(){ return objectld; } public NetworkObject getNextNetworkObject(){ return nextNetworkObject; } public void setNextNetworkObject(NetworkObject x){ nextNetworkObject = x; }} public class PROJECT1CSC220 { public static void main(String[] args){ NetworkObject p; NetworkObject n1 = new NetworkObject(1); NetworkObject n2 = new NetworkObject(2); NetworkObject n3 = new NetworkObject(3); n3.setNextNetworkObject(null); n1.setNextNetworkObject(n2); n2.setNextNetworkObject(n3); p = n1; while (p != null){ System.out.println(p.getObjectld()); p = p.getNextNetworkObject(); } }}
Explanation / Answer
1. New class called Packet.java with all mentioned fields and methods
2. Network class modified with a new packet queue.
3. I have added few methods in Network class to use this queue (which you might use in your project 3) . It was not part of assignment so If not needed you can remove them
/////////////////////////////////////////Packet.java/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class Packet {
private int targetID;
private int sourceID;
private String dataNetworkObject;
public int getTargetID() {
return targetID;
}
public void setTargetID(int targetID) {
this.targetID = targetID;
}
public int getSourceID() {
return sourceID;
}
public void setSourceID(int sourceID) {
this.sourceID = sourceID;
}
public String getDataNetworkObject() {
return dataNetworkObject;
}
public void setDataNetworkObject(String dataNetworkObject) {
this.dataNetworkObject = dataNetworkObject;
}
}
///////////////////////////////////////////////////////////////////////////NetworkObject.java//////////////////////////////////////////////////////////////////////////////
package networkAssignment;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
class NetworkObject {
private int objectld;
private NetworkObject nextNetworkObject;
//List of Packets
// LinkedList is used as Queue implementation.
Queue<Packet> packets = new LinkedList<Packet>();
public NetworkObject(int x) {
objectld = x;
}
public int getObjectld() {
return objectld;
}
public NetworkObject getNextNetworkObject() {
return nextNetworkObject;
}
public void setNextNetworkObject(NetworkObject x) {
nextNetworkObject = x;
}
// add a packet to queue
public void addElementToQueue(Packet p) {
packets.add(p);
}
// return packets
public Queue<Packet> getPackets() {
return packets;
}
/**
* To view the head of queue
*
* @return returns the first element of queue
*/
public Packet getPacketHead() {
Packet head = null;
if (packets.size() > 0) {
head = packets.peek();
System.out.println("head of queue=" + head);
}
return head;
}
// Print all the elements of Packet
public void printElementsPacket() {
Iterator<Packet> i = packets.iterator();
while (i.hasNext())
System.out.print(i.next() + " ");
}
}
///////////////////////////////////////////////////////PROJECT1CSC220.java////////////////////////////////////////////////////////////////////////////////////////
public class PROJECT1CSC220 {
public static void main(String[] args) {
NetworkObject p;
NetworkObject n1 = new NetworkObject(1);
NetworkObject n2 = new NetworkObject(2);
NetworkObject n3 = new NetworkObject(3);
n3.setNextNetworkObject(null);
n1.setNextNetworkObject(n2);
n2.setNextNetworkObject(n3);
p = n1;
while (p != null) {
System.out.println(p.getObjectld());
p = p.getNextNetworkObject();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.