0% found this document useful (0 votes)
7 views

CS1103 Assignment 1

The assignment for CS 1103-01 required writing a program to analyze text input by counting characters, identifying the most common character, and determining the frequency of specific characters and words. The student faced challenges and was unable to complete the program due to bugs. The provided code includes functionalities for character and word counting, frequency analysis, and identifying unique words.

Uploaded by

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

CS1103 Assignment 1

The assignment for CS 1103-01 required writing a program to analyze text input by counting characters, identifying the most common character, and determining the frequency of specific characters and words. The student faced challenges and was unable to complete the program due to bugs. The provided code includes functionalities for character and word counting, frequency analysis, and identifying unique words.

Uploaded by

alexcreationz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

CS 1103-01 Programming 2

Assignment Unit 1

Assignment for unit one this week was to write a program to solve the solve a problem of

counting number of characters in a text provided by a user, check for the most used character,

then gets a character from the user and provide its frequency of use. This was really a challenge I

try my best but could not complete my work as there were bugs in my program. The following is

my code.

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
public class Analyzer {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

// Task 1: User Input


System.out.print("Please enter your text: ");
String text = scanner.nextLine();

// Task 2: Character Count


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

// Task 3: Word Count


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

// Task 4: Most Common Character


Map<Character, Integer> charFrequency = new HashMap<>();
for (char c : text.toLowerCase().toCharArray()) {
if (c != ' ') {
charFrequency.put(c, charFrequency.getOrDefault(c, 0) +
1);
}
}
char mostCommonChar = ' ';
int maxFrequency = 0;
for (Map.Entry<Character, Integer> entry :
charFrequency.entrySet()) {
if (entry.getValue() > maxFrequency) {
maxFrequency = entry.getValue();
mostCommonChar = entry.getKey();
}
}
System.out.println("Most common character: " + mostCommonChar);

// Task 5: Character Frequency


System.out.print("Please enter a character to find its
frequency: ");
char charToFind = scanner.nextLine().toLowerCase().charAt(0);
int charFreq = 0;
for (char c : text.toLowerCase().toCharArray()) {
if (c == charToFind) {
charFreq++;
}
}
System.out.println("Frequency of '" + charToFind + "': " +
charFreq);

// Task 6: Word Frequency


System.out.print("Please enter a word to find its frequency:
");
String wordToFind = scanner.nextLine().toLowerCase();
int wordFreq = 0;
for (String w : words) {
if (w.equals(wordToFind)) {
wordFreq++;
}
}
System.out.println("Frequency of '" + wordToFind + "': " +
wordFreq);

// Task 7: Unique Words


int uniqueWords = new HashSet<>(Arrays.asList(words));
int uniqueWordCount = uniqueWords.size( );
System.out.println("Number of unique words: " +
uniqueWordCount);
}
}

Most utility used were imported into the java file to aid with the implementation of the code

My out put result.


Reference

Eck, D. J. (2022). Introduction to programming using java version 9, JavaFX edition. Licensed

under CC 4.0. Use the Introduction to Programming Using Java for pdf version of the file.

You might also like