CS101 Code Examples

This page contains links to the code for some of the examples discussed in class. Feel free to save the example code to a file and experiment with running and modifying it.
IntSet.java
An abstract definiiton of a set of integers. Subclasses then make this more concrete:
ListBasedIntSet
makes int sets using Lists. The concrete class UnOrdListIntSet uses unordered lists.
Relation.java
A generic Relation ADT. This uses DomainRangeList.java to manage a list of DomainRange pairs. The Domain and Range are wrappers of the generic Object type

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.

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
Implementation of a matching algorithm for men and women, each with rank-ordered lists of who they would like to marry. The 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.