CS 1103 Programming Assignment Unit 1
CS 1103 Programming Assignment Unit 1
Timothy J Reid
CS 1103: Programming 2
Alejandro Lara
Introduction
In this assignment, I will create a text analysis tool that performs various operations on a
given text input. This tool will help users gain insights into their text data by performing
character and word analysis. The operations include calculating the total number of characters
and words, finding the most common character, determining character and word frequency, and
Explanation of Code
1. User Input: The program begins by prompting the user to input a paragraph or a lengthy text.
The input is validated to ensure it is not empty. If the user enters an empty text, the program will
2. Character Count: The program calculates the total number of characters in the input text
using the `length()` method of the `String` class and displays the result (Eck, 2022).
3. Word Count: The input text is split into words using the `split("\\s+")` method, which splits
the text based on whitespace. The total number of words is then calculated and displayed (Eck,
2022).
2
4. Most Common Character: The program uses a `HashMap` to count the frequency of each
character in the text, converting the text to lowercase and ignoring non-alphanumeric characters.
5. Character Frequency: The user is asked to input a character. The program calculates the
both the text and the character to lowercase, and displays the result (Morelli & Wade, n.d.).
6. Word Frequency: The user is asked to input a word. The program calculates the frequency of
occurrences of this word in the text, ensuring case-insensitivity, and displays the result (Morelli
7. Unique Words: The program uses a `HashSet` to store unique words from the text, ensuring
case-insensitivity by converting the text to lowercase. The count of unique words is then
Conclusion
This text analysis tool demonstrates the basics of string handling and text analysis in Java. By
implementing character and word counts, frequency analysis, and identifying unique words, I have
reinforced my understanding of string manipulation and data analysis techniques. Now, I can see how
tools like this are commonly used in word processors to provide features like word count and character
count, and in social media analytics to analyze posts and trends. This hands-on experience has shown me
the practical applications of Java programming concepts and how they are used to create useful utilities
for text data analysis. Additionally, the program includes input validation to handle cases where the user
might not enter any data, ensuring it runs smoothly without errors.
4
Reference
Eck, D. J. (2022). Introduction to Programming Using Java, Version 9, JavaFX Edition. Hobart
https://math.hws.edu/javanotes/](https://math.hws.edu/javanotes/
Morelli, R., & Wade, R. (n.d.). Exceptions - When Things Go Wrong. LibreTexts. Licensed
under CC 4.0.
Java Code:
import java.util.*;
// Character Count
int characterCount = inputText.length();
System.out.println("Total number of characters: " + characterCount);
// Word Count
String[] words = inputText.split("\\s+");
int wordCount = words.length;
System.out.println("Total number of words: " + wordCount);
// Character Frequency
System.out.println("Enter a character to find its frequency:");
String charInput = scanner.nextLine().toLowerCase();
if (charInput.isEmpty()) {
System.out.println("No character entered.");
} else {
char characterToCheck = charInput.charAt(0);
int charFrequency = getCharacterFrequency(inputText,
characterToCheck);
System.out.println("Frequency of '" + characterToCheck + "': " +
charFrequency);
}
6
// Word Frequency
System.out.println("Enter a word to find its frequency:");
String wordToCheck = scanner.nextLine().toLowerCase();
int wordFrequency = getWordFrequency(inputText, wordToCheck);
System.out.println("Frequency of \"" + wordToCheck + "\": " +
wordFrequency);
// Unique Words
int uniqueWordCount = getUniqueWordCount(inputText);
System.out.println("Number of unique words: " + uniqueWordCount);
scanner.close();
}