1
- package crcalgorithm ;
1
+ package Others ;
2
2
3
3
import java .util .ArrayList ;
4
4
import java .util .Random ;
5
5
import java .util .concurrent .ThreadLocalRandom ;
6
6
7
7
/**
8
- *
9
8
* @author dimgrichr
10
9
*/
11
10
public class CRCAlgorithm {
12
11
13
12
private int correctMess ;
14
-
13
+
15
14
private int wrongMess ;
16
-
15
+
17
16
private int wrongMessCaught ;
18
-
17
+
19
18
private int wrongMessNotCaught ;
20
-
19
+
21
20
private int messSize ;
22
-
21
+
23
22
private double ber ;
24
-
23
+
25
24
private boolean messageChanged ;
26
25
27
26
private ArrayList <Integer > message ;
28
-
27
+
29
28
private ArrayList <Integer > dividedMessage ;
30
-
29
+
31
30
private ArrayList <Integer > p ;
32
-
31
+
33
32
private Random randomGenerator ;
34
-
35
-
33
+
34
+
36
35
/**
37
36
* The algorithm's main constructor.
38
37
* The most significant variables, used in the algorithm,
39
38
* are set in their initial values.
40
- * @param str The binary number P, in a string form, which is used by the CRC algorithm
39
+ *
40
+ * @param str The binary number P, in a string form, which is used by the CRC algorithm
41
41
* @param size The size of every transmitted message
42
- * @param ber The Bit Error Rate
42
+ * @param ber The Bit Error Rate
43
43
*/
44
- public CRCAlgorithm (String str , int size , double ber ){
45
- messageChanged = false ;
44
+ public CRCAlgorithm (String str , int size , double ber ) {
45
+ messageChanged = false ;
46
46
message = new ArrayList <>();
47
47
messSize = size ;
48
48
dividedMessage = new ArrayList <>();
49
49
p = new ArrayList <>();
50
- for (int i = 0 ; i < str .length ();i ++){
50
+ for (int i = 0 ; i < str .length (); i ++) {
51
51
p .add (Character .getNumericValue (str .charAt (i )));
52
52
}
53
53
randomGenerator = new Random ();
@@ -57,119 +57,120 @@ public CRCAlgorithm(String str, int size, double ber){
57
57
wrongMessNotCaught = 0 ;
58
58
this .ber = ber ;
59
59
}
60
-
61
-
60
+
61
+
62
62
/**
63
63
* Returns the counter wrongMess
64
+ *
64
65
* @return wrongMess, the number of Wrong Messages
65
66
*/
66
- public int getWrongMess (){
67
+ public int getWrongMess () {
67
68
return wrongMess ;
68
69
}
69
-
70
+
70
71
/**
71
72
* Returns the counter wrongMessCaught
73
+ *
72
74
* @return wrongMessCaught, the number of wrong messages, which are caught by the CRC algoriithm
73
75
*/
74
- public int getWrongMessCaught (){
76
+ public int getWrongMessCaught () {
75
77
return wrongMessCaught ;
76
78
}
77
-
79
+
78
80
/**
79
81
* Returns the counter wrongMessNotCaught
82
+ *
80
83
* @return wrongMessNotCaught, the number of wrong messages, which are not caught by the CRC algorithm
81
84
*/
82
- public int getWrongMessNotCaught (){
85
+ public int getWrongMessNotCaught () {
83
86
return wrongMessNotCaught ;
84
87
}
85
-
88
+
86
89
/**
87
90
* Returns the counter correctMess
91
+ *
88
92
* @return correctMess, the number of the Correct Messages
89
93
*/
90
- public int getCorrectMess (){
94
+ public int getCorrectMess () {
91
95
return correctMess ;
92
96
}
93
-
97
+
94
98
/**
95
99
* Resets some of the object's values, used on the main function,
96
100
* so that it can be re-used, in order not to waste too much memory and time,
97
101
* by creating new objects.
98
102
*/
99
- public void refactor (){
103
+ public void refactor () {
100
104
messageChanged = false ;
101
105
message = new ArrayList <>();
102
106
dividedMessage = new ArrayList <>();
103
107
}
104
-
108
+
105
109
/**
106
110
* Random messages, consisted of 0's and 1's,
107
111
* are generated, so that they can later be transmitted
108
112
*/
109
- public void generateRandomMess (){
110
- for (int i = 0 ; i < messSize ;i ++){
111
- int x = ThreadLocalRandom .current ().nextInt (0 ,2 );
113
+ public void generateRandomMess () {
114
+ for (int i = 0 ; i < messSize ; i ++) {
115
+ int x = ThreadLocalRandom .current ().nextInt (0 , 2 );
112
116
message .add (x );
113
117
}
114
118
}
115
-
119
+
116
120
/**
117
121
* The most significant part of the CRC algorithm.
118
122
* The message is divided by P, so the dividedMessage ArrayList<Integer> is created.
119
123
* If check == true, the dividedMessaage is examined, in order to see if it contains any 1's.
120
124
* If it does, the message is considered to be wrong by the receiver,so the variable wrongMessCaught changes.
121
125
* If it does not, it is accepted, so one of the variables correctMess, wrongMessNotCaught, changes.
122
- * If check == false, the diviided Message is added at the end of the ArrayList<integer> message.
126
+ * If check == false, the diviided Message is added at the end of the ArrayList<integer> message.
127
+ *
123
128
* @param check the variable used to determine, if the message is going to be checked from the receiver
124
- * if true, it is checked
125
- * otherwise, it is not
129
+ * if true, it is checked
130
+ * otherwise, it is not
126
131
*/
127
- public void divideMessageWithP (boolean check ){
132
+ public void divideMessageWithP (boolean check ) {
128
133
ArrayList <Integer > x = new ArrayList <>();
129
134
ArrayList <Integer > k = (ArrayList <Integer >) message .clone ();
130
- if (!check ){
131
- for (int i = 0 ; i < p .size ()- 1 ; i ++){
135
+ if (!check ) {
136
+ for (int i = 0 ; i < p .size () - 1 ; i ++) {
132
137
k .add (0 );
133
138
}
134
139
}
135
- while (!k .isEmpty ()){
136
- while (x .size ()< p .size () && !k .isEmpty ()){
140
+ while (!k .isEmpty ()) {
141
+ while (x .size () < p .size () && !k .isEmpty ()) {
137
142
x .add (k .get (0 ));
138
143
k .remove (0 );
139
144
}
140
- if (x .size ()== p .size ()){
141
- for (int i = 0 ; i < p .size ();i ++){
142
- if (x .get (i )== p .get (i )){
145
+ if (x .size () == p .size ()) {
146
+ for (int i = 0 ; i < p .size (); i ++) {
147
+ if (x .get (i ) == p .get (i )) {
143
148
x .set (i , 0 );
144
- }
145
- else {
149
+ } else {
146
150
x .set (i , 1 );
147
151
}
148
152
}
149
- for (int i = 0 ; i < x .size () && x .get (i )!= 1 ; i ++){
153
+ for (int i = 0 ; i < x .size () && x .get (i ) != 1 ; i ++) {
150
154
x .remove (0 );
151
- }
155
+ }
152
156
}
153
157
}
154
158
dividedMessage = (ArrayList <Integer >) x .clone ();
155
- if (!check ){
156
- for (int z : dividedMessage ){
159
+ if (!check ) {
160
+ for (int z : dividedMessage ) {
157
161
message .add (z );
158
162
}
159
- }
160
- else {
161
- if (dividedMessage .contains (1 ) && messageChanged ){
163
+ } else {
164
+ if (dividedMessage .contains (1 ) && messageChanged ) {
162
165
wrongMessCaught ++;
163
- }
164
- else if (!dividedMessage .contains (1 ) && messageChanged ){
166
+ } else if (!dividedMessage .contains (1 ) && messageChanged ) {
165
167
wrongMessNotCaught ++;
166
- }
167
- else if (!messageChanged ){
168
+ } else if (!messageChanged ) {
168
169
correctMess ++;
169
170
}
170
171
}
171
172
}
172
-
173
+
173
174
/**
174
175
* Once the message is transmitted, some of it's elements,
175
176
* is possible to change from 1 to 0, or from 0 to 1,
@@ -180,25 +181,23 @@ else if(!messageChanged){
180
181
* Based on these changes. the boolean variable messageChanged, gets the value:
181
182
* true, or false.
182
183
*/
183
- public void changeMess (){
184
- for (int y : message ){
184
+ public void changeMess () {
185
+ for (int y : message ) {
185
186
double x = randomGenerator .nextDouble ();
186
- while ( x < 0.0000 || x > 1.00000 ){
187
+ while ( x < 0.0000 || x > 1.00000 ) {
187
188
x = randomGenerator .nextDouble ();
188
189
}
189
- if ( x < ber ){
190
+ if ( x < ber ) {
190
191
messageChanged = true ;
191
- if (y == 1 ){
192
+ if (y == 1 ) {
192
193
message .set (message .indexOf (y ), 0 );
193
- }
194
- else {
194
+ } else {
195
195
message .set (message .indexOf (y ), 1 );
196
196
}
197
197
}
198
198
}
199
- if (messageChanged ){
199
+ if (messageChanged ) {
200
200
wrongMess ++;
201
201
}
202
202
}
203
-
204
203
}
0 commit comments