Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
26 views

Programming Assignment Unit 1

Uploaded by

cebeni8671
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Programming Assignment Unit 1

Uploaded by

cebeni8671
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Code:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class TextAnalysisTool {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

// User Input
System.out.println("Please enter a paragraph or a lengthy text:");
String text = scanner.nextLine();

// Character Count
int charCount = text.length();
System.out.println("Total number of characters: " + charCount);

// Word Count
String[] words = text.split("\\s+");
int wordCount = words.length;
System.out.println("Total number of words: " + wordCount);

// Most Common Character


char mostCommonChar = findMostCommonCharacter(text);
System.out.println("Most common character: " + mostCommonChar);

// Character Frequency
System.out.println("Please enter a character to find its frequency:");
char characterToFind = scanner.next().charAt(0);
int charFrequency = findCharacterFrequency(text, characterToFind);
System.out.println("Frequency of '" + characterToFind + "': " + charFrequency);

// Word Frequency
System.out.println("Please enter a word to find its frequency:");
scanner.nextLine(); // consume the leftover newline
String wordToFind = scanner.nextLine();
int wordFrequency = findWordFrequency(words, wordToFind);
System.out.println("Frequency of \"" + wordToFind + "\": " + wordFrequency);

// Unique Words
int uniqueWordsCount = findUniqueWordsCount(words);
System.out.println("Number of unique words: " + uniqueWordsCount);

scanner.close();
}

private static char findMostCommonCharacter(String text) {


Map<Character, Integer> charFrequencyMap = new HashMap<>();
for (char c : text.toCharArray()) {
char lowerC = Character.toLowerCase(c);
charFrequencyMap.put(lowerC, charFrequencyMap.getOrDefault(lowerC, 0) + 1);
}

char mostCommonChar = ' ';


int maxFrequency = 0;
for (Map.Entry<Character, Integer> entry : charFrequencyMap.entrySet()) {
if (entry.getValue() > maxFrequency) {
mostCommonChar = entry.getKey();
maxFrequency = entry.getValue();
}
}

return mostCommonChar;
}

private static int findCharacterFrequency(String text, char character) {


int frequency = 0;
char lowerCharacter = Character.toLowerCase(character);
for (char c : text.toCharArray()) {
if (Character.toLowerCase(c) == lowerCharacter) {
frequency++;
}
}
return frequency;
}

private static int findWordFrequency(String[] words, String word) {


int frequency = 0;
String lowerWord = word.toLowerCase();
for (String w : words) {
if (w.toLowerCase().equals(lowerWord)) {
frequency++;
}
}
return frequency;
}

private static int findUniqueWordsCount(String[] words) {


Map<String, Integer> wordFrequencyMap = new HashMap<>();
for (String word : words) {
String lowerWord = word.toLowerCase();
wordFrequencyMap.put(lowerWord, wordFrequencyMap.getOrDefault(lowerWord, 0) +
1);
}
return wordFrequencyMap.size();
}
}

Explanation:
1. User Input: The program first prompts the user to input a paragraph or lengthy text and
stores it.
2. Character Count: The length of the text is calculated using text.length().
3. Word Count: The text is split into words using split("\\s+"), and the length of the
resulting array gives the word count.
4. Most Common Character: A frequency map is built for all characters, and the most
frequent character is found.
5. Character Frequency: The program prompts the user to enter a character and calculates
its frequency in the text.
6. Word Frequency: The program prompts the user to enter a word and calculates its
frequency in the text.
7. Unique Words: A frequency map is built for all words, and the number of unique words
is determined.
Screenshot:

You might also like