1. 程式人生 > 其它 >The code that comes with this project consists of two classes

The code that comes with this project consists of two classes

The code that comes with this project consists of two classes, namely IDSWorkerClass.java and PrioritizedNetworkTraffic.java. IDSWorkerClass contains code that is used by the NIDS to operate on the priority queue. PrioritizedNetworkTraffic is the class that implements the actual priority queue and its operations, and hence is the class that students are required to implement in this project. Students can verify the correctness of their PrioritizedNetworkTraffic class by comparing their output with the following output: IDS analysis of prioritized network traffic is running...

-------------------------------------------------------------

Displaying priority queue data...

Index: 0 Packet Priority: -1

Index: 1 Packet Priority: -1

Index: 2 Packet Priority: -1

Index: 3 Packet Priority: -1

Index: 4 Packet Priority: -1

Index: 5 Packet Priority: -1

Index: 6 Packet Priority: -1

Index: 7 Packet Priority: -1

Current Head: -1;

Current Tail: 0

-------------------------------------------------------------

At Enqueue: inserted 4 in index 0

-------------------------------------------------------------

Displaying priority queque data...

Index: 0 Packet Priority: 4

Index: 1 Packet Priority: -1

Index: 2 Packet Priority: -1

Index: 3 Packet Priority: -1

Index: 4 Packet Priority: -1

Index: 5 Packet Priority: -1

Index: 6 Packet Priority: -1

Index: 7 Packet Priority: -1

Current Head: 0;

Current Tail: 1

-------------------------------------------------------------

At Enqueue: inserted 9 in index 1

-------------------------------------------------------------

Displaying priority queue data...

Index: 0 Packet Priority: 4

Index: 1 Packet Priority: 9

Index: 2 Packet Priority: -1

Index: 3 Packet Priority: -1

Index: 4 Packet Priority: -1

Index: 5 Packet Priority: -1

Index: 6 Packet Priority: -1

Index: 7 Packet Priority: -1

Current Head: 1;

Current Tail: 2

-------------------------------------------------------------

At Enqueue: inserted 15 in index 2

-------------------------------------------------------------

Displaying priority queue data...

Index: 0 Packet Priority: 4

Index: 1 Packet Priority: 9

Index: 2 Packet Priority: 15

Index: 3 Packet Priority: -1

Index: 4 Packet Priority: -1

Index: 5 Packet Priority: -1

Index: 6 Packet Priority: -1

Index: 7 Packet Priority: -1

Current Head: 2;

Current Tail: 3

-------------------------------------------------------------

At Enqueue: inserted 1 in index 3

-------------------------------------------------------------

Displaying priority queue data...

Index: 0 Packet Priority: 4

Index: 1 Packet Priority: 9

Index: 2 Packet Priority: 15

Index: 3 Packet Priority: 1

Index: 4 Packet Priority: -1

Index: 5 Packet Priority: -1

Index: 6 Packet Priority: -1

Index: 7 Packet Priority: -1

Current Head: 2;

Current Tail: 4

-------------------------------------------------------------

At Enqueue: inserted 4 in index 4

-------------------------------------------------------------

Displaying priority queue data...

Index: 0 Packet Priority: 4

Index: 1 Packet Priority: 9

Index: 2 Packet Priority: 15

Index: 3 Packet Priority: 1

Index: 4 Packet Priority: 4

Index: 5 Packet Priority: -1

Index: 6 Packet Priority: -1

Index: 7 Packet Priority: -1

Current Head: 2;

Current Tail: 5

-------------------------------------------------------------

At Enqueue: inserted 10 in index 5

-------------------------------------------------------------

Displaying priority queue data...

Index: 0 Packet Priority: 4

Index: 1 Packet Priority: 9

Index: 2 Packet Priority: 15

Index: 3 Packet Priority: 1

Index: 4 Packet Priority: 4

Index: 5 Packet Priority: 10

Index: 6 Packet Priority: -1

Index: 7 Packet Priority: -1

Current Head: 2;

Current Tail: 6

-------------------------------------------------------------

At Enqueue: inserted 5 in index 6

-------------------------------------------------------------

Displaying priority queue data...

Index: 0 Packet Priority: 4

Index: 1 Packet Priority: 9

Index: 2 Packet Priority: 15

Index: 3 Packet Priority: 1

Index: 4 Packet Priority: 4

Index: 5 Packet Priority: 10

