In: Computer Science
Topic: Computer Science | Java Programming
Complete the class Packet below. The Packet class contains: Four private instance variables to store information regarding to the source host, destination host, time stamp and ip packet size. A constructor that takes a string object as a parameter and set up instance variables defined above. public accessor methods: getSourceHost(), getDestinationHost(), getTimeStamp(), and getIpPacketSize(); public mutator methods: setSourceHost(), setDestinationHost(), setTimeStamp(), and setIpPacketSize(); A toString() method that returns a string description of a packet.
Test case1: Packet p1 = new Packet("1\t0.000000000\t192.168.0.24\t\t10.0.0.5\t\t98\t84\t\t\t\t\t\t\t\t1"); System.out.println(p1);
Excepted output src=192.168.0.24, dest=10.0.0.5, time=0.00, size=84
Test case2: Packet p2 = new Packet("6\t5.000675000\t\t\t\t\t60\t\t\t\t\t\t\t\t\t"); System.out.println(p2);
Excepted output src=, dest=, time=5.00, size=0
Test case3:
Packet p3 = new Packet("36\t33.434837000\t192.168.0.15\t8000\t10.0.0.5\t47745\t74\t60\t0\t1\t1\t0\t0\t0\t1\t1"); System.out.println(p3);
Excepted output: src=192.168.0.15, dest=10.0.0.5, time=33.43, size=60
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Packet.java
public class Packet {
// attributes
private String source;
private String dest;
private double timestamp;
private int size;
// constructor taking a String containing all data values
public Packet(String text) {
// initializing instance variables with default values
source = dest = "";
timestamp = 0;
size = 0;
// splitting the text by tab, -1 indicates that the empty values are not
// avoided. otherwise Java will skip empty values between tabs and we
// cannot process the contents correctly.
String fields[] = text.split("\t", -1);
// ensuring that the length of resultant array is at least 8
if (fields.length >= 8) {
// taking String at index 2 as source host
source = fields[2].trim();
// taking String at index 4 as destination host
dest = fields[4].trim();
// using the helper method parseAsNumber, parsing String at index 1
// as a double value for timestamp
timestamp = parseAsNumber(fields[1]);
// parsing String at index 7 as an int value for size
size = (int) parseAsNumber(fields[7]);
}
}
// helper method to parse a String to numeric format
private double parseAsNumber(String text) {
// trying to convert text to a number
try {
double number = Double.parseDouble(text);
// if conversion is successful, returning the number
return number;
} catch (Exception e) {
}
// if failed, returning 0 as default value
return 0;
}
// getters and setters
public String getSourceHost() {
return source;
}
public String getDestinationHost() {
return dest;
}
public double getTimeStamp() {
return timestamp;
}
public int getIpPacketSize() {
return size;
}
public void setSourceHost(String source) {
this.source = source;
}
public void setDestinationHost(String dest) {
this.dest = dest;
}
public void setTimeStamp(double timestamp) {
this.timestamp = timestamp;
}
public void setIpPacketSize(int size) {
this.size = size;
}
// returns a formatted String containing all details about the packet
public String toString() {
return String.format("src=%s, dest=%s, time=%.2f, size=%d", source,
dest, timestamp, size);
}
public static void main(String[] args) {
// testing using all three test cases
Packet p1 = new Packet(
"1\t0.000000000\t192.168.0.24\t\t10.0.0.5\t\t98\t84\t\t\t\t\t\t\t\t1");
System.out.println(p1);
Packet p2 = new Packet("6\t5.000675000\t\t\t\t\t60\t\t\t\t\t\t\t\t\t");
System.out.println(p2);
Packet p3 = new Packet(
"36\t33.434837000\t192.168.0.15\t8000\t10.0.0.5\t47745\t74\t60\t0\t1\t1\t0\t0\t0\t1\t1");
System.out.println(p3);
}
}
/*OUTPUT*/
src=192.168.0.24, dest=10.0.0.5, time=0.00, size=84
src=, dest=, time=5.00, size=0
src=192.168.0.15, dest=10.0.0.5, time=33.43, size=60