Sample Programs

Advanced Programming/Practicum
15-200: Sections A,B,G
Spring 2006


The following is a list of zipped Eclipse programming project files (more might be added during the semester). Please feel free to download, unzip, and study these programs (both their code and their run-time behavior). Programmers gain tremendous insight into all facets of programming by studying the code of other programmers (especially those with more experience; and I am happy to improve my code based on your observations -nothing is perfect). A good programmer typically makes elegant use of the required language features, resulting in smaller, more elegant code.

All downloaded projects are listed alphabetically. All are zip files, so unzip them first. Most project files require the standard course library; if you followed the Eclipse installation instructions, you should be fine with this jar file (reread the direction if necessary).


Download: Array Demonstration
This program shows how to declare, initialize, and manipulate simple arrays of int/String values using a variety of for loops.

Download: 2-D Array Demonstration
This program shows how to declare, initialize, and manipulate simple 2-d (rectangular) arrays of int values using a variety of for loops.

Download: Backtracking
These programs use a generalized backtracking search to search an implicit game tree. See the backTrack package, which contains the Problem interface, and the Solver class, which solves any problem expressible by a class implementing this interface. The NQueens package contains a NQueens class and a simple application that uses Solver to solve it (how to place N queens on a chessboard so that none can attack another). The Satifiability package contains a Satisfiability class and a simple application that uses Solver to solve it (how to assign T/F values to variables in a formula to satisfy it -make it evaluated to true). The Sudoku package contains a Sudoku class and a simple application that reads a Sudoku puzzle and uses Solver to solve it (how to assign 1-9 values to cells in a Sudoku puzzle to satisfy its constraints). Download: Bouncing Balls
The program is implemented using the Model-View-Controller pattern. It simulate balls bouncing in a box. The user can create new balls by clicking in the box. The user also can press buttons to start/stop/reset the simulation.

Download: Class Examples
This program illustrates how to construct and use (call methods on) objects from a variety of simple classes: DiceEnsemble, StringTokenizer, Timer, TypedBufferReader, DecimalFormat, BigInteger, Integer, and String.

Download: Color Calculator
The program is implemented using the Model-View-Controller pattern. It allows us to see a color (and its hexidecimal value) as we change its red, green, and blue components in the range [0..255]. These colors are all represented by ModularCounters, and the Model class has a main method so that it can be tested independently form the Controller and View. Note, the hex value can be used in HTML to specify a color.

Download: Collatz Conjecture
The Collatz conjecture states: starting with any positive number, repeat the process "If it is even, halve it; if it is odd, multiply it by three and add one" and eventually the remaining value reduces to one. No one has been able to prove this conjecture, but a short program allows you to investigate it. It is interesting to obseve the number of cycles required to reduce the starting number to 1, looking for a pattern. This program uses the BigInteger class to allow arbitrarily large values (not restricted to the bounded type int) and the Timer class to keep track of the processing time.

Download: Collatz Conjecture FOR DEBUGGER HANDOUT
The Collatz conjecture states: starting with any positive number, repeat the process "If it is even, halve it; if it is odd, multiply it by three and add one" and eventually the remaining value reduces to one. No one has been able to prove this conjecture, but a short program allows you to investigate it. It is interesting to obseve the number of cycles required to reduce the starting number to 1, looking for a pattern. Unlike the program above, this is a simpler one, using the int type, useful for illustrating the debugger.

Download: Controller Demonstrations
Demonstrations of using Controllers in the MVC pattern. The mouse project illustrates all mouse controllers (each associated with a trivial behavior). The grabbag project illustrate many other controllers: JButton, JTextField, JRadioButton, JTextArea, JScrollPane, RadioButtonPanel, and SliderPanel.

Download: Craps Statistics
This program prompts the user for the number of craps games to play (craps is a dice game whose rules are explained in the opening comment in the Application.java file). It then simulates that many games of craps, printing the number of wins, losses, dice throws, elapsed time, and playing speed. This program uses the DiceEnsemble and Timer classes.

