Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

language is: java we use only studio: NetBeans IDE 8.0.2 Book: horstmann big jav

ID: 3769963 • Letter: L

Question

language is: java

we use only studio: NetBeans IDE 8.0.2

Book: horstmann big java late objects

all I need is checking my soluation if it's correct 100%. the following is the question requirments and correct me if there is anything wrong, I posted my codes you can copy them to check

you will be implementing a network of routers. Each router is a node on a graph. Each of these nodes will contain a queue of Packet objects that represent information from other nodes. At random times, a particular node will create a random packet, which consists of an Integer of data and an Integer for the destination and place it into a stack on the node. Whenever the queue for a router is empty, it will pop off a packet from the stack and insert it into it's queue.
Use the following pseudo-code for illustration and design purposes:

1. Ask user for the number of steps to run the simulation for. Minimum is 10, maximum is 500.

2. Initialize a random number generator with the “long” number 123456789098765.

3. Load the network topology from the file network.txt and create the graph for it.

4. Generate a random number of packet objects for each router and place them into the stack (minimum 1, maximum 10)

5. For each step from 1, do the following:

1. for each router on the Internet, do the following

1. For each packet in the waiting area on this router

1. if the delay time on the Packet is zero, add the packet to the queue for this router

2. if the delay time is greater than zero, subtract one from the delay.

2. If the router queue is empty, pop off an item from the local stack and add it to the router's queue.

1. A queue should be considered empty if it has zero or one packet in it's queue.

3. Select the first packet from the queue

1. if the packet destination is the current node, overwrite the value stored on the current node

2. determine the shortest path to the destination from the current node

4. Send the packet to the next node as determined by 2

5. select a random number between 1 and 100

1. if the number is 1-15,

1. decrease the weight on a random number of outgoing edges from the current node.

2. Make certain the new weight is greater than zero

2. If the number is between 16 and 25, increase the weight on a random number of outgoing edges

3. If the number is between 26 and 38, add an item to the current router's stack

4. if one of the above actions are taken, log the router ID, the random number generated, and a statement of the action taken to “results.txt”

6. If the current router has an empty stack, randomly add 1 to 10 items onto the stack.

2. (end for each router)

6. (end for each step)

7. For each router, write a “results.txt” file with the information stored at the router, including:

1. The router's ID

2. the value stored on the router

3. the packet information stored in the queue

4. the packet information stored in the stack

* notic: they asked to use <T> but I don't know how to use it also the following is my codes I created 3 files.

file number 1 is: IntSim.java

package intsim;
/*

* Java Program to Implement the Internet using a network of routers

*/

import java.util.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

/* Class stackUsingQueue */

class stackUsingQueue

{

Queue<Integer> q ;

Queue<Integer> tmp ;

/* Constructor */

public stackUsingQueue()

{

q = new LinkedList<Integer>();

tmp = new LinkedList<Integer>();

}

/* Function to push an element to the stack */

public void push(int data)

{

/* if no element is present in queue q then

* enqueue the new element into q */   

if (q.size() == 0)

q.add(data);

else

{

/* if elements are present in q then

* dequeue all the elements to

* temporary queue tmp */   

int l = q.size();

for (int i = 0; i < l; i++)

tmp.add(q.remove());

/* enqueue the new element into q */

q.add(data);

/* dequeue all the elements from

* temporary queue tmp to q */   

for (int i = 0; i < l; i++)

q.add(tmp.remove());

}

}

/* Function to remove top element from the stack */

public int pop()

{

if (q.size() == 0)

throw new NoSuchElementException("No more packets to send (Underflow Exception)");

return q.remove();

}

/* Function to check the top element of the stack */

public int peek()

{

if (q.size() == 0)

throw new NoSuchElementException("No packets awaiting to be sen to stack(Underflow Exception)");

return q.peek();

}

/* Function to check if stack is empty */

public boolean isEmpty()

{

return q.size() == 0 ;

}

/* Function to get the size of the stack */

public int getSize()

{

return q.size();

}

/* Function to display the status of the stack */

public void display()

{

System.out.print(" Router Stack = ");

int l = getSize();

if (l == 0)

System.out.print("Router Stack is Empty ");

else

{

Iterator it = q.iterator();

while (it.hasNext())

System.out.print(it.next()+" ");

System.out.println();

}

}
  

}

