Skip to content

Commit d434e4a

Browse files
authored
Update CRCAlgorithm.java
1 parent 31bf10f commit d434e4a

File tree

1 file changed

+67
-68
lines changed

1 file changed

+67
-68
lines changed

Others/CRCAlgorithm.java

Lines changed: 67 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
1-
package crcalgorithm;
1+
package Others;
22

33
import java.util.ArrayList;
44
import java.util.Random;
55
import java.util.concurrent.ThreadLocalRandom;
66

77
/**
8-
*
98
* @author dimgrichr
109
*/
1110
public class CRCAlgorithm {
1211

1312
private int correctMess;
14-
13+
1514
private int wrongMess;
16-
15+
1716
private int wrongMessCaught;
18-
17+
1918
private int wrongMessNotCaught;
20-
19+
2120
private int messSize;
22-
21+
2322
private double ber;
24-
23+
2524
private boolean messageChanged;
2625

2726
private ArrayList<Integer> message;
28-
27+
2928
private ArrayList<Integer> dividedMessage;
30-
29+
3130
private ArrayList<Integer> p;
32-
31+
3332
private Random randomGenerator;
34-
35-
33+
34+
3635
/**
3736
* The algorithm's main constructor.
3837
* The most significant variables, used in the algorithm,
3938
* 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
4141
* @param size The size of every transmitted message
42-
* @param ber The Bit Error Rate
42+
* @param ber The Bit Error Rate
4343
*/
44-
public CRCAlgorithm(String str, int size, double ber){
45-
messageChanged=false;
44+
public CRCAlgorithm(String str, int size, double ber) {
45+
messageChanged = false;
4646
message = new ArrayList<>();
4747
messSize = size;
4848
dividedMessage = new ArrayList<>();
4949
p = new ArrayList<>();
50-
for(int i=0;i<str.length();i++){
50+
for (int i = 0; i < str.length(); i++) {
5151
p.add(Character.getNumericValue(str.charAt(i)));
5252
}
5353
randomGenerator = new Random();
@@ -57,119 +57,120 @@ public CRCAlgorithm(String str, int size, double ber){
5757
wrongMessNotCaught = 0;
5858
this.ber = ber;
5959
}
60-
61-
60+
61+
6262
/**
6363
* Returns the counter wrongMess
64+
*
6465
* @return wrongMess, the number of Wrong Messages
6566
*/
66-
public int getWrongMess(){
67+
public int getWrongMess() {
6768
return wrongMess;
6869
}
69-
70+
7071
/**
7172
* Returns the counter wrongMessCaught
73+
*
7274
* @return wrongMessCaught, the number of wrong messages, which are caught by the CRC algoriithm
7375
*/
74-
public int getWrongMessCaught(){
76+
public int getWrongMessCaught() {
7577
return wrongMessCaught;
7678
}
77-
79+
7880
/**
7981
* Returns the counter wrongMessNotCaught
82+
*
8083
* @return wrongMessNotCaught, the number of wrong messages, which are not caught by the CRC algorithm
8184
*/
82-
public int getWrongMessNotCaught(){
85+
public int getWrongMessNotCaught() {
8386
return wrongMessNotCaught;
8487
}
85-
88+
8689
/**
8790
* Returns the counter correctMess
91+
*
8892
* @return correctMess, the number of the Correct Messages
8993
*/
90-
public int getCorrectMess(){
94+
public int getCorrectMess() {
9195
return correctMess;
9296
}
93-
97+
9498
/**
9599
* Resets some of the object's values, used on the main function,
96100
* so that it can be re-used, in order not to waste too much memory and time,
97101
* by creating new objects.
98102
*/
99-
public void refactor(){
103+
public void refactor() {
100104
messageChanged = false;
101105
message = new ArrayList<>();
102106
dividedMessage = new ArrayList<>();
103107
}
104-
108+
105109
/**
106110
* Random messages, consisted of 0's and 1's,
107111
* are generated, so that they can later be transmitted
108112
*/
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);
112116
message.add(x);
113117
}
114118
}
115-
119+
116120
/**
117121
* The most significant part of the CRC algorithm.
118122
* The message is divided by P, so the dividedMessage ArrayList<Integer> is created.
119123
* If check == true, the dividedMessaage is examined, in order to see if it contains any 1's.
120124
* If it does, the message is considered to be wrong by the receiver,so the variable wrongMessCaught changes.
121125
* 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+
*
123128
* @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
126131
*/
127-
public void divideMessageWithP(boolean check){
132+
public void divideMessageWithP(boolean check) {
128133
ArrayList<Integer> x = new ArrayList<>();
129134
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++) {
132137
k.add(0);
133138
}
134139
}
135-
while(!k.isEmpty()){
136-
while(x.size()<p.size() && !k.isEmpty()){
140+
while (!k.isEmpty()) {
141+
while (x.size() < p.size() && !k.isEmpty()) {
137142
x.add(k.get(0));
138143
k.remove(0);
139144
}
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)) {
143148
x.set(i, 0);
144-
}
145-
else{
149+
} else {
146150
x.set(i, 1);
147151
}
148152
}
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++) {
150154
x.remove(0);
151-
}
155+
}
152156
}
153157
}
154158
dividedMessage = (ArrayList<Integer>) x.clone();
155-
if(!check){
156-
for(int z:dividedMessage){
159+
if (!check) {
160+
for (int z : dividedMessage) {
157161
message.add(z);
158162
}
159-
}
160-
else{
161-
if(dividedMessage.contains(1) && messageChanged){
163+
} else {
164+
if (dividedMessage.contains(1) && messageChanged) {
162165
wrongMessCaught++;
163-
}
164-
else if(!dividedMessage.contains(1) && messageChanged){
166+
} else if (!dividedMessage.contains(1) && messageChanged) {
165167
wrongMessNotCaught++;
166-
}
167-
else if(!messageChanged){
168+
} else if (!messageChanged) {
168169
correctMess++;
169170
}
170171
}
171172
}
172-
173+
173174
/**
174175
* Once the message is transmitted, some of it's elements,
175176
* is possible to change from 1 to 0, or from 0 to 1,
@@ -180,25 +181,23 @@ else if(!messageChanged){
180181
* Based on these changes. the boolean variable messageChanged, gets the value:
181182
* true, or false.
182183
*/
183-
public void changeMess(){
184-
for(int y : message){
184+
public void changeMess() {
185+
for (int y : message) {
185186
double x = randomGenerator.nextDouble();
186-
while(x<0.0000 || x>1.00000){
187+
while (x < 0.0000 || x > 1.00000) {
187188
x = randomGenerator.nextDouble();
188189
}
189-
if(x<ber){
190+
if (x < ber) {
190191
messageChanged = true;
191-
if(y == 1){
192+
if (y == 1) {
192193
message.set(message.indexOf(y), 0);
193-
}
194-
else{
194+
} else {
195195
message.set(message.indexOf(y), 1);
196196
}
197197
}
198198
}
199-
if(messageChanged){
199+
if (messageChanged) {
200200
wrongMess++;
201201
}
202202
}
203-
204203
}

0 commit comments

Comments
 (0)