Programming Assignment Unit 1
Programming Assignment Unit 1
CS 1103-01 - AY2025-T2
2. Programming Assignment Unit 1
Assignment Instructions
Scenario: You have been asked to create a text analysis tool that will perform
various operations on a given text input. This tool will help users gain insights
into the text data by performing character and word analysis.
Assignment Tasks:
1. User Input: Ask the user to input a paragraph or a lengthy text. Your
program should read and store this input.
3. Word Count: Calculate and display the total number of words in the
input text. Assume that words are separated by spaces.
4. Most Common Character: Find and display the most common
character in the text. In case of a tie, select any of the tied characters.
6. Word Frequency: Ask the user to input a word. Check and display the
frequency of occurrences of this word in the text. Be case-insensitive.
Submission Instructions
Read the rubric on how you are going to be graded before you start to
work on this assignment.
6. Code style and readability - Refers to the way the code is written,
organized, and presented. It focuses on making the code clear, easy to
understand, and maintainable.
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
// Character Count
int charCount = text.length();
System.out.println("Total characters: " + charCount);
// Word Count
String[] words = text.split("\\s+");
int wordCount = words.length;
System.out.println("Total words: " + wordCount);
// Word Frequency
System.out.print("Enter a word to find its frequency: ");
String wordToFind = scanner.next().toLowerCase();
int wordFreq = 0;
for (String word : words) {
if (word.equalsIgnoreCase(wordToFind)) {
wordFreq++;
}
}
System.out.println("Frequency of '" + wordToFind + "': " + wordFreq);
// Unique Words
Set<String> uniqueWords = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
for (String word : words) {
uniqueWords.add(word);
}
System.out.println("Number of unique words: " + uniqueWords.size());
scanner.close();
}
}
--------------------------------------------------------------
Hey there! Alaa here, ready to explain this awesome text analyzer code we have in front of us.
This code is like a detective for your text, finding out all sorts of interesting things about it. Let's
break it down step by step, shall we?
1. Getting Ready:
First things first, we need some tools to work with. We import a bunch of helpful things from
java.util. Think of them like a toolbox with a scanner (to read what you type), maps (to store
information), and sets (like a collection without duplicates). We also define a class called
TextAnalyzer - that's like our workspace for this detective work.
Now, comes the fun part! We use the scanner to ask you for some text. It can be a paragraph, a
story, or even a song lyric - anything you want to analyze. We store this text in a variable called
text.
3. Counting Characters:
Time to put on our detective hat! First, we want to know the total number of characters in the
text. We use the handy length() method on the text variable. It's like counting each letter,
space, and punctuation mark. We then print this number out, letting you know how long the text
is.
Next up, we want to see how many words are hiding in that text. We use a method called
split() on the text variable. This method acts like a pair of scissors, cutting the text into
separate words based on spaces. We store these words in an array called words. Then, we use the
magic of length() again, but this time on the words array, to see how many elements (words) it
has. This tells us the total number of words in the text. We print that out too!
We create a Map called charFreq. Maps are like fancy notebooks where we can keep
track of things. In this case, we'll use the characters as keys and a number (their
frequency) as values.
We loop through each character in the text (using toCharArray() to convert it into
individual characters). For each character, we check if it's already in our charFreq map.
If it is, we add 1 to its existing frequency (like marking another appearance in our
detective notes). If it's a new character, we add it to the map with a frequency of 1 (its
first appearance).
After looping through all characters, we find the champion - the most frequent character!
We loop through the charFreq map again, comparing frequencies. If a character's
frequency is higher than the current champion, we update the champion and its
frequency. Finally, we print out the most common character, along with its frequency.
Now, suppose you have a hunch about a particular character. Maybe you noticed a lot of "e"s
while reading the text? We can use our detective skills to find the exact frequency of any
character you want!
Similar to finding a specific character, we can also investigate the frequency of a specific word
in the text.
I hope this explanation is clear and helpful! Let me know if you have any other questions.
References and sources
1- bronze badges. (1959b, December 1). Java - string input exception handling.
Stack Overflow. https://stackoverflow.com/questions/26279208/java-string-input-
exception-handling