Write a program in java code to implement the checksum method. The implementatio
ID: 3572643 • Letter: W
Question
Write a program in java code to implement the checksum method.
The implementation has two parts:
Calculating the checksum at the sender
Verifying the checksum at the receiver
Calculating the checksum at the sender
Inputs: 4 data each consisting of 16-bit (binary)
Output: Checksum of 16-bit (binary)
For example, suppose we have the following data. I separate the
data into groups of 4 bits only for readability.
1000 0110 0101 1110
1010 1100 0110 0000
0111 0001 0010 1010
1000 0001 1011 0101
Verifying the checksum at the receiver
Inputs: 4 data each is consisting of 16-bit and their 16-bit checksum
Output: Checksum for 4 data each is consisting of 16-bit and their 16-bit
checksum
If checksum is equal to 0s, your data is not corrupted
If checksum is not equal 0s, your data is corrupted
Explanation / Answer
import java.util.*;
public class CheckSumMethod
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
String list[] = new String [4];
int check[]=new int[4];
int ll=0;
//sender
while(ll<4)
{
int k =ll+1;
System.out.println("Enter the string input "+k+" :");
String input = scan.next();
list[ll]=input;
int checksum = generateChecksum(input);
check[ll]=checksum;
ll++;
}
ll=0;
//printing output
while(ll<4)
{
// Call the method to create the checksum
int k=ll+1;
System.out.print("The checksum generated for "+k+" = ");
String s = Integer.toBinaryString(check[ll]);
int n =s.length();
while(n<16)
{
System.out.print("0");
n++;
}
System.out.println(s);
ll++;
}
//receiver
ll=0;
while(ll<4)
{
String input;
int f=ll+1;
System.out.println("Enter the data to be sent "+f+" :");
input = scan.next();
list[ll]=input;
System.out.println("Enter the checksum to be sent:");
String r = scan.next();
System.out.println(r);
// checksum = Integer.parseInt(r);
int checksum=0;
//System.out.println(checksum);
int l; l = r.length();
int k=0;
checksum =0;
l--;
System.out.println(checksum);
while(l>=0)
{
if(r.charAt(l)=='1')
checksum=checksum+(int)Math.pow(2,k);
l--;
k++;
}
receive(input, checksum);
ll++;
}
}
static int generateChecksum(String s)
{
String hex_value = new String();
// 'hex_value' will be used to store various hex values as a string
int x, i, checksum = 0;
// 'x' will be used for general purpose storage of integer values
// 'i' is used for loops
// 'checksum' will store the final checksum
for (i = 0; i < s.length() - 2; i = i + 2)
{
x = (int) (s.charAt(i));
hex_value = Integer.toHexString(x);
x = (int) (s.charAt(i + 1));
hex_value = hex_value + Integer.toHexString(x);
// Extract two characters and get their hexadecimal ASCII values
// System.out.println(s.charAt(i) + "" + s.charAt(i + 1) + " : "
// + hex_value);
x = Integer.parseInt(hex_value, 16);
// Convert the hex_value into int and store it
checksum += x;
// Add 'x' into 'checksum'
}
if (s.length() % 2 == 0)
{
// If number of characters is even, then repeat above loop's steps
// one more time.
x = (int) (s.charAt(i));
hex_value = Integer.toHexString(x);
x = (int) (s.charAt(i + 1));
hex_value = hex_value + Integer.toHexString(x);
//System.out.println(s.charAt(i) + "" + s.charAt(i + 1) + " : "
// + hex_value);
x = Integer.parseInt(hex_value, 16);
}
else
{
// If number of characters is odd, last 2 digits will be 00.
x = (int) (s.charAt(i));
hex_value = "00" + Integer.toHexString(x);
x = Integer.parseInt(hex_value, 16);
//System.out.println(s.charAt(i) + " : " + hex_value);
}
checksum += x;
// Add the generated value of 'x' from the if-else case into 'checksum'
// System.out.println("Intermediate:"+checksum);
hex_value = Integer.toHexString(checksum);
// Convert into hexadecimal string
if (hex_value.length() > 4)
{
// If a carry is generated, then we wrap the carry
int carry = Integer.parseInt(("" + hex_value.charAt(0)), 16);
// Get the value of the carry bit
hex_value = hex_value.substring(1, 5);
// Remove it from the string
checksum = Integer.parseInt(hex_value, 16);
// Convert it into an int
checksum += carry;
// Add it to the checksum
}
checksum = generateComplement(checksum);
// Get the complement
return checksum;
}
static void receive(String s, int checksum)
{
int generated_checksum = generateChecksum(s);
// Calculate checksum of received data
generated_checksum = generateComplement(generated_checksum);
// Then get its complement, since generated checksum is complemented
int syndrome = generated_checksum + checksum;
// Syndrome is addition of the 2 checksums
syndrome = generateComplement(syndrome);
// It is complemented
System.out.println("checksum = " + Integer.toHexString(syndrome));
if (syndrome == 0)
{
System.out.println("Data is received without error.");
}
else
{
System.out.println("There is an error in the received data.");
}
}
static int generateComplement(int checksum)
{
// Generates 15's complement of a hexadecimal value
checksum = Integer.parseInt("FFFF", 16) - checksum;
return checksum;
}
}
ouput:-
run:
Enter the string input 1 :
1000011001011110
Enter the string input 2 :
1010110001100000
Enter the string input 3 :
0111000100101010
Enter the string input 4 :
1000000110110101
The checksum generated for 1 = 0111101001111010
The checksum generated for 2 = 0111101001111100
The checksum generated for 3 = 0111101001111011
The checksum generated for 4 = 0111101101111010
Enter the data to be sent 1 :
1000011001011110
Enter the checksum to be sent:
0111101001111010
0111101001111010
0
checksum = 0
Data is received without error.
Enter the data to be sent 2 :
1010110001100001
Enter the checksum to be sent:
0111101001111100
0111101001111100
0
checksum = ffffffff
There is an error in the received data.
Enter the data to be sent 3 :
0111000100101010
Enter the checksum to be sent:
0111101001111011
0111101001111011
0
checksum = 0
Data is received without error.
Enter the data to be sent 4 :
1000000110110101
Enter the checksum to be sent:
0111101101111011
0111101101111011
0
checksum = ffffffff
There is an error in the received data.
BUILD SUCCESSFUL (total time: 1 minute 31 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.