Skip to content

Java Gradle project building letter-frequency histograms from text files; demonstrates file I/O, arrays, collections, sorting, exceptions, encapsulation, iteration constructs, parameterization, and thorough JUnit 5 testing.

Notifications You must be signed in to change notification settings

r-siddiq/Histogram

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Histogram (A–K) Frequency Visualizer

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.


Highlights

  • Prompts for a filename at runtime, then scans the file line-by-line.
  • Counts only the uppercase letters AK and 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.

Project Structure

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/...

How It Works

  • State
    • char[] letters = "ABCDEFGHIJK".toCharArray()
    • int[] counts = new int[letters.length]
    • String filename
  • Flow
    1. readFileName() prompts: Input filename :.
    2. read() opens the given file and increments counts[i] whenever a character matches letters[i].
    3. sort() bubble-sorts by counts ascending, keeping letters in sync.
    4. 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.

Usage

Tip: Run the program from the Histogram/ directory so the sample files in app/ are easy to reference.

Option A: Plain javac / java

# 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.txt

Option B: Gradle

Update 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.txt

Run tests (JUnit 5):

./gradlew test

Input Expectations

  • The program counts only A through K (uppercase).
  • Lowercase letters, digits, punctuation, spaces, and any letters outside AK are ignored.
  • Empty lines are safely skipped.

Example

  • 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.


Design Notes

  • 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 StringBuilder and printf for tidy alignment.

✅ License / Usage

Feel free to use, modify, and extend.

About

Java Gradle project building letter-frequency histograms from text files; demonstrates file I/O, arrays, collections, sorting, exceptions, encapsulation, iteration constructs, parameterization, and thorough JUnit 5 testing.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages