File not found. Java Program to Calculate Entropy 2 Ways | Java Programs
Latest :

Java Program to Calculate Entropy 2 Ways | Java Programs

Java program to calculate entropy in 5 ways. Entropy, in short, is the average level of information or uncertainty in a particular variable’s possibility of outcomes.

Our problem statement here is, to write a code to find the entropy of a number of messages. For this, our required inputs are, the number of messages (n) and the probability of each message (p) which can be fractional so we prefer making use of double datatype. Our expected output is the entropy value which is also of type double.

Firstly, to gather our requirements or inputs at runtime, we can make use of Scanner class in Java. This can be used to read input at runtime for any primitive datatype. So, to make use of predefined methods in this class, we first create an object for this class.

Scanner input = new Scanner(System.in);

We will then read the number of messages at runtime using the scanner class as follows:

n = input.nextDouble();

Since entropy is the uncertainty for all the possible outcomes, we will read the probability of each message from 1 to n and using the predefined log() method of Math package, we will use the formula below and keep adding the uncertainty of each message to the entropy as follows:

for(int i=1;i<=n;i++) {

System.out.println(“\nEnter the the probability of message “+i +”:”);

p = input.nextDouble();

entrophy+=(p*(Math.log(1/p))/Math.log(2));

}

As you can observe, in case of each message uncertainty is calculated as added to the value of entropy which in the beginning of the code was initialized of zero. This sum total by the end of the loop is nothing but, our expected output i.e., the entropy of the source which is stored in the resultant variable (entropy).  This is then, printed or displayed on the console screen by making use of System.out.println() method to display in a new line.

System.out.println(“\nThe entrophy of a source is :”+ entrophy);

x

Check Also

Java Inverted Mirrored Right Triangle Star Pattern

Java program to print Inverted mirrored right triangle star pattern program. We have written below ...

atakoy escortatakoy escortbakırköy escorthalkali escortbağcılar escortsightcareavcilar eskortkocaeli escortmaltepe escortdeneme bonusu veren sitelerbasaksehir escortistanbul, escortromabetdeneme bonusu veren sitelerdeneme bonusu veren sitelerkulisbetjojobetbelek escortjojobet토토사이트Deneme Bonusu Veren Sitelergrandpashabetdeneme bonusu veren sitelergrandpashabetgrandpashabetgrandpashabetistanbul escortAlanya Escortcasibom girişholiganbetGrandpashabetCasibombetmarinograndpashabetjojobetholiganbet girişsekabet girişportobetjustin tvcasibom resmi girişcasibom giriştaraftariumselçuksportsultraslotcasibomultraslotultraslotultrabetgrandpashabetcasibom girişcasibom güncel girişdeneme bonusu veren sitelergrandpashabetdeneme bonusu veren sitelerdeneme bonusu veren sitelerdeneme bonusucanlı casinovaycasinoHoliganbetHoliganbetbahsegelbahsegelsekabetcasibomcasibom güncel girişbetsmovebahsegelkralbetbetplay girişvaycasinocasibomdeneme bonusucasibomİmajbetmatadorbetgrandpashabetjojobetbetcio güncel girişcasibomcasibomcasibombetcio güncel girişgrandpashabetbetturkey güncel girişcasibomcasibom girişcasibom girişcasibomgrandpashabetmarsbahismarsbahis girişjojobetGrandpashabetcasibomportobetOtobetNakitbahisbetturkeybetturkeyzbahisbetcioOtobetcasibomHoliganbetgrandpashabet girişpadisahbetvaycasinovaycasinoperabetgrandpashabetmarsbahis güncel girişgrandpashabetMadridbetgrandpashabetGrandpashabetcasibom girişmatbetmatbet girişgrandpashabet casibom güncel girişcasibom resmi girişsekabet girişbetsmove girişbahsegel girişkralbet girişcasibom güncel girişmatadorbet girişbetciocasibom güncel girişBetsatmatbetcasibomcasibomcasibomcasibomcasibom girişcasibom güncel giriş