Question: import java.util.Random; import java.util.Scanner; class HammingCode { static void print ( int ar [ ] ) { for ( int i = 1 ; i

import java.util.Random;
import java.util.Scanner;
class HammingCode {
static void print(int ar[]){
for (int i =1; i < ar.length; i++){
System.out.print(ar[i]);
}
System.out.println();
}
static int[] calculation(int[] ar, int r){
for (int i =0; i < r; i++){
int x =(int) Math.pow(2, i);
for (int j =1; j < ar.length; j++){
if (((j >> i) & 1)==1){
if (x != j)
ar[x]= ar[x]^ ar[j];
}
}
System.out.println("p"+ x +"="+ ar[x]);
}
return ar;
}
static int[] generateCode(String str, int M, int r){
int[] ar = new int[r + M +1];
int j =0;
for (int i =1; i < ar.length; i++){
if ((Math.ceil(Math.log(i)/ Math.log(2)))==(Math.floor(Math.log(i)/ Math.log(2)))){
ar[i]=0;
} else {
ar[i]=(int)(str.charAt(j)-'0');
j++;
}
}
return ar;
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
// Input message from the user
System.out.print("Enter Data: ");
String str = scanner.nextLine();
int M = str.length();
int k =1;
//2^K-1>= M + K
while ((-1+ Math.pow(2, k))<(M + k)){
k++;
}
int[] ar = generateCode(str, M, k);
System.out.println("Generated Hamming code ");
print(ar);
// Simulate transmission with random errors
Random random = new Random();
for (int i =1; i < ar.length; i++){
if (random.nextDouble()<0.1){// Simulating 10% error rate (adjust as needed)
ar[i]=1- ar[i]; // Flip the bit
}
}
System.out.println("Received Hamming code with errors ");
print(ar);
ar = calculation(ar, k);
// Calculate error percentage
int errorCount =0;
for (int i =1; i < ar.length; i++){
if (ar[i]!=0){
errorCount++;
}
}
double errorPercentage =(double) errorCount /(ar.length -1)*100;
System.out.println("Percentage of errors at the receiver: "+ errorPercentage +"%");
scanner.close();
}
}....in this code i want to enter the number of errors from the user thenprint the percentage error at the reciever

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Heres the modified code import javautilRandom import javautilScanner class HammingCode static void p... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!