Practical JAVA

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

//CRC

import java.util.Scanner;
public class CRC{
static String data = "";
static String checkValue = "";
static String genPoly = "";
static int dataLength, i,j;
static int N;
public static void XOR() {
StringBuilder result = new StringBuilder();
for (j = 1; j < N; j++) {
result.append((checkValue.charAt(j) ==
genPoly.charAt(j)) ? '0' : '1');
}
checkValue = result.toString();
}
public static void receiver() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the received data: ");
String receivedData = scanner.next();
data=receivedData;
System.out.println("\n----------------------");
System.out.println("Data received: " +
receivedData);
crc();
for (i=0; (i<N-1) && (checkValue.charAt(i) != '
1'); i++) {
}
if (i <N-1) {
System.out.println("\nError detected\n"
);
} else {
System.out.println("\nNo error detected
\n");
}
scanner.close();
}
public static void crc() {
checkValue = data;
N = genPoly.length();
do {
if (checkValue.charAt(0) == '1') {
XOR();
}
checkValue = checkValue.substring(1) + (i
< data.length() ? data.charAt(i) : '0');
i++;
} while (i <= dataLength + N - 1);
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter data to be
transmitted: ");
String inputData = scanner.next();
data=inputData;
System.out.print("\nEnter the Generating
polynomial: ");
String genPolyInput = scanner.next();
genPoly = genPolyInput;
dataLength=inputData.length();
for (i = dataLength; i < dataLength +
genPoly.length() - 1; i++) {
data += '0';
}
System.out.println("\n
--------------------------------------");
System.out.println("Data padded with n-1
zeros: " + data);
System.out.println(
"----------------------------------------");
crc();
System.out.println("\nCRC or Check value is
: " + checkValue);
for (i = dataLength; i < dataLength +
genPoly.length() - 1; i++) {
data = data.substring(0, i) +
checkValue.charAt(i - dataLength) + data.substring(i +
1);
}
System.out.println("\n
-----------------------");
System.out.println("Final data to be sent:"
+ data);
System.out.println(
"--------------------------");
receiver();
}
}
//GoBack-N
import java.util.*;

public class Goback{


static Scanner SC=new Scanner(System.in);
public static void main(String args[]){
System.out.println("Enter Size of the Window(n
):-");
int n=SC.nextInt();
System.out.println("Enter total number of
frames:-");
int total = SC.nextInt();
int base = 1;
int nextSeqNum = 1;
while(base<=total){
transmitN(base,Math.min(base+n-1,total));
Random var = new Random();
int r = var.nextInt(2);
if(r==0) {
System.out.println("Acknowledgement for
frames " + base + " to " + nextSeqNum + " is Received"
);
base = nextSeqNum + 1;
nextSeqNum = base;
}else{
System.out.println("Acknowledgement for
frames " + base + " to " + nextSeqNum + " is not
Received");
System.out.println("Revisiting");
nextSeqNum = base;
}
System.out.println();
}
}
public static void transmitN(int start, int end){
System.out.print("Transmitting Frames..");
for(int i=start;i<=end;i++)
System.out.print(i+"");
System.out.println();
}
}
//Hamming Code
import java.util.*;

public class HammingCode{


public static void printArray(int[] a){
for(int num : a){
System.out.print(num + " ");
}
System.out.println();
}

public static int log2(int a){


return (int) (Math.log(a) / Math.log(2));
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of bits for
Hamming Data: ");
int n = scanner.nextInt();
int [] b = new int[n];
System.out.print("Enter the string: ");
for(int i=0;i<n;i++){
b[i]=scanner.nextInt();
}
System.out.print("Entered Data: ");
printArray(b);
int r=0;
while(true){
if(Math.pow(2,r)>=r+n+1){
break;
}
r++;
}
System.out.println("Redundant bits: "+r);

int[] a = new int[r+n+1];


int index =0;

for(int i=r+n;i>0;i--){
int l = log2(i);
if(Math.pow(2,l) != i){
a[r+n-i] = b[index++];
}else{
a[r+n-i]=-1;
}
}

for(int i =1; i<=r+n;i++){


int l = log2(i);
if(Math.pow(2,l)==i){
int num = i;
List<Integer>list = new ArrayList<>();
while (true){
for(int j=0;j<i;j++){
if(num >r+n) break;
list.add(num);
num++;
}
num += i;
if(num>r+n) break;
}
int count = 0;
for(int j=1;j<list.size();j++){
if(a[r+n-list.get(j)]==1) count++;
}
if(count % 2 == 0){
a[r+n-i] = 0;
}else{
a[r+n-i] = 1;
}
}
}

System.out.println("Generated Code: ");


printArray(a);

System.out.print("Enter position to check for


error detection: ");
int pos = scanner.nextInt();
index = r+n-pos;
a[index] = 1 -a[index];

String bit = "";


for (int i = 1; i <= r + n; i++) {
int l = log2(i);
if (Math.pow(2, l) == i) {
int num = i;
ArrayList<Integer> list = new ArrayList
<>();
while (true) {
for (int j = 0; j < i; j++) {
if (num > r + n)
break;
list.add(num);
num++;
}
num += i;
if (num > r + n)
break;
}
int count = 0;
for (int j = 0; j < list.size(); j++) {
if (a[r + n - list.get(j)] == 1)
count++;
}
if (count % 2 == 0) {
bit = "0" + bit;
} else {
bit = "1" + bit;
}
}
}
System.out.print("Sent Code: ");
printArray(a);

int num = Integer.parseInt(bit,2);


System.out.println("Error at position " +num);
a[index]= 1-a[index];

System.out.print("Corrected code: ");


printArray(a);
scanner.close();
}
}
//Socket Program Client side (1/2)
import java.net.*;
import java.io.*;
class ClientSocketDemo{
public static void main(String args[])throws
Exception{
Socket s=new Socket("localhost",3333);
DataInputStream din=new DataInputStream(s.
getInputStream());
DataOutputStream dout=new DataOutputStream(s.
getOutputStream());
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));

String str="",str2="";
while(!str.equals("stop")){
System.out.println("Enter Client side
message:");
str=br.readLine();
dout.writeUTF(str);
dout.flush();
str2=din.readUTF();
System.out.println("Server says: "+str2);
}

dout.close();
s.close();
}
}
//Socket Program Server side (2/2)
import java.net.*;
import java.io.*;
class ServerSocketDemo{
public static void main(String args[])throws
Exception{
ServerSocket ss=new ServerSocket(3333);
Socket s=ss.accept();
DataInputStream din=new DataInputStream(s.
getInputStream());
DataOutputStream dout=new DataOutputStream(s.
getOutputStream());
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));

String str="",str2="";
while(!str.equals("stop")){
System.out.println("Enter Server side
message:");
str=din.readUTF();
System.out.println("client says: "+str);
str2=br.readLine();
dout.writeUTF(str2);
dout.flush();
}
din.close();
s.close();
ss.close();
}
}
//IPv4 Subnet calculations
import java.util.Scanner;

public class IPv4SubnetCalculator{


public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter IPv4 address(e.g.,192.
168.1.10):");
String ipAddress = scanner.nextLine();
System.out.print("Enter Subnet Mask in CIDR
notation(e.g.,24):");
int subnetMask = scanner.nextInt();
String[] ipParts = ipAddress.split("\\.");
long ipValue = 0;
for(int i = 0;i < 4;i++){
ipValue += Long.parseLong(ipParts[i])<<(24
- 8 * i);
}
long subnetMaskValue = (0xFFFFFFFFL<<(32-
subnetMask)) & 0xFFFFFFFFL;
long networkAddress = ipValue & subnetMaskValue
;
long broadcastAddress = networkAddress | (~
subnetMaskValue & 0xFFFFFFFFL);
long availableRangeStart = networkAddress + 1;
long availableRangeEnd = broadcastAddress - 1;

System.out.println("Network Address: "+


formatIPv4(networkAddress));
System.out.println("Broadcast Address: "+
formatIPv4(broadcastAddress));
System.out.println("Available IP Range: "+
formatIPv4(availableRangeStart)+" to "+formatIPv4(
availableRangeEnd));
scanner.close();
}
public static String formatIPv4(long ipValue){
return String.format("%d.%d.%d.%d",
(ipValue>>24) & 0xFF,
(ipValue>>16) & 0xFF,
(ipValue>>8) & 0xFF,
ipValue & 0xFF);
}
}

You might also like