[ Home | Suzanne Menzel | Sun Java Docs | 2003 Session 1 | DrJava ]

Session 1: Collections

In this session we will demo the DrJava programming environment and cover some of the Collection classes in the context of an adventure game. We will also discuss a few teaching stategies and experiment with the Classroom Participation System (CPS) from eInstruction.

Source Files: [ Heart | Adventurer | Room | BackPack | World | Util | glass.au | CPS Lesson ]

DrJava Development Environment

We'll use the latest development release of DrJava as our development environment.

Text-Based Adventure Game

To describe and motivate the notion of a text-based adventure game, play StrongBad's game. Zork I, II and III are freely available at the Infocom home page. Here's information about how to play other Infocom games cheap, including my personal favorite: Hitchhiker's Guide to the Galaxy. For those of you who want to try out a text adventure game without having to bother downloading one, look no farther than emacs. If you type M-x dunnet (or ESC x dunnet), you'll start the game built into the program.

Welcome to My World

The first thing to do is to devise a map of the rooms in your world and their interconnections. We imagine that the exits in the room are labeled with some "direction" string. This could be something like "north" or "southeast" or something completely unexpected like "dennis".

Adventure Game Design

The adventurer will be modeled by an Adventurer class. When we create an Adventurer we will, at the same time, create his BackPack. Ask yourself which of the following statements makes more sense:

When we create an Adventurer, he must be placed in some Room. We have no need to create Adventurers whose location is unspecified.

Our goal is to implement the following hierarchy of classes:

The Collection Interface

Especially useful Collection methods are size, isEmpty, add, remove, contains, and iterator.

The Iterator methods are hasNext, next, and remove.

Classes in java.util that implement the Collection interface are ArrayList, Vector, Stack, HashSet. Because the contents of our BackPack and each Room is unordered and without duplicates, we choose to represent them with a HashSet.

Argueably the worst design decision of all time occured when some programmer opted to implement Stack as an extension of Vector. Why was this the wrong choice and what should have been done instead?

HashMap is a data structure that represents an unordered sequence of associations, i.e., key-value pairs.

> pairs = new HashMap();
> pairs instanceof Collection
> pairs.isEmpty()
> pairs.put("lucy", "ethel");
> pairs.put("pat", "vanna")
> pairs.put("lucy", "ricky")
> pairs.get("pat")
> pairs.get("vanna")

HashMap is an ideal data structure for representing the exit information associated with each Room.

Classroom Participation System

CPS is a "poll the audience" type system that allows students to respond anonymously to multiple-choice questions and then provides immediate feedback, giving the correct answer and the percentages of responses to each option. This system is manufactured by eInstruction and was featured in a recent NY Times article.

Adventure Game Classes

Teaching Strategies

It takes a great deal of effort to be a good teacher. Often it helps to do something unusual, atypical, or memorable. Props are always good.

For a discussion of the benefits of using atypical classroom techniques in a computer science class, read Mixed Nuts by Sid Stamm or visit (and contribute to) his Atypical Techniques online repository.

All materials copyright ©2004 Suzanne Menzel. Last updated November 6, 2004.