Index: 6 Packet Priority: 5

Index: 7 Packet Priority: -1

Current Head: 2;

Current Tail: 7

-------------------------------------------------------------

At Enqueue: inserted 3 in index 7

-------------------------------------------------------------

Displaying priority queue data...

Index: 0 Packet Priority: 4

Index: 1 Packet Priority: 9

Index: 2 Packet Priority: 15

Index: 3 Packet Priority: 1

Index: 4 Packet Priority: 4

Index: 5 Packet Priority: 10

Index: 6 Packet Priority: 5

Index: 7 Packet Priority: 3

Current Head: 2;

Current Tail: 3

-------------------------------------------------------------

At Dequeue: removed 15 from index 2

-------------------------------------------------------------

Displaying priority queue data...

Index: 0 Packet Priority: 4

Index: 1 Packet Priority: 9

Index: 2 Packet Priority: -1

Index: 3 Packet Priority: 1

Index: 4 Packet Priority: 4

Index: 5 Packet Priority: 10

Index: 6 Packet Priority: 5

Index: 7 Packet Priority: 3

Current Head: 5;

Current Tail: 2

-------------------------------------------------------------

At Enqueue: inserted 17 in index 2

-------------------------------------------------------------

Displaying priority queue data...

Index: 0 Packet Priority: 4

Index: 1 Packet Priority: 9

Index: 2 Packet Priority: 17

Index: 3 Packet Priority: 1

Index: 4 Packet Priority: 4

Index: 5 Packet Priority: 10

Index: 6 Packet Priority: 5

Index: 7 Packet Priority: 3

Current Head: 2;

Current Tail: 3

---------------------------------------

IDSWorkerClass.java

import textbook.PrioritizedNetworkTraffic;

public class IDSWorkerClass {

   public static void main(String[] args) {
      
       int arraySize = 8;
      
       int Max = 0;
      
       System.out.println("IDS analysis of prioritized network traffic is running...\n");
      
       PrioritizedNetworkTraffic ptraffic = new PrioritizedNetworkTraffic(arraySize);
  
       ptraffic.DisplayPriorityQueue();
      
ptraffic.Enqueue(4);
ptraffic.DisplayPriorityQueue();
  
ptraffic.Enqueue(9);
ptraffic.DisplayPriorityQueue();
  
ptraffic.Enqueue(15);
ptraffic.DisplayPriorityQueue();
  
ptraffic.Enqueue(1);
ptraffic.DisplayPriorityQueue();
  
ptraffic.Enqueue(4);
ptraffic.DisplayPriorityQueue();
  
ptraffic.Enqueue(10);
ptraffic.DisplayPriorityQueue();
  
ptraffic.Enqueue(5);
ptraffic.DisplayPriorityQueue();
  
ptraffic.Enqueue(3);
ptraffic.DisplayPriorityQueue();
  
ptraffic.Dequeue();
ptraffic.DisplayPriorityQueue();
  
ptraffic.Enqueue(17);
ptraffic.DisplayPriorityQueue();
  
   }

}

PrioritizedNetworkTraffic.java

package textbook;


public class PrioritizedNetworkTraffic {

   private int[] Q;

   private int head = -1;
   private int tail = 0;
   private int length;
  
   // Precondition: Priority queue Q is defined but not created.
   // Postcondition: Priority queue Q is created, and its size is set to "queueSize".
   // All values of Q are initialized to -1.
   // length is set to "queueSize".
   public PrioritizedNetworkTraffic(int queueSize)
   {
   // Implement me.
   }
  
  
   // Precondition: Priority queue Q is existent.
   // "tail" is already set to the index of Q where a new packet may be inserted.
   // "head" is already set to the index of Q where the packet with the highest priority is stored.
   // Postcondition: A new packet with priority "newElement" is inserted in the priority queue Q.
   // "head" is set to the index of Q where the packet with the highest priority is stored.
   // "tail" is set to the next index of Q that is available for storage of a new element.
   // If Q is full, "tail" is to the index of Q where the packet with the lowest priority is stored.
   public void Enqueue(int newElement)
   {     
       // Implement me.
   }

  
   // Precondition: Priority queue Q is existent.
   // "head" is already set to the index of Q where the packet with the highest priority is stored.
   // If Q is empty, "head" is -1.
   // "tail" is already set to the index of Q where a new packet may be inserted.
   // Postcondition: The packet with index equal to "head" is removed from Q and is returned to the caller.
   // -1 is inserted in its place, namely in the cell of Q where the removed packet was previously stored.
// "head" is set to the index of Q where the packet with the highest priority is stored.
// "tail" is set to the next index of Q that is available for storage of a new element.
// If Q is empty, "head" is set to -1 and "tail" is set to 0.
  
