Can I get help fixing my code? Public test @Before public void testA() { String
ID: 3721708 • Letter: C
Question
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<String> text;
public LetterTally() {
text = new ArrayList<String>();
}
public static void setText(ArrayList<String>text) {
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);
}
}
Explanation / Answer
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<String> text;
public LetterTally() {
text = new ArrayList<String>();
}
public static void setText(ArrayList<String>text) {
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);
}
}
The code looks good to me. Could you please explain the issue that you are facing right now. Since, you have not provided the full code, it is difficult for me to compile the code and see the issue myself.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.