CSE132 Examples

These code examples from currrent and prior semesters of CSE132 are a good resource for studying some of the technical concepts covered in CSE132. Try experimenting with examples related to topics we are covering in class.
Replace -- a utility for replacing all occurrences of a string in a file
Replace.java (the source code)
Replace.java (the source code with javadoc comments)
Replace.html (the generated javadoc page)

Graph -- represents a graph containing vertices and edges
Graph.java
Visitor.java (an interface for depth first visiting of graph vertices)
NonMutatingIterator.java (wraps an iterator to prevent modification)
GraphTest.java (a simple JUnit test, incomplete code coverage)

Scanner Example -- illustrates use of java.util.Scanner
InteractiveScanner.java

XML Encoding -- creating XML from an object and displaying it
Person.java -- the class with a main method to output instances as XML
Test.xml -- the XML output generated
mystyle.css -- a style sheet for displaying it
Test.xml -- as displayed in your browser
screen shot -- how Test.xml is displayed in Safari (the Apple browser)

Layout Managers
GUILayoutExample.java -- Spring 2007 example using FlowLayout, BorderLayout, GridLayout, and GridBagLayout
GUI.java -- Spring 2006 example using FlowLayout, BorderLayout, GridLayout, and GridBagLayout
CascadingLayout.java -- a custom layout manager

Custom Components
PaintedBuffer.java -- for creating "thumbnail" images of a component

Model/View Separation
DocumentSharing.java -- two text areas and a button to control their sharing of a document
InfiniteTree.java -- an "infinite" tree model
ListeningPaintedBuffer.java -- a version of the painted buffer that updates due to property change events from the original component
SomeModelState.java -- a simple model with property change support
SomeView.java -- a simple view that listens for property change events to trigger updating
SomeViewTest.java -- a JUnit test with dialog box input for the user to check that the view changes as expected

Direct Manipulation
PanelWithOverlay.java -- a JLayeredPane that provides methods for raising and lowering components between a given content panel and the drag layer
Dragger.java -- provides direct manipulation by drag and drop between containers
EditableTree.java -- event handling and popup menu for manipulation of a tree model
image for "accept" cursor
image for "reject" cursor

Event Handling, Recording, and Playback
Scribble.java -- a view of for a sketch and its associated controller
EventRecorder.java -- a subclass of EventQueue that saves event information for playback
EventInfo.java -- an abstraction of mouse and keyboard event information
EventPlayer.java -- a subclass of Robot that plays back events saved by the EventRecorder

More about Customizing Components
ScrollingAround.java -- illustrates header rows and columns, pulldown menus, etc.
ShapeComponent.java -- illustrates overriding the paint method, colors, fonts, etc.
Picture.java -- a component that loads an image from a file, with example in main method
person.jpg -- an example file for use with the Picture.java main method; in Eclipse, file names are relative to the project directory

Midterm Exam
Review Sheet
OLD Sample Exam Questions -- the exam this year will be different in format, so this old sample exam is probably only marginally helpful
Answers to Sample Questions

Spring 2007 Midterm Exam
Spring 2007 Midterm Exam solutions

Threads and Concurrency Control
ThreadsExample.java -- creates and manages runnable objects in different ways to illustrate the effects
ProgressLoop.java -- a progress bar with a variety of different run() methods and a shared variable for illustrating concurrency control issues
DiningPhilosopher.java -- these philosophers don't wait for anyone
DiningPhilosopher2.java -- these philosophers eat one at a time
DiningPhilosopher3.java -- neighbors cannot eat simultaneously, but deadlock can occur without an arbitration lock (token) that must be held while acquiring both locks in an atomic step
DiningPhilosopher4.java -- neighbors cannot eat simultaneously, but in this solution symmetry is broken to avoid the possibility of deadlock

Blocking Queue
BlockingQueue.java -- an ADT for interacting producer/consumer threads

Client/Server Framework from Spring 2007
zip file -- zip file containing all of the following Java classes
Communicator.java -- supports sneding and receiving messages as objects
AbstractActiveClient.java -- for clients that explicitly receive messages when they are ready
AbstractReactiveClient.java -- for clients that handle messages whenever they arrive
AbstractServer.java -- a multithreaded server implementation with a factory method for creating communicators to handle interaction with each client
SquareServer.java -- an example server that accepts messages (integers) and returns their squares
SquareClient.java -- a client that sends a sequence of integers to a square server

Client/Server Framework from Previous Years
architecture diagram -- an overview of the framework
zip file -- containing all of these Java classes
ServerBehavior.java -- an interface that captures the server logic
ClientBehavior.java -- an interface that captures the client logic
AbstractServerBehavior.java -- a skeleton implementation of a ServerBehavior
AbstractClientBehavior.java -- a skeleton implementation of a ClientBehavior
Server.java -- a server that accepts client connections and creates a ClientHandler to handle each session in a separate thread
ClientHandler.java -- responsible for communication with each connected client (on the server side)
ClientManager.java -- responsible for communication at each client (on the client side)
ClientServerTest.java -- a sample application that creates a simple client and server within the framework

Remote Method Invocation (RMI) example
zip file -- zip file containing all of the following Java classes
Squarer.java -- the remote interface for the server
SquarerImpl.java -- the implementation of the server object
SquareServer.java -- creates a SquarerImpl and binds it in the rmi registry (be sure to start the rmiregistry first in the directory in which you will start the server)
SquareClient.java -- connects to the rmi registry and gets a remote referece to ths square server, and then makes calls on it
client.policy -- an example policy file that allows connections to any remote host
DisplayFrame.java -- a GUI support class

Reflection Examples
LineTool.java is an example plug-in for YOPS, which we developed for CSE131.

DataView has been used in our introductory CS course for visualization of arbitrary data structures by reflection. See, for example, Lab6.java and ListItem.java. Supporting classes can be found in the canvas package.
JTable Model Design Example
table.zip contains files for an example JTable model and JTable implementation supporting the creation of a JTable that renders an arbitrary list of objects. Reflection is used to identify the columns of the table and get and set values. The model fires changes whenever the list is modified.