   public int Dequeue()
   {     
       // Implement me.
   }
  
   // Precondition: Priority queue Q is existent.
   // "startIndex" and "endIndex" are indices of Q that define the beginning and the end, respectively, of the subarray of Q that needs to be searched.
   // "startIndex" and "endIndex" may refer to the entire Q.
   // Postcondition: The index of Q, where the packet with the highest priority is stored, is found and is returned to the caller.
   public int LocateNextHead(int startIndex, int endIndex)
   {     
       // Implement me.
   }
  
  
   // Precondition: Priority queue Q is existent.
   // "startIndex" and "endIndex" are indices of Q that define the beginning and the end, respectively, of the subarray of Q that needs to be searched.
   // "startIndex" and "endIndex" may refer to the entire Q.
   // Postcondition: The index of Q, where the packet with the lowest priority is stored, is found and is returned to the caller.
   public int LocateNextTail(int startIndex, int endIndex)
   {     
       // Implement me.
   }
  
  
   // Precondition: Priority queue Q is existent.
   // Postcondition: The index and priority of each packet in Q are displayed on the monitor.
   public void DisplayPriorityQueue()
   {
       // Implement me.
   }
  
}

 


Hello:)

Here is the answer hope you like it :)

class PrioritizedNetworkTraffic
{
private int[] Q;
private int head;
private int length;
  
int min, max=0;
  
int min_index , max_index=0;
  
public PrioritizedNetworkTraffic(int queueSize)
{
this.length = queueSize;
Q = new int[this.length];
head = -1;
}
  
public void Enqueue(int newElement)
{
this.head++;
this.Q[head] = newElement;
}
  
public int Dqueue()
{
int temp_head = Q[head];
head--;
return temp_head;
}
  
public void LocateNextHead(int startIndex, int endIndex)
{
if(startIndex>endIndex)
{
return;
}
if(max<Q[startIndex])
{
max_index = startIndex;
max = Q[startIndex];
LocateNextHead(++startIndex, endIndex);
}
}
  
public void LocateNextTail(int startIndex, int endIndex)
{
if(startIndex>endIndex)
{
return;
}
if(min>Q[startIndex])
{
min = Q[startIndex];
min_index = startIndex;
LocateNextHead(++startIndex, endIndex);
}
  
}
  
public void DisplayPriorityQueue()
{
for(int i=0; i<=head; i++)
{
System.out.println("Index: "+i+" Packet Priority: "+Q[i]);
}
max = 0;
this.LocateNextHead(0, head);
System.out.println("\nCurrent Head: "+max_index);
min = Q[0];
this.LocateNextTail(0, head);
System.out.println("\nCurrent Tail: "+min_index);
}
}


public class IDSWorkerClass
{
public static void main(String[] args)
{
int arraySize = 8;
int Max = 0;
System.out.println("IDS analysis of prioritized network traffic is running...\n");
PrioritizedNetworkTraffic ptraffic = new PrioritizedNetworkTraffic(arraySize);
ptraffic.DisplayPriorityQueue();
  
ptraffic.Enqueue(4);
ptraffic.DisplayPriorityQueue();
  
ptraffic.Enqueue(9);
ptraffic.DisplayPriorityQueue();
  
ptraffic.Enqueue(15);
ptraffic.DisplayPriorityQueue();
  
ptraffic.Enqueue(1);
ptraffic.DisplayPriorityQueue();
  
ptraffic.Enqueue(4);
ptraffic.DisplayPriorityQueue();
  
ptraffic.Enqueue(10);
ptraffic.DisplayPriorityQueue();
  
ptraffic.Enqueue(5);
ptraffic.DisplayPriorityQueue();
  
ptraffic.Enqueue(3);
ptraffic.DisplayPriorityQueue();
  
ptraffic.Dqueue();
ptraffic.DisplayPriorityQueue();
  
ptraffic.Enqueue(17);
ptraffic.DisplayPriorityQueue();
}
}

轉載自趕due論壇(info.gandue.net)