Download: Craps Statistics FOR DEBUGGER HANDOUT
This program prompts the user for the number of craps games to play (craps is a dice game whose rules are explained in the opening comment in the Application.java file). It then simulates that many games of craps, printing the number of wins, losses, dice throws, elapsed time, and playing speed. Unlike the program above, this is a simpler one, not using classes, useful for illustrating the debugger.

Download: Date Calculator #1
A small program for for computing some information about dates. This program illustrates how to use five static methods and twelve static fields written directly in the Application class.

Download: Date Calculator #2
A small program for for computing some information about dates. This program illustrates how to use five static methods and twelve static fields written in the DateUtility class (included in this project folder). When learning to write classes (that are just libraries of static methods), this is a good place to start.

Download: DiceEnsemble Demonstration
A driver program for testing the DiceEnsemble class (from the 15-1XX library).

Download: Directory Lister
A program that lists the contents of any folder and computes statistics about its subfolders and files. This program uses Java's File class (defined in the java.io package), which represents an N-ary tree by using arrays. These N-ary trees are processed by both iteration and recursion.

Download: Expression Trees
A program that reads an expression in standard Java form (literals only, no variables) and then builds an expression tree for it (using parentheses and operator precedence to guide it) and then evaluates the tree and prints its postfix form. It uses a SimpleStack as an critical data structure.

Download: Expression Trees (Extended)
A program that reads an expression in standard Java form (BigInteger literals and variables) and then builds an expression tree for it (using parentheses, operator precedence, and associativity to guide it) and then evaluates the tree and prints its postfix form (and shows the variable Map). It uses a SimpleStack as an critical data structure.

Download: File Input
A simple program illustrating the pattern used to read input files (including a try-catch statment to terminate the file reading loop when there is no more input). This program actually recovers from errors on a line by line basis, as described in the lecture on the TypedBufferReader class for Java file input.

Download: Interface Demonstration
A simple program illustrating two Java interfaces DecisionInt and Univariate and how they are used, as described in the lecture on the Interfaces. This is the beginning of real object-oriented programming. The name of the interface is uses as a parameter type in a method, and many classes are defined to implement that interface.

Download: Java 1.5 Examples
Illustrates various features new to Java 1.5: extended for loops, autoboxing, var-args, c-style formatting, and using generic collection class.

