CSE131 Code Examples

This page contains links to the code for some of the lengthier examples discussed in class over the years. Feel free to save the example code to a file and experiment with running and modifying it.
DoublyLinkedList.java
A doubly linked list implementation that uses parameterized types (Java Generics) and provides an iterator. Also illustrates a simple use of inner classes.

ListMerge.java
Two ways to merge sorted lists using an iterator.

Rectangle.java
Illustrates encapsulation, constructors, and the toString method.

Triangles.java
A complete program illustrating the use of procedural abstraction.

Couple.java
A class definition for objects containing a couple's names and year married, with accessor methods to extract that information.

Account.java
This class defines a bank account object with deposit, withdraw, and transfer methods.

Temperature.java
This class defines an object that keeps track of the current temperature, high, and low temperatures, and converts between Fahrenheit and Celsius.

Sphere.java
Represents a sphere in 3-space, with move and resize methods, as well as a method to determine if a point is within the sphere.

Scorekeeper.java
A device that keeps track of the current "count" in a baseball game.

Rational.java
A rational number class, supporting rational number arithmetic.

Relation.java
An implementation of a relation between domain and range objects. Each domain element occurs at most once.

ListOfInts.java
Implementation of a list of ints, including methods for appending and prepending, as well as traversing, inserting, and deleting using a marker.

Stacks and Queues
Implementations of stacks and queues using linked lists of ListItem objects.

ListOfObjects.java
Implementation of a general list that can contain any type of Object as data. An Iterator is also provided.

More on Stacks and Queues
Simplified Implementations of stacks and queues using an encapsulated ListOfObjects.

Circular Lists
Implementation of a circular list ADT, along with implementations of stack and queue in terms of circular lists.

Base Conversion
Implementation of a BaseNumber ADT that allows conversion between positive integers in different bases. A list of digits, along with the integer base, is used as the internal representation. Bases are assumed to be in the range 2-10.

The Stable Marriage Problem (Spring 2001 IMPLEMENTATION)
Implementation of a matching algorithm for men and women, each with rank-ordered lists of who they would like to marry. The final arrangement must be "stable" meaning that no two people prefer each other over their own spouses. In this implementation, rankings are generated randomly. A variation of this algorithm is used to match medical school graduates to hospitals.

The Stable Marriage Problem (OLD IMPLEMENTATION)
Implementation of a matching algorithm for men and women, each with rank-ordered lists of who they would like to marry. A variation of this algorithm is used to match medical school graduates to hospitals. The Relation class is used to keep track of the engagements.

Quicksort
A recursive implementation of quicksort using an array of integers. This example prints out the intermediate contents of the array after each partition. A few test cases are included.
Collections classes demo
This inefficient collision detection demonstration illustrates some of the collections classes available in java.util.