public class IntSim
{
   private static void GeneratePackets() {
       RandomNumberGenerator.main();
   }
private static void LoadNetwork() throws Exception{
       network.main();
   }
  
private static void results(int run){
try {
File results = new File("C:/results.txt");
FileOutputStream is = new FileOutputStream(results);
OutputStreamWriter osw = new OutputStreamWriter(is);
Writer w = new BufferedWriter(osw);
stackUsingQueue suq = new stackUsingQueue();
w.write(" Router ID = "+ suq.getSize());
w.write("Stack Status Info ");

w.write(" Packets = "+ suq.isEmpty());
  
  
w.write(" Queue information");
for (int idx = 1; idx <= run; ++idx){
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(100);

w.write(" Packets = "+ randomInt );
  
}
w.close();
System.out.println("Results.txt file successfully created!");
} catch (IOException e){
System.err.println("Problem writing to the file results.txt");
}
}


public static void main(String[] args) throws Exception

{
  
int runs;
  
  

Scanner scan = new Scanner(System.in);

stackUsingQueue suq = new stackUsingQueue();
  

/* Perform Router Stack Operations */

System.out.println("Implement the Internet ");
  
System.out.println("Enter the number of steps - (between 10 and 500)to run the simulation ");
  
runs = scan.nextInt();
  
  
  
  
  
  
GeneratePackets();
  

char ch;   

do

{

System.out.println(" Implement the Internet: Router Operations");
  
System.out.println("0. Load network topology");

System.out.println("1. Add packets to the stack (Push)");

System.out.println("2. Add items to the queue (Pop from stack and enqueue)");

System.out.println("3. Check current packet to be addes to router stack (Peek)");

System.out.println("4. Check Router Stack if Empty");

System.out.println("5. Check Size of Router Stack");
  
System.out.println("6. Create results.txt file");

int choice = scan.nextInt();

switch (choice)

{
case 0 :
LoadNetwork();

  

break;

case 1 :
for (int idx = 1; idx <= runs; ++idx){

System.out.println("Pushing packets onto Router stack");
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(100);

suq.push(randomInt ); }

break;

case 2 :

try

{
if (suq.isEmpty()){
System.out.println("Router stack status = "+ suq.isEmpty() );}
else {
for (int idx = 1; idx <= runs; ++idx){

System.out.println("Popped Packet sent to Queue = "+ suq.pop() );
  
}
}

}

catch (Exception e)

{

System.out.println("Error : " + e.getMessage());

}

break;   

case 3 :

try

{

System.out.println("Next Packet on queue = "+ suq.peek() );

}

catch (Exception e)

{

System.out.println("Error : " + e.getMessage());

}

break;   

case 4 :

System.out.println("Router stack status = "+ suq.isEmpty() );

break;

case 5 :

System.out.println("Size of Router Stack = "+ suq.getSize() );

break;


  
case 6 :

IntSim write = new IntSim();
write.results(runs);

break;

default :

System.out.println("Wrong Entry ");

break;

}

/* Display Router Stack */

suq.display();
  

System.out.println(" Perform another router operation (Type y or n) ");

ch = scan.next().charAt(0);

} while (ch == 'Y'|| ch == 'y');   

}

}

file number 2 is: RandomNumberGenerator.java

package intsim;
import java.util.Random;

/** Generate 1 - 10 Packets */
public final class RandomNumberGenerator {
  
public static final void main(String... aArgs){
log("Generating a random number or packets between 1 and 10");
  
//note a single Random object is reused here
Random randomGenerator = new Random();
for (int idx = 1; idx <= 1; ++idx){
int randomInt = randomGenerator.nextInt(11);
  
if (randomInt != 0)
log("Generated : " + randomInt);
else
--idx;
}
  
log("Done.");
}
  
  
private static void log(String aMessage){
System.out.println(aMessage);
}
}

file number 3 is: network.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package intsim;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

/**
*
* @author
*/
public class network {
/*Code for loading Network Topology*/
public static void main(String... aArgs) throws Exception

{
File file = new File("C:/network.txt");
StringBuffer contents = new StringBuffer();
BufferedReader reader = null;
  
reader = new BufferedReader(new FileReader(file));
String text = null;
  
//repeat until all lines is read
while((text = reader.readLine()) != null) {
contents.append(text).append(System.getProperty("line.separator"));
}
reader.close();
System.out.println(contents.toString());
System.out.println("Network.txt loaded successfully, graph created");
}
  
}

Explanation / Answer

Hi, This is looking fine.