Java Programs
Writing complete, working programs — not just isolated code snippets — is how abstract concepts become real skills. A program has a clear purpose, hand...
Foundational Programs Every Java Learner Should Write
Writing complete, working programs — not just isolated code snippets — is how abstract concepts become real skills. A program has a clear purpose, handles input, processes data, and produces output, forcing you to make all the pieces work together. The following programs cover the fundamentals and are worth implementing yourself from scratch.
The classic calculator program accepts two numbers and an operator from the user and returns the result. It touches Scanner for input, conditional logic or a switch statement for operator dispatch, and input validation for division by zero. Once basic, extend it to handle chained operations and you introduce loops and state management.
A temperature converter between Celsius, Fahrenheit, and Kelvin is similarly foundational. It practices method decomposition — one method per conversion formula — and demonstrates why extracting logic into named methods makes code readable and testable.
A palindrome checker tests whether a string reads the same forwards and backwards. The naive approach uses a loop comparing characters from both ends. The concise approach uses StringBuilder.reverse() and compares. Both are worth writing to see the trade-off between clarity and brevity. Extend the program to ignore spaces and punctuation for a more realistic version: "A man a plan a canal Panama" is a palindrome when non-letter characters are stripped.
A Fibonacci sequence generator illustrates both iterative and recursive approaches. Write both, then compare them by timing their output for large inputs. The recursive version without memoization is exponentially slow; adding a HashMap to cache computed values makes it linear. This exercise concretely demonstrates why algorithm choice matters more than hardware for performance-critical code.
A student grade calculator reads student names and scores from a list, computes averages, assigns letter grades, and prints a formatted report. It exercises ArrayList, loops, String formatting with printf, and simple statistical calculations. This program type appears in many introductory courses because it touches a realistic domain with clear requirements.
Intermediate Java Programs for Skill Building
Once you are comfortable with basic programs, intermediate projects push you to use Java's standard library more deeply and think about program design — how to structure data, how to separate concerns, and how to write code that is easy to extend later.
A library book management system is a productive intermediate project. Model books with a Book class (title, author, ISBN, available status). Store books in a HashMap<String, Book> keyed by ISBN for fast lookup. Implement methods to add a book, check out a book (marking it unavailable), return a book, and search by title or author using stream filtering. Store the collection to a file using serialization or simple CSV format, and load it on startup. This project exercises OOP, collections, file I/O, and search — a comprehensive review.
A contact book application with persistent storage is similar in scope but models different data. Contacts have a name, phone number, and email. The key design challenge is deciding on a data structure that supports both fast lookup by name and efficient listing of all contacts alphabetically. A TreeMap<String, Contact> sorted by name handles both requirements elegantly.
A word frequency analyzer reads a plain text file and produces a ranked list of the most common words, excluding stop words (the, a, is, are, etc.). This program exercises file reading with BufferedReader, String.split() with regex to tokenize text, HashMap for counting, and sorting entries by value — which requires converting to a List of Map.Entry and sorting with a custom Comparator.
A simple task tracker that persists tasks to a JSON file (using a library like Gson or Jackson) introduces working with external libraries. Tasks have a description, priority, due date, and completion status. Implement add, complete, delete, and list-by-priority operations. This project introduces dependency management through Maven or Gradle, an essential skill for professional Java work.
When you finish any program, review it for extensibility. Could you add a new feature without changing existing methods? If not, consider whether the design should be restructured.
Programs Targeting Algorithms and Data Structures
Implementing classic data structures and algorithms in Java builds the deep understanding that separates programmers who can use libraries from those who understand how they work — and therefore know when to use them, when to avoid them, and how to adapt them.
Implement a stack using both an array and a LinkedList. The array-based stack preallocates capacity and tracks a top pointer; push increments the pointer and assigns the value, pop retrieves the value and decrements the pointer. The LinkedList-based stack uses a head node and is truly unbounded. Comparing the two reveals the space-versus-flexibility trade-off concretely.
Implement a binary search tree with insert, search, and in-order traversal methods. In-order traversal of a BST visits nodes in sorted order, which demonstrates why tree structure matters for algorithms. Extend it with a delete method — the most complex BST operation because deleting a node with two children requires finding the in-order successor.
Write both an iterative and recursive implementation of binary search on a sorted array. Binary search is O(log n) and the comparison with linear search is immediately visible even for arrays of a few hundred elements. Implement merge sort, which is a beautiful recursive algorithm that illustrates divide-and-conquer: split the array in half, recursively sort each half, then merge the two sorted halves.
Implement a simple HashMap from scratch using an array of linked lists (separate chaining) for collision resolution. Implement a put(key, value) method that computes a hash, maps it to a bucket, and either updates an existing entry or appends a new node. Implement get(key) to walk the bucket's chain. This exercise reveals exactly why HashMap provides amortized O(1) performance and what happens when the hash function distributes keys poorly.
One tip: after implementing any data structure, compare your implementation's performance to the corresponding Java standard library class using System.nanoTime() measurements on large datasets.