CN Codes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

HAMMING CODE:

package javaTpoint.JavaExample;

import java.util.*;

class HammingCodeExample {

public static void main(String args[])

int size, hammingCodeSize, errorPosition;

int arr[];

int hammingCode[];

Scanner sc = new Scanner(System.in);

System.out.println("Enter the bits size for the data.");

size = sc.nextInt();

arr = new int[size];

for(int j = 0 ; j < size ; j++) {

System.out.println("Enter " + (size - j) + "-bit of the data:");

arr[size - j - 1] = sc.nextInt();

System.out.println("The data which you enter is:");

for(int k = 0 ; k < size ; k++) {

System.out.print(arr[size - k - 1]);

System.out.println();

hammingCode = getHammingCode(arr);

hammingCodeSize = hammingCode.length;

System.out.println("The hamming code generated for your data is:");

for(int i = 0 ; i < hammingCodeSize; i++) {

System.out.print(hammingCode[(hammingCodeSize - i - 1)]);

System.out.println();

System.out.println("For detecting error at the reciever end, enter position of a bit to alter original da
ta " + "(0 for no error):");
errorPosition = sc.nextInt();

sc.close();

if(errorPosition != 0) {

hammingCode[errorPosition - 1] = (hammingCode[errorPosition - 1] + 1) % 2;

System.out.println("Sent Data is:");

for(int k = 0; k < hammingCodeSize; k++) {

System.out.print(hammingCode[hammingCodeSize - k - 1]);

System.out.println();

receiveData(hammingCode, hammingCodeSize - arr.length);

static int[] getHammingCode(int data[]) {

int returnData[];

int size;

int i = 0, parityBits = 0 ,j = 0, k = 0;

size = data.length;

while(i < size) {

if(Math.pow(2, parityBits) == (i + parityBits + 1)) {

parityBits++;

else {

i++;

returnData = new int[size + parityBits];

for(i = 1; i <= returnData.length; i++) {

if(Math.pow(2, j) == i) {

returnData[(i - 1)] = 2;

j++;

}
else {

returnData[(k + j)] = data[k++];

for(i = 0; i < parityBits; i++) {

returnData[((int) Math.pow(2, i)) - 1] = getParityBit(returnData, i);

return returnData;

static int getParityBit(int returnData[], int pow) {

int parityBit = 0;

int size = returnData.length;

for(int i = 0; i < size; i++) {

if(returnData[i] != 2) {

int k = (i + 1);

String str = Integer.toBinaryString(k);

int temp = ((Integer.parseInt(str)) / ((int) Math.pow(10, pow))) % 10;

if(temp == 1) {

if(returnData[i] == 1) {

parityBit = (parityBit + 1) % 2;

return parityBit;

static void receiveData(int data[], int parityBits) {

int pow;

int size = data.length;

int parityArray[] = new int[parityBits];

String errorLoc = new String();


for(pow = 0; pow < parityBits; pow++) {

for(int i = 0; i < size; i++) {

int j = i + 1;

String str = Integer.toBinaryString(j);

int bit = ((Integer.parseInt(str)) / ((int) Math.pow(10, pow))) % 10;

if(bit == 1) {

if(data[i] == 1) {

parityArray[pow] = (parityArray[pow] + 1) % 2;

errorLoc = parityArray[pow] + errorLoc; }

int finalLoc = Integer.parseInt(errorLoc, 2);

if(finalLoc != 0) {

System.out.println("Error is found at location " + finalLoc + ".");

data[finalLoc - 1] = (data[finalLoc - 1] + 1) % 2;

System.out.println("After correcting the error, the code is:");

for(int i = 0; i < size; i++) {

System.out.print(data[size - i - 1]);

System.out.println();

else {

System.out.println("There is no error in the received data.");

System.out.println("The data sent from the sender:");

pow = parityBits - 1;

for(int k = size; k > 0; k--) {

if(Math.pow(2, pow) != k) {

System.out.print(data[k - 1]);

}
else {

pow--;

System.out.println();

}
LEAKEY BUCKET:

import java.util.Scanner;

public class LeakyBucket {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the bucket size: ");

int bucketSize = sc.nextInt();

System.out.println("Enter the output rate: ");

int outputRate = sc.nextInt();

System.out.println("Enter the number of incoming packets: ");

int n = sc.nextInt();

int[] packets = new int[n];

System.out.println("Enter the size of each packet: ");

for (int i = 0; i < n; i++) {

packets[i] = sc.nextInt();

int bucketContent = 0; // The current size of the bucket

for (int i = 0; i < n; i++) {

System.out.println("\nProcessing packet " + (i + 1) + " with size " + packets[i]);

if (packets[i] <= (bucketSize - bucketContent)) {

// If the packet can fit into the bucket

bucketContent += packets[i];

System.out.println("Packet accepted. Bucket content: " + bucketContent);

} else {

System.out.println("Packet dropped. Not enough space in the bucket.");

if (bucketContent >= outputRate) {

bucketContent -= outputRate;

} else {

bucketContent = 0;
}

System.out.println("After transmission, bucket content: " + bucketContent);

while (bucketContent > 0) {

System.out.println("\nTransmitting remaining data...");

if (bucketContent >= outputRate) {

bucketContent -= outputRate;

} else {

bucketContent = 0;

System.out.println("After transmission, bucket content: " + bucketContent);

sc.close();

}
CRC:

import java.util.*;

class CRCExample {

public static void main(String args[]) {

Scanner scan = new Scanner(System.in);

int size;

System.out.println("Enter the size of the data array: ");

size = scan.nextInt();

int data[] = new int[size];

System.out.println("Enter data bits in the array one by one: ");

for(int i = 0 ; i < size ; i++) {

System.out.println("Enter bit " + (size-i) + ":");

data[i] = scan.nextInt();

System.out.println("Enter the size of the divisor array:");

size = scan.nextInt();

int divisor[] = new int[size];

System.out.println("Enter divisor bits in the array one by one: ");

for(int i = 0 ; i < size ; i++) {

System.out.println("Enter bit " + (size-i) + ":");

divisor[i] = scan.nextInt();

int rem[] = divideDataWithDivisor(data, divisor);

for(int i = 0; i < rem.length-1; i++) {

System.out.print(rem[i]);

System.out.println("\nGenerated CRC code is: ");

for(int i = 0; i < data.length; i++) {

System.out.print(data[i]);

}
for(int i = 0; i < rem.length-1; i++) {

System.out.print(rem[i]);

System.out.println();

int sentData[] = new int[data.length + rem.length - 1];

System.out.println("Enter bits in the array which you want to send: ");

for(int i = 0; i < sentData.length; i++) {

System.out.println("Enter bit " +(sentData.length - 1)+ ":");

sentData[i] = scan.nextInt();

receiveData(sentData, divisor);

static int[] divideDataWithDivisor(int oldData[], int divisor[]) {

// declare rem[] array

int rem[] = new int[divisor.length];

int i;

int data[] = new int[oldData.length + divisor.length];

System.arraycopy(oldData, 0, data, 0, oldData.length);

System.arraycopy(data, 0, rem, 0, divisor.length);

for(i = 0; i < oldData.length; i++) {

System.out.println((i+1) + ".) First data bit is : "+ rem[0]);

System.out.print("Remainder : ");

if(rem[0] == 1) {

// We have to exor the remainder bits with divisor bits

for(int j = 1; j < divisor.length; j++) {

rem[j-1] = exorOperation(rem[j], divisor[j]);

System.out.print(rem[j-1]);

else {

for(int j = 1; j < divisor.length; j++) {


rem[j-1] = exorOperation(rem[j], 0);

System.out.print(rem[j-1]);

rem[divisor.length-1] = data[i+divisor.length];

System.out.println(rem[divisor.length-1]);

return rem;

static int exorOperation(int x, int y) {

// This simple function returns the exor of two bits

if(x == y) {

return 0;

return 1;

static void receiveData(int data[], int divisor[]) {

int rem[] = divideDataWithDivisor(data, divisor);

for(int i = 0; i < rem.length; i++) {

if(rem[i] != 0) {

System.out.println("Currupted data received...");

return;

System.out.println("Data received without any error.");

}
SOCKET PROGRAMMING:
SHORTEST-PATH:

import java.util.*;

import java.io.*;

import java.lang.*;

public class DijkstraExample

static final int totalVertex = 9;

int minimumDistance(int distance[], Boolean spSet[])

int m = Integer.MAX_VALUE, m_index = -1;

for (int vx = 0; vx < totalVertex; vx++) {

if (spSet[vx] == false && distance[vx] <= m) {

m = distance[vx];

m_index = vx;

} }

return m_index;

void printSolution(int distance[], int n)

System.out.println("The shortest Distance from source 0th node to all other nodes are: ");

for (int j = 0; j < n; j++)

System.out.println("To " + j + " the shortest distance is: " + distance[j]);

void dijkstra(int graph[][], int s)

int distance[] = new int[totalVertex];

Boolean spSet[] = new Boolean[totalVertex];

for (int j = 0; j < totalVertex; j++)

distance[j] = Integer.MAX_VALUE;

spSet[j] = false; }
distance[s] = 0;

for (int cnt = 0; cnt < totalVertex - 1; cnt++)

{ int ux = minimumDistance(distance, spSet);

spSet[ux] = true;

for (int vx = 0; vx < totalVertex; vx++)

if (!spSet[vx] && graph[ux][vx] != - && distance[ux] != Integer.MAX_VALUE && distance[ux] + gra


ph[ux][vx] < distance[vx]){

distance[vx] = distance[ux] + graph[ux][vx];

} }

printSolution(distance, totalVertex);

} public static void main(String argvs[]) {

int grph[][] = new int[][] { { -1, 3, -1, -1, -1, -1, -1, 7, -1 },

{ 3, -1, 7, -1, -1, -1, -1, 10, 4 },

{ -1, 7, -1, 6, -1, 2, -1, -1, 1 },

{ -1, -1, 6, -1, 8, 13, -1, -1, 3 },

{ -1, -1, -1, 8, -1, 9, -1, -1, -1 },

{ -1, -1, 2, 13, 9, -1, 4, -1, 5 },

{ -1, -1, -1, -1, -1, 4, -1, 2, 5 },

{ 7, 10, -1, -1, -1, -1, 2, -1, 6 },

{ -1, 4, 1, 3, -1, 5, 5, 6, -1 } };

DijkstraExample obj = new DijkstraExample();

obj.dijkstra(grph, 0);

} }

You might also like