Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can I get help fixing my code? Public test @Before public void testA() { String s = In addition, it made us tolerant of each

Can I get help fixing my code?

Public test

@Before public void testA() { String s = "In addition, it made us tolerant of each other's " + "yarns--and even convictions. "; bm = new BookMain(); Scanner input = new Scanner(s); bm.analyzeBookText(input); }

@Test public void testTotalCount() { int totalCount = bm.letterData.totalCount(); assertEquals(61, totalCount); }

@Test public void testFreqChar() { assertEquals(4, bm.letterData.freqChar('s')); }

@Test public void testComputeEntropy() { int entropy = bm.letterData.computeEntropy(); assertEquals(3, entropy); }

This is the Code

import java.util.ArrayList;

public class LetterTally {

private static ArrayList text;

public LetterTally() {

text = new ArrayList();

}

public static void setText(ArrayListtext) {

LetterTally.text = text;

}

public int totalCount() {

int count = 0;

for(String s : text) {

for(int i = 0 ; i < s.length(); i++){

if(Character.isLetter(s.charAt(i))){

count++;

}

}

}

return count;

}

public int freqChar(char ch) {

int charCount = 0;

for(String s : text) {

for(int i = 0 ; i < s.length(); i++){

if(s.charAt(i) == ch){

charCount++;

}

}

}

return charCount;

}

public int computeEntropy() {

double entropy = 0;

double [] p = new double [26];

for(int i = 0; i < p.length; i++) {

p [i] = ((freqChar((char) 0)) / ((double)totalCount()));

if(p[i] != 0) {

entropy += p[i] * (Math.log(1/p[i]));

}

else

entropy = 0 ;

}

return (int)Math.round(entropy);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Marketing Database Analytics

Authors: Andrew D. Banasiewicz

1st Edition

0415657881, 978-0415657884

Students also viewed these Databases questions