Update CRCAlgorithm.java

This commit is contained in:
Libin Yang
2019-02-17 20:56:47 +08:00
committed by GitHub
parent 31bf10f29f
commit d434e4a4f1

View File

@ -1,11 +1,10 @@
package crcalgorithm; package Others;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random; import java.util.Random;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
/** /**
*
* @author dimgrichr * @author dimgrichr
*/ */
public class CRCAlgorithm { public class CRCAlgorithm {
@ -37,17 +36,18 @@ public class CRCAlgorithm {
* The algorithm's main constructor. * The algorithm's main constructor.
* The most significant variables, used in the algorithm, * The most significant variables, used in the algorithm,
* are set in their initial values. * are set in their initial values.
*
* @param str The binary number P, in a string form, which is used by the CRC algorithm * @param str The binary number P, in a string form, which is used by the CRC algorithm
* @param size The size of every transmitted message * @param size The size of every transmitted message
* @param ber The Bit Error Rate * @param ber The Bit Error Rate
*/ */
public CRCAlgorithm(String str, int size, double ber){ public CRCAlgorithm(String str, int size, double ber) {
messageChanged=false; messageChanged = false;
message = new ArrayList<>(); message = new ArrayList<>();
messSize = size; messSize = size;
dividedMessage = new ArrayList<>(); dividedMessage = new ArrayList<>();
p = new ArrayList<>(); p = new ArrayList<>();
for(int i=0;i<str.length();i++){ for (int i = 0; i < str.length(); i++) {
p.add(Character.getNumericValue(str.charAt(i))); p.add(Character.getNumericValue(str.charAt(i)));
} }
randomGenerator = new Random(); randomGenerator = new Random();
@ -61,33 +61,37 @@ public class CRCAlgorithm {
/** /**
* Returns the counter wrongMess * Returns the counter wrongMess
*
* @return wrongMess, the number of Wrong Messages * @return wrongMess, the number of Wrong Messages
*/ */
public int getWrongMess(){ public int getWrongMess() {
return wrongMess; return wrongMess;
} }
/** /**
* Returns the counter wrongMessCaught * Returns the counter wrongMessCaught
*
* @return wrongMessCaught, the number of wrong messages, which are caught by the CRC algoriithm * @return wrongMessCaught, the number of wrong messages, which are caught by the CRC algoriithm
*/ */
public int getWrongMessCaught(){ public int getWrongMessCaught() {
return wrongMessCaught; return wrongMessCaught;
} }
/** /**
* Returns the counter wrongMessNotCaught * Returns the counter wrongMessNotCaught
*
* @return wrongMessNotCaught, the number of wrong messages, which are not caught by the CRC algorithm * @return wrongMessNotCaught, the number of wrong messages, which are not caught by the CRC algorithm
*/ */
public int getWrongMessNotCaught(){ public int getWrongMessNotCaught() {
return wrongMessNotCaught; return wrongMessNotCaught;
} }
/** /**
* Returns the counter correctMess * Returns the counter correctMess
*
* @return correctMess, the number of the Correct Messages * @return correctMess, the number of the Correct Messages
*/ */
public int getCorrectMess(){ public int getCorrectMess() {
return correctMess; return correctMess;
} }
@ -96,7 +100,7 @@ public class CRCAlgorithm {
* so that it can be re-used, in order not to waste too much memory and time, * so that it can be re-used, in order not to waste too much memory and time,
* by creating new objects. * by creating new objects.
*/ */
public void refactor(){ public void refactor() {
messageChanged = false; messageChanged = false;
message = new ArrayList<>(); message = new ArrayList<>();
dividedMessage = new ArrayList<>(); dividedMessage = new ArrayList<>();
@ -106,9 +110,9 @@ public class CRCAlgorithm {
* Random messages, consisted of 0's and 1's, * Random messages, consisted of 0's and 1's,
* are generated, so that they can later be transmitted * are generated, so that they can later be transmitted
*/ */
public void generateRandomMess(){ public void generateRandomMess() {
for(int i=0;i<messSize;i++){ for (int i = 0; i < messSize; i++) {
int x = ThreadLocalRandom.current().nextInt(0,2); int x = ThreadLocalRandom.current().nextInt(0, 2);
message.add(x); message.add(x);
} }
} }
@ -120,51 +124,48 @@ public class CRCAlgorithm {
* If it does, the message is considered to be wrong by the receiver,so the variable wrongMessCaught changes. * If it does, the message is considered to be wrong by the receiver,so the variable wrongMessCaught changes.
* If it does not, it is accepted, so one of the variables correctMess, wrongMessNotCaught, changes. * If it does not, it is accepted, so one of the variables correctMess, wrongMessNotCaught, changes.
* If check == false, the diviided Message is added at the end of the ArrayList<integer> message. * If check == false, the diviided Message is added at the end of the ArrayList<integer> message.
*
* @param check the variable used to determine, if the message is going to be checked from the receiver * @param check the variable used to determine, if the message is going to be checked from the receiver
* if true, it is checked * if true, it is checked
* otherwise, it is not * otherwise, it is not
*/ */
public void divideMessageWithP(boolean check){ public void divideMessageWithP(boolean check) {
ArrayList<Integer> x = new ArrayList<>(); ArrayList<Integer> x = new ArrayList<>();
ArrayList<Integer> k = (ArrayList<Integer>) message.clone(); ArrayList<Integer> k = (ArrayList<Integer>) message.clone();
if(!check){ if (!check) {
for(int i=0;i<p.size()-1;i++){ for (int i = 0; i < p.size() - 1; i++) {
k.add(0); k.add(0);
} }
} }
while(!k.isEmpty()){ while (!k.isEmpty()) {
while(x.size()<p.size() && !k.isEmpty()){ while (x.size() < p.size() && !k.isEmpty()) {
x.add(k.get(0)); x.add(k.get(0));
k.remove(0); k.remove(0);
} }
if(x.size()==p.size()){ if (x.size() == p.size()) {
for(int i=0;i<p.size();i++){ for (int i = 0; i < p.size(); i++) {
if(x.get(i)==p.get(i)){ if (x.get(i) == p.get(i)) {
x.set(i, 0); x.set(i, 0);
} } else {
else{
x.set(i, 1); x.set(i, 1);
} }
} }
for(int i=0;i<x.size() && x.get(i)!=1;i++){ for (int i = 0; i < x.size() && x.get(i) != 1; i++) {
x.remove(0); x.remove(0);
} }
} }
} }
dividedMessage = (ArrayList<Integer>) x.clone(); dividedMessage = (ArrayList<Integer>) x.clone();
if(!check){ if (!check) {
for(int z:dividedMessage){ for (int z : dividedMessage) {
message.add(z); message.add(z);
} }
} } else {
else{ if (dividedMessage.contains(1) && messageChanged) {
if(dividedMessage.contains(1) && messageChanged){
wrongMessCaught++; wrongMessCaught++;
} } else if (!dividedMessage.contains(1) && messageChanged) {
else if(!dividedMessage.contains(1) && messageChanged){
wrongMessNotCaught++; wrongMessNotCaught++;
} } else if (!messageChanged) {
else if(!messageChanged){
correctMess++; correctMess++;
} }
} }
@ -180,25 +181,23 @@ public class CRCAlgorithm {
* Based on these changes. the boolean variable messageChanged, gets the value: * Based on these changes. the boolean variable messageChanged, gets the value:
* true, or false. * true, or false.
*/ */
public void changeMess(){ public void changeMess() {
for(int y : message){ for (int y : message) {
double x = randomGenerator.nextDouble(); double x = randomGenerator.nextDouble();
while(x<0.0000 || x>1.00000){ while (x < 0.0000 || x > 1.00000) {
x = randomGenerator.nextDouble(); x = randomGenerator.nextDouble();
} }
if(x<ber){ if (x < ber) {
messageChanged = true; messageChanged = true;
if(y == 1){ if (y == 1) {
message.set(message.indexOf(y), 0); message.set(message.indexOf(y), 0);
} } else {
else{
message.set(message.indexOf(y), 1); message.set(message.indexOf(y), 1);
} }
} }
} }
if(messageChanged){ if (messageChanged) {
wrongMess++; wrongMess++;
} }
} }
} }