I would like some help in completing this exercise. Thank You public class Lette
ID: 3712168 • Letter: I
Question
I would like some help in completing this exercise. Thank You
public class LetterTally {
/** Declare instance variables. */
/**
* Default constructor. Initialize the instance variables in this method.
*
*/
public LetterTally() {
throw new UnsupportedOperationException("replace with your implementation");
}
/**
* This method returns the number of total letters in the book.
*
* @return the total number of letters in the book
*/
public int totalCount() {
throw new UnsupportedOperationException("replace with your implementation");
}
/**
* This method returns the number of times a letter appear in the book
*
* @param ch
* - the letter of interest
*
* @return
*/
public int freqChar(char ch) {
throw new UnsupportedOperationException("replace with your implementation");
}
/**
* This method computes the entropy of the letters. To compute the entropy, for
* each letter, compute the ratio (say it p) of the frequency count of the
* letter to the total frequency count of all letters. Then, compute the
* following value: p * Math.log (1 / p). Return the sum of the values, rounded
* to the nearest integer value.
*
* @return
*/
public int computeEntropy() {
throw new UnsupportedOperationException("replace with your implementation");
}
}
Explanation / Answer
package com.fd.learningPrograms;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class LetterTally {
private String count;
private String nameOfFile;
private File file;
public LetterTally(String count, String nameOfFile, File file) {
super();
this.count = count;
this.nameOfFile = nameOfFile;
this.file = file;
}
public LetterTally() {
super();
}
/*
* This method is used to read number of letters in a file
*/
public int totalCount() {
// Provide the path of the book which I supposed will be provided in the
// form of file
// Note : Path provided need to defined where your file is located on
// your machine
int countWord = 0;
try {
// Java provide inbuilt method to read a file
File fileToRead = new File("C:\files\input.txt");
FileInputStream fileInputStream = new FileInputStream(fileToRead);
InputStreamReader inputStreamReader = new InputStreamReader(
fileInputStream);
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
// declare to read a line from file
String bookLine;
// to get total number of letters in file
try {
while ((bookLine = bufferedReader.readLine()) != null) {
// words are stored from file in array form and seperated on
// the basis of space between words
String[] spiltWord = new String[50];
// following is declared as \s+ is space regex expressions
if (!(bookLine.equals(""))) {
spiltWord = bookLine.split("\s+");
}
// To calculate only letters i.e a to z or A to Z
// Using regex expression for that
for (String s : spiltWord) {
// replace all is used if words contains any special
// characters or is alphanumberic. As we have to read
// only letters replace all other characters with blank
s = s.replaceAll("[^A-Za-z]+", "");
System.out.println("hdajk" + s.length());
countWord += s.length();
}
}
System.out.println("Total number of word are" + countWord);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return countWord;
}
/*
* Program to read number of time characters appeared in book i.e file in our case
*/
public int freqChar(char ch) {
// Provide the path of the book which I supposed will be provided in the
// form of file
// Note : Path provided need to defined where your file is located on
// your machine
int countCharacterOccurence = 0;
try {
// Java provide inbuilt method to read a file
File fileToRead = new File("C:\files\input.txt");
FileInputStream fileInputStream = new FileInputStream(fileToRead);
InputStreamReader inputStreamReader = new InputStreamReader(
fileInputStream);
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
// declare to read a line from file
String bookLine;
// to get total number of letters in file
try {
while ((bookLine = bufferedReader.readLine()) != null) {
// words are stored from file in array form and seperated on
// the basis of space between words
String[] spiltWord = new String[50];
// following is declared as \s+ is space regex expressions
if (!(bookLine.equals(""))) {
spiltWord = bookLine.split("\s+");
}
for(String s:spiltWord){
for(int i = 0; i < s.length(); i++) {
char charOfString=s.charAt(i);
if(charOfString==ch){
countCharacterOccurence++;
}
}
}
}
System.out.println("Total number of word are" + countCharacterOccurence);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return countCharacterOccurence;
}
/**
* This method computes the entropy of the letters. To compute the entropy, for
* each letter, compute the ratio (say it p) of the frequency count of the
* letter to the total frequency count of all letters. Then, compute the
* following value: p * Math.log (1 / p). Return the sum of the values, rounded
* to the nearest integer value.
*
* @return
*/
public int computeEntropy() {
int getTotalNumberOfLetters=totalCount();
//Here we will just take example of letter A,B and C remaining can be calaculated based on this
int numberOfOccurenceOfA=freqChar('a');
int numberOfOccurenceOfB=freqChar('b');
int numberOfOccurenceOfC=freqChar('c');
//To calculate entropy
float p=numberOfOccurenceOfA/getTotalNumberOfLetters;
float entropy=(float) (p *(Math.log(1/p)));
System.out.println("The rounded value is " + Math.round(entropy));
return Math.round(entropy);
}
public static void main(String[] args) {
LetterTally lt = new LetterTally();
lt.computeEntropy();
}
}