A small CLI that reads a text file, counts occurrences of the uppercase letters A through K, sorts the results by frequency (ascending), and prints a clean vertical histogram plus a compact “Char Occurrences” summary.
Preview I/O without running the code: sample input/output screenshots are available on my portfolio at rsiddiq.com/software-design.
- Prompts for a filename at runtime, then scans the file line-by-line.
- Counts only the uppercase letters
A…Kand ignores all other characters. - Sorts the
(letter, count)pairs by count (ascending) while keeping letters aligned. - Prints:
- a “Char Occurrences” list (only letters with nonzero counts), and
- a vertical histogram built top-down with aligned levels.
Histogram/
├─ app/
│ ├─ src/
│ │ └─ main/java/
│ │ ├─ Driver.java # Entry point (creates Histogram and runs read → sort → display)
│ │ └─ Histogram.java # Core implementation
│ ├─ src/test/java/
│ │ └─ HistogramFullTest.java (JUnit 5)
│ ├─ tq1.txt # Sample inputs
│ ├─ tq2.txt
│ ├─ tq3.txt
│ ├─ tq4.txt
│ ├─ tq5.txt
│ └─ tq6.txt
├─ settings.gradle
├─ app/build.gradle
├─ gradlew / gradlew.bat
└─ gradle/wrapper/...
- State
char[] letters = "ABCDEFGHIJK".toCharArray()int[] counts = new int[letters.length]String filename
- Flow
readFileName()prompts:Input filename :.read()opens the given file and incrementscounts[i]whenever a character matchesletters[i].sort()bubble-sorts bycountsascending, keepinglettersin sync.display()prints:- “Char Occurrences” (only nonzero counts), and
- a vertical histogram with levels from max count down to 1, then a bottom legend of letters.
Tip: Run the program from the
Histogram/directory so the sample files inapp/are easy to reference.
# From the Histogram/ directory:
mkdir -p out
javac app/src/main/java/*.java -d out
# Run the program
cd out
java Driver
# When prompted, enter a path to a file, e.g.:
# ../app/tq3.txtUpdate app/build.gradle to point to the correct main class:
application {
mainClass = 'Driver'
}Then run:
# From the Histogram/ directory:
./gradlew run
# Enter a file path when prompted, e.g.:
# app/tq6.txtRun tests (JUnit 5):
./gradlew test- The program counts only
AthroughK(uppercase). - Lowercase letters, digits, punctuation, spaces, and any letters outside
A…Kare ignored. - Empty lines are safely skipped.
- Char Occurrences: lists only the letters that appeared, sorted from least frequent to most frequent.
- Histogram: printed top-down; a level number at left, with letters aligned in columns where their counts reach that level.
If you’d like to preview the look and feel of the output before running locally, sample I/O is posted on my site: rsiddiq.com/software-design.
- Separation of concerns: user input (
readFileName), parsing (read), ordering (sort), and presentation (display) are distinct, which keeps the code easy to test and modify. - Sorting keeps two parallel arrays in sync (counts and letters) for a straightforward, allocation-light approach.
- Output formatting uses
StringBuilderandprintffor tidy alignment.
Feel free to use, modify, and extend.