-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtwoDParityCheck.java
222 lines (213 loc) · 6.62 KB
/
twoDParityCheck.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
/**
* 1. Implement a two dimensional parity check code and show that it can detect an even number of errors.
* Created by
* Peerachai Banyongrakkul Sec.1 5988070
* Boonyada Lojanarungsiri Sec.1 5988153
* twoDParityCheck.java
*/
import java.util.*;
public class twoDParityCheck
{
public static void main(String[]args)
{
System.out.println("---------- 2D Parity Check----------");
System.out.println("ENCODE");
Scanner sc = new Scanner(System.in);
System.out.print("Input number of block: ");
int n = sc.nextInt();
String temp = sc.nextLine();
System.out.print("Input length of each block: ");
int length = sc.nextInt();
temp = sc.nextLine();
String[] data = new String[n];
int count = 0;
while(count < n)
{
System.out.print("Block "+(count+1)+": ");
data[count] = sc.nextLine();
if(data[count].length() != length)
{
System.out.println("Wrong input!! you must input "+length+" bits");
System.exit(0);
}
count++;
}
String codeWord[] = encode(data);
System.out.println();
System.out.println();
System.out.println("CHECK IT CAN DETECT AN EVEN NUMBER OF ERRORS");
System.out.println("Received codeword: ");
String[] ReCodeWord = new String[n];
count = 0;
while(count < n)
{
System.out.print("Block "+(count+1)+": ");
ReCodeWord[count] = sc.nextLine();
if(ReCodeWord[count].length() != length+1)
{
System.out.println("Wrong input!! you must input "+(length+1)+" bits");
System.exit(0);
}
ReCodeWord[count] = ReCodeWord[count].substring(0,ReCodeWord[count].length()-1);
count++;
}
checkError(codeWord,ReCodeWord);
}
public static String[] encode(String[] dataWord)
{
int count = 0;
char rRowBit[] = new char[dataWord.length];
while(count<dataWord.length)
{
rRowBit[count] = dataWord[count].charAt(0);
for(int i = 1 ; i < dataWord[count].length() ; i++)
{
if(rRowBit[count] == dataWord[count].charAt(i))
{
rRowBit[count] = '0';
}
else
{
rRowBit[count] = '1';
}
}
dataWord[count] += rRowBit[count];
count++;
}
System.out.print("Row parities: ");
String RowBit = "";
for(int i = 0 ; i<rRowBit.length ; i++)
{
RowBit += rRowBit[i];
System.out.print(rRowBit[i]);
}
System.out.println();
count = 0;
char rColBit[] = new char[dataWord[0].length()];
String ColBit = "";
while(count<dataWord[0].length())
{
rColBit[count] = dataWord[0].charAt(count);
for(int i = 1 ; i < dataWord.length ; i++)
{
//System.out.println(dataWord[i].charAt(count));
if(rColBit[count] == dataWord[i].charAt(count))
{
rColBit[count] = '0';
}
else
{
rColBit[count] = '1';
}
}
count++;
}
System.out.print("Column parities: ");
for(int i = 0 ; i<rColBit.length ; i++)
{
ColBit += rColBit[i];
System.out.print(rColBit[i]);
}
System.out.println();
String code[] = new String[dataWord.length+1];
String codeWord = "";
for(int i = 0 ; i<code.length ; i++)
{
if( i == dataWord.length)
{
code[i] = ColBit;
}
else
{
code[i] = dataWord[i];
}
}
System.out.print("Codeword: ");
for(int i = 0 ; i<code.length ; i++)
{
System.out.print(code[i] + " ");
codeWord += code[i];
}
String[] CodeWord = new String[3];
for(int i = 0 ; i<3 ; i++)
{
switch (i) {
case 0:
CodeWord[i] = codeWord;
break;
case 1:
CodeWord[i] = RowBit;
break;
default:
CodeWord[i] = ColBit;
break;
}
}
return CodeWord;
}
public static void checkError(String[] codeWord, String[] ReCodeWord)
{
int count = 0;
char rRowBit[] = new char[ReCodeWord.length];
while(count<ReCodeWord.length)
{
rRowBit[count] = ReCodeWord[count].charAt(0);
for(int i = 1 ; i < ReCodeWord[count].length() ; i++)
{
if(rRowBit[count] == ReCodeWord[count].charAt(i))
{
rRowBit[count] = '0';
}
else
{
rRowBit[count] = '1';
}
}
ReCodeWord[count] += rRowBit[count];
count++;
}
System.out.print("Row parities: ");
String RowBit = "";
for(int i = 0 ; i<rRowBit.length ; i++)
{
RowBit += rRowBit[i];
System.out.print(rRowBit[i]);
}
System.out.println();
count = 0;
char rColBit[] = new char[ReCodeWord[0].length()];
String ColBit = "";
while(count<ReCodeWord[0].length())
{
rColBit[count] = ReCodeWord[0].charAt(count);
for(int i = 1 ; i < ReCodeWord.length ; i++)
{
//System.out.println(dataWord[i].charAt(count));
if(rColBit[count] == ReCodeWord[i].charAt(count))
{
rColBit[count] = '0';
}
else
{
rColBit[count] = '1';
}
}
count++;
}
System.out.print("Column parities: ");
for(int i = 0 ; i<rColBit.length ; i++)
{
ColBit += rColBit[i];
System.out.print(rColBit[i]);
}
System.out.println();
if(codeWord[1].equals(RowBit) && codeWord[2].equals(ColBit))
{
System.out.println("***The codeword isn't in error");
}
else
{
System.out.println("***The codeword is in error");
}
}
}