-
Notifications
You must be signed in to change notification settings - Fork 2
/
ParityChunk.java
236 lines (228 loc) · 7.27 KB
/
ParityChunk.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
* 2. Implement the parity check code with chunk interleaving and show that it can correct a set of four 16-bit packets suffering from a 4-bit burst error.
* Created by
* Peerachai Banyongrakkul Sec.1 5988070
* Boonyada Lojanarungsiri Sec.1 5988153
* ParityChunk.java
*/
import java.util.*;
public class ParityChunk
{
public static void main(String[] args)
{
System.out.println("----------Parity check code with chunk interleaving----------");
Scanner sc = new Scanner(System.in);
String code = "";
int count = 0;
String[] packet = new String[4];
while(count < packet.length)
{
System.out.print("input "+ (count+1) +" 16-bit packet: ");
packet[count] = sc.nextLine();
if(packet[count].length() != 16)
{
System.out.println("Wrong Input!!");
System.exit(0);
}
count++;
}
/*PACKET CREATION AT SENDER*/
String[][] chunk = new String[4][5];
char[][] rBit = new char[4][4];
for(int i=0;i<4;i++)
{
for(int j=0;j<5;j++)
{
int bit=0;
if(j == 0)
{
bit=0;
chunk[i][j] = packet[i].substring(0,4);
for(int m=0;m<4;m++)
{
rBit[bit][j] = chunk[i][j].charAt(m);
bit++;
}
}
else if( j == 1)
{
bit=0;
chunk[i][j] = packet[i].substring(4,8);
for(int m=0;m<4;m++)
{
rBit[bit][j] = chunk[i][j].charAt(m);
bit++;
}
}
else if( j == 2 )
{
bit=0;
chunk[i][j] = packet[i].substring(8,12);
for(int m=0;m<4;m++)
{
rBit[bit][j] = chunk[i][j].charAt(m);
bit++;
}
}
else if ( j == 3)
{
bit=0;
chunk[i][j] = packet[i].substring(12, 16);
for(int m=0;m<4;m++)
{
rBit[bit][j] = chunk[i][j].charAt(m);
bit++;
}
bit++;
}
else
{
chunk[i][j] = "";
String temp = "";
int c = 0;
while(c<4)
{
char rbit = rBit[c][0];
for(int l = 1 ; l<4 ; l++)
{
if(rbit == rBit[c][l])
{
rbit = '0';
}
else
{
rbit = '1';
}
}
temp += rbit;
c++;
}
chunk[i][j] = temp;
temp = "";
for(int a =0; a<4;a++)
{
for(int b=0;b<4;b++)
{
rBit[a][b]=0;
}
}
}
}
}
System.out.println();
System.out.println("PACKET CREATION AT SENDER");
System.out.println("\t|chunk 1|chunk 2|chunk 3|chunk 4|r-bit|");
for(int i = 0; i<4 ; i++)
{
System.out.print("Packet "+(i+1)+" | ");
for(int j = 0 ; j<5 ; j++)
{
System.out.print(chunk[i][j]+" | ");
}
System.out.println();
}
/*END PACKET CREATION AT SENDER*/
/*PACKETS SENT*/
System.out.println();
System.out.println("PACKETS SENT");
for(int i = 0; i< 5 ; i++)
{
System.out.print("\t Pactket "+(5-i)+"\t\t");
}
System.out.println();
for(int i = 0; i<5 ; i++)
{
System.out.print("| ");
for(int j = 3 ; j>=0 ; j--)
{
System.out.print(chunk[j][i]+" | ");
}
System.out.print(" ");
}
/*END PACKETS SENT*/
/*PACKETS RECEIVED & ERROR CORRECTION*/
System.out.println();
System.out.println();
System.out.println("PACKETS RECEIVED & ERROR CORRECTION (Suppose there is 4-bit burst error in packet 5 chunk 4)");
String newBit ="";
for(int i = 0 ; i < 4 ; i++)
{
System.out.print(chunk[3][0].charAt(i));
if(chunk[3][0].charAt(i) == '0')
{
newBit += '1';
}
else
{
newBit += '0';
}
}
System.out.print(" is changed to ");
chunk[3][0] = newBit;
System.out.print(newBit);
System.out.println();
for(int i = 0; i< 5 ; i++)
{
System.out.print("\t Pactket "+(5-i)+"\t\t");
}
System.out.println();
for(int i = 0; i<5 ; i++)
{
System.out.print("| ");
for(int j = 3 ; j>=0 ; j--)
{
System.out.print(chunk[j][i]+" | ");
}
System.out.print(" ");
}
System.out.println();
int p = 0;
String correctBit = "";
while(p<4)
{
char tempBit = chunk[3][0].charAt(p);
for(int i = 1 ; i < 4 ; i++)
{
if(tempBit == chunk[3][i].charAt(p))
{
tempBit = 0;
}
else
{
tempBit = 1;
}
}
if(tempBit != chunk[3][4].charAt(p))
{
if(chunk[3][0].charAt(p) == '0')
{
System.out.println("Correct digit of packet 5 chunk 4 from "+chunk[3][0].charAt(p)+" to 1");
correctBit += 1;
}
else
{
System.out.println("Correct digit of packet 5 chunk 4 from "+chunk[3][0].charAt(p)+" to 0");
correctBit += 0;
}
}
p++;
}
System.out.println("Correct packet 5 chunk 4 from " + chunk[3][0] + " to " + correctBit);
chunk[3][0] = correctBit;
/*END PACKETS RECEIVED & ERROR CORRECTION*/
/*PACKET RECREATION AT RECEIVER*/
System.out.println();
System.out.println("PACKET RECREATION AT RECEIVER");
System.out.println("\t|chunk 1|chunk 2|chunk 3|chunk 4|r-bit|");
for(int i = 0; i<4 ; i++)
{
System.out.print("Packet "+(i+1)+" | ");
for(int j = 0 ; j<5 ; j++)
{
System.out.print(chunk[i][j]+" | ");
}
System.out.println();
}
/*END PACKET RECREATION AT RECEIVER*/
}
}