Question

In: Computer Science

Topic: Computer Science | Java Programming Complete the class Packet below. The Packet class contains: Four...

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

Solutions

Expert Solution

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


Related Solutions

USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static...
USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static methods for performing common DNA-based operations in * bioinformatics. * * */ public class BasicBioinformatics { /** * Calculates and returns the number of times each type of nucleotide occurs in a DNA sequence. * * @param dna a char array representing a DNA sequence of arbitrary length, containing only the * characters A, C, G and T * * @return an int array...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A double data field named price (for the price of a ticket from this machine). • A double data field named balance (for the amount of money entered by a customer). • A double data field named total (for total amount of money collected by the machine). • A constructor that creates a TicketMachine with all the three fields initialized to some values. • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A string data field named symbol1 for the stock’s symbol. • A string data field named name for the stock’s name. • A double data field named previousClosingPrice that stores the stock price for the previous day. • A double data field named currentPrice that stores the stock price for the current time. • A constructor that creates a stock with the specified symbol and...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
USING JAVA: complete these one method in the BasicBioinformatics class /** * Class BasicBioinformatics contains static...
USING JAVA: complete these one method in the BasicBioinformatics class /** * Class BasicBioinformatics contains static methods for performing common DNA-based operations in * bioinformatics. * * */ public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each...
Computer Science - Java Programming How do you read a text file and store three different...
Computer Science - Java Programming How do you read a text file and store three different pieces of information in the database when the given text file contains this info.: 12345 Computer Science Bob Stone 23456 Art James G. Ocean? These are written in the format as ID Class Name. I was going to take the three different pieces of information by separating them by spaces, but the number of spaces is random and I don't know how to adjust...
JAVA PROGRAMMING Implement a class Purse. A purse contains a collection of coins. For simplicity, we...
JAVA PROGRAMMING Implement a class Purse. A purse contains a collection of coins. For simplicity, we will only store the coin names in an ArrayList<String>. Supply a method void addCoin(String coinName). Add a method toString to the Purse class that prints the coins in the purse in the format Purse[Quarter,Dime,Nickel,Dime]. Write a method reverse that reverses the sequence of coins in a purse. Implement a TestPurse class with a main method in it. It will use the toString method to...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data field named id for the account (default 0). 2. A private double data field named balance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A no-arg constructor that creates a default account. 6....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT