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.
An interface for elements in a set
An extension of Card from Lab 7 that can serve as a set element.
An interface that any set should implement
An implementation based on linked-lists.
A generic Relation ADT. This uses ListOfObjects.java to manage a list of DomainRange containers.

A complete program illustrating the use of procedural abstraction.

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

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

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

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.

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

A rational number class, supporting rational number arithmetic.

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

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.

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.