/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package crcalgorithm; import java.util.ArrayList; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; /** * * @author dimig */ public class CRCAlgorithm { private int correctMess; private int wrongMess; private int wrongMessCaught; private int wrongMessNotCaught; private int messSize; private double ber; private boolean messageChanged; private ArrayList message; private ArrayList dividedMessage; private ArrayList p; private Random randomGenerator; /** * The algorithm's main constructor. * The most significant variables, used in the algorithm, * are set in their initial values. * @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 ber The Bit Error Rate */ public CRCAlgorithm(String str, int size, double ber){ messageChanged=false; message = new ArrayList<>(); messSize = size; dividedMessage = new ArrayList<>(); p = new ArrayList<>(); for(int i=0;i(); dividedMessage = new ArrayList<>(); } /** * Random messages, consisted of 0's and 1's, * are generated, so that they can later be transmitted */ public void generateRandomMess(){ for(int i=0;i is created. * If check == true, the dividedMessaage is examined, in order to see if it contains any 1's. * 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 check == false, the diviided Message is added at the end of the ArrayList message. * @param check the variable used to determine, if the message is going to be checked from the receiver * if true, it is checked * otherwise, it is not */ public void divideMessageWithP(boolean check){ ArrayList x = new ArrayList<>(); ArrayList k = (ArrayList) message.clone(); if(!check){ for(int i=0;i) x.clone(); if(!check){ for(int z:dividedMessage){ message.add(z); } } else{ if(dividedMessage.contains(1) && messageChanged){ wrongMessCaught++; } else if(!dividedMessage.contains(1) && messageChanged){ wrongMessNotCaught++; } else if(!messageChanged){ correctMess++; } } } /** * Once the message is transmitted, some of it's elements, * is possible to change from 1 to 0, or from 0 to 1, * because of the Bit Error Rate (ber). * For every element of the message, a random double number is created. * If that number is smaller than ber, then the spesific element changes. * On the other hand, if it's bigger than ber, it does not. * Based on these changes. the boolean variable messageChanged, gets the value: * true, or false. */ public void changeMess(){ for(int y : message){ double x = randomGenerator.nextDouble(); while(x<0.0000 || x>1.00000){ x = randomGenerator.nextDouble(); } if(x