Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed assignment #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/CharacterFrequencyCounter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
import java.util.Map;

/**
* The CharacterFrequencyCounter class analyzes a given string to count the frequency
* of characters (letters and digits ONLY) in a case-insensitive manner. It provides methods
* to get the frequency of a specific character, retrieve the full frequency map, and
* The CharacterFrequencyCounter class analyzes a given string to count the
* frequency
* of characters (letters and digits ONLY) in a case-insensitive manner. It
* provides methods
* to get the frequency of a specific character, retrieve the full frequency
* map, and
* calculate the relative percentage of a character's occurrence.
*/
public class CharacterFrequencyCounter {
Expand Down Expand Up @@ -32,7 +35,7 @@ public CharacterFrequencyCounter(String input) {
private void processString(String input) {
for (char c : input.toCharArray()) {
if (Character.isLetterOrDigit(c)) { // Count only letters and digits
c = Character.toLowerCase(c); // Case insensitive counting
c = Character.toLowerCase(c); // Case insensitive counting
frequencyMap.put(c, frequencyMap.getOrDefault(c, 0) + 1);
totalCharacterCount++;
}
Expand All @@ -47,20 +50,25 @@ private void processString(String input) {
* @return the frequency of the specified character, or 0 if it is not present
*/
public int getFrequency(char c) {
c = Character.toLowerCase(c); // Case insensitive lookup
c = Character.toLowerCase(c); // Case insensitive lookup
return frequencyMap.getOrDefault(c, 0);
}

/**
* Returns the relative percentage of a character's occurrence in the input string.
* The percentage is calculated as (character's frequency / total characters) * 100.
* Returns the relative percentage of a character's occurrence in the input
* string.
* The percentage is calculated as (character's frequency / total characters) *
* 100.
*
* @param c the character to calculate the relative percentage for
* @return the relative percentage of the character's occurrence in the string
*/
public double getRelativePercentage(char c) {
c = Character.toLowerCase(c);
int charCount = frequencyMap.get(c);
int charCount = 0;
if (frequencyMap.get(c) != null) {
charCount = frequencyMap.get(c);
}

return ((double) charCount / totalCharacterCount) * 100;
}
Expand Down
20 changes: 19 additions & 1 deletion src/CharacterFrequencyCounterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,23 @@
import org.junit.jupiter.api.Test;

public class CharacterFrequencyCounterTest {

@Test
public void testLAppearsThreeTimesInHelloWorld() {
// Arrange
CharacterFrequencyCounter counter = new CharacterFrequencyCounter("hello world");
// Act
int actualCount = counter.getFrequency('l');
// Assert
assertEquals(3, actualCount);
}

@Test
public void testNonOccurringCharacterPercentageIsZero() {
// Arrange
CharacterFrequencyCounter counter = new CharacterFrequencyCounter("hi there");
// Act
double actualPercentage = counter.getRelativePercentage('z');
// Assert
assertEquals(0.0, actualPercentage, 0.0001);
}
}