Download: JUnit: Stack Test
Uses JUnit to test a stack class (which is correct). I recommend that you insert bugs into the stack class and see if they are diagnosed (so you will get familiar with interpreting errors from JUnit. See also the Course Software download JUnit Testing Framework (reachable via the Online Resources link).

Download: Layout Manager Demonstrations
Demonstrations for four standard Layout Managers: FlowLayout, BoxLayout, GridLayout and BorderLayour. Also includes a demonstration of recursively combining these JPanels and their layouts. Finally, includes a demonstration TwoColumnLayout, showing how to write new, simple layout managers.

Download: Minimum Distance between Points: 3 Algorithmis
A driver program for using the three algorithms on arrays of randomly generated Point2D.Doubles. The brute force algorithm is O(N^2), the sorting/recursive algorithm is O(NlogN), and the hash algorithm is O(N), although empirically it runs more slowly than the sort/recursive algorithm.

Download: ModularCounter Demonstration
A driver program for testing the ModularCounter class (from the 15-1XX library).

Download: Object-Oriented List Demo
A driver program for testing Object-Oriented Linked Lists. Included are interfaces for list algorithms, the declaration of the classes needed to implement linked lists, and various classes that specify algorithms. Uses the singleton and strategy pattern. See also Patterns for Decoupling Data Structurs and Algorithms

Download: Ordered Collection Demonstration
A driver program for testing all the ordered collection classes: ArrayStack, ListStack, ArrayQueue, ListQueue, ArrayPriorityQueue, and ArrayUnsortedPriorityQueue. All the relevant interfaces, abstract classes, and classes are included in this driver, which can generically test each of these concrete classes.

Download: Positional Shape Inheritance Demonstration
A program using abstract classes and inheritance. The classes in this program is described in the first design in the Abstract Classes lecture.

Download: Positional Shape Inheritance Demonstration 2
A program using interfaces, abstract classes, and inheritance. The classes in this program is described in the second design in the Abstract Classes lecture.

Download: Rational Demonstration
A driver program for testing the Rational class (from the 15-1XX library).

Download: Reflective Test Driver
Using Java's powerful reflection mechanism, this test driver allows you to test any Java class! Just enter its full name (prefixed by its package) and then you'll get to choose a constructor (and arguments) and repeatedly be able to call methods on it (and supply arguments). Truly amazing!

Download: SimpleDiceEnsemble Demonstration
A driver program for testing the SimpleDiceEnsemble class (a slightly simpler version than the one in the 15-1XX library; it doesn't use arrays and has no getPips method) which is discussed in the Writing Classes lecture.

Download: SimpleQueue Demonstration
A driver program for using the SimpleQueue collection class described in the lecture on the 1-d Arrays.... It declares an instance variable that is an array that grows to acccomodate all the required references stored in the queue. Queues support first-in/first-out behavior.

Download: SimpleQueue with Iterator Demonstration
A driver program for using the SimpleQueue collection class described first in the lecture on the 1-d Arrays... and then modified to include iterators in the lecture Collection Classes: Basics. It declares an instance variable that is an array that grows to acccomodate all the required references stored in the stack. Queues support first-in/first-out behavior. The menu has been enlarged to include testing the iterator inner-class.

Download: SimpleStack Demonstration
A driver program for using the SimpleStack collection class described in the lecture on the 1-d Arrays.... It declares an instance variable that is an array that grows to acccomodate all the required references stored in the stack. Stacks support last-in/first-out behavior.

Download: size in BST
A solution to a problem even too simple to put on the programming exam, for computing the size of a BST using the classes appearing on the second programming exam.

Download: Sorting (with Interfaces) Demonstration
A driver program for using the Arrays.sort method on arrays of Points. It includes classes that implement the Comparator interface, for sorting by X coordinate, by Y coordinate, by distance from the origin, and by angle from the origin (in all cases, both increasing and decreasing through the use of the ReverseAComparator decorator.

Download: Sorting Methods Demonstraction
This program is a driver for the sorting methods in the Sort class (included in this download). It allows the testing and timing of all these sort methods, and Arrays.sort. It also contains an executable, that you can run immediately.

Download: Supermarket Simulation
This program simulates a supermarket using queues (for the checkout lines) and priority queues (for the scheduling events; the event to happen most closely in the future has the highest priority). The simulated supermarket is specified by an array storing the maximum number of items that can be checked-out in each line. The events are "enter the store and start shopping" (at random intervals), "enter a checkout line" (based on the number of items bought, the sizes of the lines, and how many items are allowable), start checkout process (keep track of shopper's wait time: from entering a line to checking out), and "exit store". The unhappiness of a custom is computed (line wait time/shop time) and an average over all shoppers is reported. The program also reports the running time and simulation speed (# of events processed/second).

Download: Thread Demonstrations
This folder contains four small programs that demonstrate threads: Thread/Runnable illustrate two ways to use threads. Timer illustrates how to use an action listener which is activated regularly. Animation shows how such a time can be used to run a tiny GUI animation.

Download: View Demonstrations
Demonstrations of using JFrames and JPanels. The JFrame demonstration shows how to create, position, size, and label windows. The JPanel demonstrations show how to draw figures, put text, put icons (and animate them), and put buttons into JPanels.

Download: State Space Search
These programs use a generalized state-space searching to solve various various problems involving applying operators. See the stateSpace package, which contains the Problem interface, and the Solver class, which solves any problem expressible by a class implementing this interface. The puzzleNxN package contains a PuzzleNxN class and a simple application that uses Solver to solve it (how to acheive a standard puzzle configuration). The waterJugs package contains a WaterJugs class and a simple application that uses Solver to solve it (how to achieve a certain configuration).

Download: X (cross) Reference
This program produces a cross-reference list of any text file. It uses the map and list collection classes to do so: the Application class uses non-generic collections (pre Java 1.5) and the GenericApplication class uses Java 1.5 Generic collections (and one Java 1.5 for loop). The download includes a complete copy of Mark Twain's "Tom Sawyer" and the correct output for this input.

Download: Face Flasher