Skip to content

Commit d77e2db

Browse files
authored
Merge pull request #1 from anuj-das-10/main
updated
2 parents d8ae93e + 409ddaf commit d77e2db

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

Solutions/Q-01/CRC_ErrorDetectionAlgorithm.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Contributed by - Anuj Das ( GC University, Silchar - @ Department of Computer Science )
22

3-
// 1. Simulate Cyclic Redundancy Check (CRC) Error Detection Algorithm for Noisy Channel.
3+
// 1. Simulate and implement Cyclic Redundancy Check (CRC) Error Detection Algorithm for Noisy Channel.
44

55
import java.util.Scanner;
66

@@ -10,7 +10,7 @@ public static String xorOperation(String modified_message, String polynomial) {
1010
int size_in_bits = 16;
1111
int shift = modified_message.length() - polynomial.length();
1212
while(shift >= 0) {
13-
modified_message = Integer.toBinaryString(Integer.parseInt(modified_message,2)^(Integer.parseInt(polynomial,2)<<shift));
13+
modified_message = Integer.toBinaryString(Integer.parseInt(modified_message,2)^Integer.parseInt(polynomial,2)<<shift);
1414
shift = modified_message.length() - polynomial.length();
1515
}
1616

@@ -25,8 +25,8 @@ public static String xorOperation(String modified_message, String polynomial) {
2525

2626
// It generates the encoded data/message to be transmitted (i.e., by adding CRC Check Bits to the Original Message)
2727
public static String generateCRC_CheckBits(String modified_message, String polynomial) {
28-
String remainder = xorOperation(modified_message,polynomial);
29-
remainder = remainder.substring(remainder.length() - modified_message.length());
28+
String remainder = xorOperation(modified_message,polynomial); // 16bits remainder
29+
remainder = remainder.substring(remainder.length() - modified_message.length()); // substring(int startIndex)
3030

3131
int CRC_CheckBits[] = new int[modified_message.length()];
3232
for(int i = 0; i < modified_message.length(); i++) {
@@ -49,34 +49,46 @@ public static String modifyMessage(String message, String polynomial) {
4949
return message;
5050
}
5151

52+
// Passing Through Noisy Channel
53+
public static String noisyChannel(String msgToBeTransmitted) {
54+
StringBuilder noise = new StringBuilder(msgToBeTransmitted);
55+
noise.setCharAt(msgToBeTransmitted.length()/2 , '1');
56+
String noisyData = noise.toString();
57+
return noisyData;
58+
}
59+
5260

5361
public static void main(String[] args) {
5462
Scanner sc = new Scanner(System.in);
5563
System.out.print("Enter the Data(Message) to be Encrypted: "); // Original Message
56-
String message = sc.next(); // Test --> 1010000
64+
String message = sc.next(); // Test --> 1010000 , 100100111
5765

5866
System.out.print("Enter the CRC Generator(Polynomial): "); // CRC Generator
5967
String polynomial = sc.next(); // Test --> 1001
6068

69+
// Sender's end
6170
String modified_message = modifyMessage(message,polynomial);
6271
System.out.println("Modified Data(Message): " + modified_message);
6372

6473
String msgToBeTransmitted = generateCRC_CheckBits(modified_message, polynomial);
6574
System.out.println("\nData(Message) is ready to be Transmitted: " + msgToBeTransmitted);
6675

67-
System.out.print("Enter the Data(Message) to be Transmitted: ");
68-
String send_data = sc.next();
76+
// Noisy Transmission Channel
77+
String send_data = noisyChannel(msgToBeTransmitted);
78+
System.out.println("Data(Message) Transmitted through Noisy Channel: "+send_data);
79+
6980

81+
// Receiver's end
7082
String checkAllZeroes = xorOperation(send_data,polynomial);
7183

7284
// It checks if the remainder contains only zeroes --> If it contains only zeros then the data/message is accepted else considered as error in Transmission
7385
for(int i = 0; i < checkAllZeroes.length(); i++) {
7486
if(checkAllZeroes.charAt(i) == '1') {
75-
System.out.println("Error in Transmission!");
87+
System.out.println("\nError in Transmission!\n");
7688
return;
7789
}
7890
}
7991

80-
System.out.println("No! Error in Transmission!");
92+
System.out.println("\nData(Message) Transmitted Successfully!\n");
8193
}
8294
}

Solutions/Q-06/GetLocalHostAddress.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
class GetLocalHostAddress {
88
public static void main(String[] args) throws UnknownHostException {
99
InetAddress localhost = InetAddress.getLocalHost();
10-
System.out.println(localhost.getHostAddress());
10+
System.out.println("LocalHost Address: "+localhost.getHostAddress());
1111
}
1212
}

Solutions/Q-07/GetIPAddress.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
class GetIPAddress {
1010
public static void main(String[] args) throws UnknownHostException{
1111
Scanner sc = new Scanner(System.in);
12-
System.out.print("Enter a valid URL of Website: ");
12+
System.out.print("Enter a valid URL(including https://) of Website: ");
1313
String web_url = sc.next(); // https://www.youtube.com/
14+
15+
// if(web_url.charAt(0) == 'H' || web_url.charAt(0) == 'h') { }
16+
// else { web_url = "https://" + web_url; }
17+
1418
try {
1519
InetAddress address = InetAddress.getByName(new URL(web_url).getHost());
1620
String ip = address.getHostAddress();

0 commit comments

Comments
 (0)