Project 4 – Sorted Linked List (Java) Author: Reed McLean
Overview:
Reads integers from standard input, stores them in a java.util.LinkedList, sorts ascending using Collections.sort, and prints the result as a single space-separated line. Runs from the command line (no IDE required).
**Files in this submission:
- SortedLinkedList.java – source code (uses Java Collections Framework)
- README.md – this file
- docs/ – generated Javadoc output (folder)
- input.txt – sample test data (For grading/reference purposes)
Code Reuse (examples):
Standard Library Reuse: Uses java.util.LinkedList and Collections.sort(...) from the Java Collections Framework rather than writing custom list or sort logic.
Reusable Input Utility: readIntegersFromStdIn() cleanly encapsulates token scanning and integer parsing; it can be reused unchanged in future assignments that read integer streams.
Reusable Output Utility: printSpaceSeparated(List) prints any List as a single space-separated line and can be reused by later modules (the assignment notes this “infrastructure” will be reused).
Result: the program composes small, general helpers + library types, demonstrating reuse over re-implementation.
- How to compile:
Run:
javac SortedLinkedList.java
- How to run (choose one)
a. With a txt file (input.txt file included for reference of testing "5 3 8 9 -1 14 -6"):
Run:
java SortedLinkedList < input.txt
b. With inline values:
Run:
echo "5 -3 8 9 -1 14 -6" | java SortedLinkedList
c. Interactive (type numbers, then end input):
*Git Bash / WSL / macOS / Linux: press Ctrl+D to finish. *Windows CMD/PowerShell: press Ctrl+Z then Enter.
Run:
java SortedLinkedList
- Javadoc (required command) - Generate API docs into the docs/ folder
Run:
javadoc -d docs -author -version SortedLinkedList.java
*Open docs/index.html in a browser to view.
- Input format
Run:
java SortedLinkedList < input.txt
*Any whitespace-separated integers from standard input. *Non-integer tokens are safely ignored. Duplicates are preserved. Negatives allowed.
*Example input.txt
10 2 2 8 -1 hello 4 4 4 9 0 0
*Expected output:
-1 0 0 2 2 4 4 4 8 9 10
- Troubleshooting
*bash: input.txt: No such file or directory: -Create input.txt in the same folder you run the command from, or provide a full path.
*Could not find or load main class: -Ensure you compiled successfully and you’re running the command in the directory that contains SortedLinkedList.class. The class name and file name must match exactly.
*No output shown: -When running interactively, you must signal end-of-input (Ctrl+D or Ctrl+Z then Enter).
- Design (how it works)
Data flow: STDIN → Scanner → LinkedList → Collections.sort(...) → printSpaceSeparated(...) → STDOUT
Steps:
-
Read tokens from standard input with Scanner. Non-integers are skipped, integers are appended to a LinkedList.
-
Sort the list in ascending order using Collections.sort.
-
Print the sorted list as a single space-separated line (no trailing space).
Reasons for LinkedList + Collections.sort:
-It satisfies the requirement for using the Collections Framework -It keeps the logic focused on composition (read → store → sort → print) rather than reinventing algorithms.
+---------+ +---------+ +-----------------+ +----------------------+ | STDIN | ---> | Scanner | ---> | LinkedList | ---+-> | Collections.sort(...)| +---------+ +---------+ +-----------------+ | +----------------------+ | v printSpaceSeparated(list) ---> STDOUT