0% found this document useful (0 votes)
3 views4 pages

Programming Assignment Unit 1

The document describes a Java program that analyzes text input by counting characters and words, identifying the most common character, and calculating the frequency of specified characters and words. It also determines the number of unique words in the input. The program uses various Java classes and methods, including Scanner, HashMap, and HashSet, to perform these tasks.

Uploaded by

kamalsami734
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Programming Assignment Unit 1

The document describes a Java program that analyzes text input by counting characters and words, identifying the most common character, and calculating the frequency of specified characters and words. It also determines the number of unique words in the input. The program uses various Java classes and methods, including Scanner, HashMap, and HashSet, to perform these tasks.

Uploaded by

kamalsami734
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Programming Assignment Unit 1

import [Link].*;

import [Link].*;

public class TextAnalysisTool {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

// User Input

[Link]("Please enter a paragraph or a lengthy text:");

String text = [Link]();

// Character Count

int charCount = [Link]();

[Link]("Total number of characters: " + charCount);

// Word Count

String[] words = [Link]("\\s+");

int wordCount = [Link];

[Link]("Total number of words: " + wordCount);

// Most Common Character

Map<Character, Integer> charFrequency = new HashMap<>();

for (char c : [Link]().replaceAll("\\s", "").toCharArray()) {

[Link](c, [Link](c, 0) + 1);

}
char mostCommonChar = [Link]([Link](),
[Link]()).getKey();

[Link]("Most common character: '" + mostCommonChar + "'");

// Character Frequency

[Link]("Enter a character to find its frequency:");

char charToSearch = [Link]().toLowerCase().charAt(0);

int charFrequencyCount = (int) [Link]().chars().filter(c -> c ==


charToSearch).count();

[Link]("Frequency of character '" + charToSearch + "': " + charFrequencyCount);

// Word Frequency

[Link]("Enter a word to find its frequency:");

[Link](); // consume newline

String wordToSearch = [Link]().toLowerCase();

long wordFrequencyCount = [Link](words).map(String::toLowerCase).filter(word ->


[Link](wordToSearch)).count();

[Link]("Frequency of word '" + wordToSearch + "': " + wordFrequencyCount);

// Unique Words

Set<String> uniqueWords = new HashSet<>([Link](words));

[Link]("Number of unique words: " + [Link]());

[Link]();

Explanations

1. User Input:

java
Copy code
[Link]("Please enter a paragraph or a lengthy text:");
String text = [Link]();
Explanation: This section prompts the user to input a paragraph or text. It uses
Scanner to read the text from the console.

2. Character Count

java
Copy code
int charCount = [Link]();
[Link]("Total number of characters: " + charCount);

Explanation: This counts and displays the total number of characters in the input text
using the length() method of the String class.

3. Word Count

java
Copy code
String[] words = [Link]("\\s+");
int wordCount = [Link];
[Link]("Total number of words: " + wordCount);

Explanation: This splits the text into words based on whitespace and counts the
number of words using split() and length.

4. Most Common Character

java
Copy code
Map<Character, Integer> charFrequency = new HashMap<>();
for (char c : [Link]().replaceAll("\\s", "").toCharArray()) {
[Link](c, [Link](c, 0) + 1);
}
char mostCommonChar = [Link]([Link](),
[Link]()).getKey();
[Link]("Most common character: '" + mostCommonChar + "'");

Explanation: This creates a frequency map for characters, then finds the most
common character using [Link]().

5. Character Frequency

java
Copy code
[Link]("Enter a character to find its frequency:");
char charToSearch = [Link]().toLowerCase().charAt(0);
int charFrequencyCount = (int) [Link]().chars().filter(c -> c ==
charToSearch).count();
[Link]("Frequency of character '" + charToSearch + "': " +
charFrequencyCount);
Explanation: This counts how many times a user-specified character appears in the
text using chars().filter().

6. Word Frequency

java
Copy code
[Link]("Enter a word to find its frequency:");
[Link](); // consume newline
String wordToSearch = [Link]().toLowerCase();
long wordFrequencyCount =
[Link](words).map(String::toLowerCase).filter(word ->
[Link](wordToSearch)).count();
[Link]("Frequency of word '" + wordToSearch + "': " +
wordFrequencyCount);

Explanation: This counts how many times a user-specified word appears in the text,
ignoring case differences.

7. Unique Words

java
Copy code
Set<String> uniqueWords = new HashSet<>([Link](words));
[Link]("Number of unique words: " + [Link]());

Explanation: This finds the number of unique words by converting the array of words
into a Set, which automatically removes duplicates.

References:

Eck, D. J. (2022). Introduction to programming using java version 9, JavaFX edition. Licensed under CC
4.0. [Link]

You might also like