-
Notifications
You must be signed in to change notification settings - Fork 2
/
ParityCheck.java
81 lines (77 loc) · 2.22 KB
/
ParityCheck.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
/**
* 4. Write a computer program that encode a dataword into a codeword and decode a codeword to determine whether the codeword is in error for parity check code.
* Created by Peerachai Banyongrakkul Sec.1 5988070
* ParityCheck.java
*/
import java.util.Scanner;
public class ParityCheck
{
public static void main(String[] args)
{
System.out.println("---------- Parity Check ----------");
System.out.println("ENCODE");
Scanner sc = new Scanner(System.in);
System.out.print("Dataword: ");
String data = sc.nextLine();
DataWord dataWord = new DataWord(data);
String codeWord = encode(dataWord);
System.out.println();
System.out.println("DECODE");
System.out.print("Received Codeword: ");
String code = sc.nextLine();
decode(code,codeWord);
}
/**
*
* @param d
* @return
*/
public static String encode(DataWord d)
{
String codeword = d.getData();
char rBit = d.getData().charAt(0);
for(int i = 1 ; i < d.getLength(); i++)
{
//System.out.print(d.getData().charAt(i));
if(rBit == d.getData().charAt(i))
{
rBit = '0';
}
else
{
rBit = '1';
}
}
codeword += rBit;
System.out.println("r-bit: "+ rBit);
System.out.println("Codeword: "+codeword);
return codeword;
}
public static void decode(String code, String codeWord)
{
System.out.println();
String cw = codeWord.substring(0, codeWord.length()-2);
char r = codeWord.charAt(codeWord.length()-1);
char rBit = cw.charAt(0);
for(int i = 1 ; i < cw.length(); i++)
{
//System.out.print(d.getData().charAt(i));
if(rBit == cw.charAt(i))
{
rBit = '0';
}
else
{
rBit = '1';
}
}
if(rBit == r)
{
System.out.println("***The codeword isn't in error");
}
else
{
System.out.println("***The codeword is in error");
}
}
}