CIS 4930 (Programming in Java):
Sample Programs
Examples from class
- (January 13) Sample applets from chapter 2 of the text:
(Warning: Many browsers do not support the Java features
used in these examples. If clicking on the .html files above
gives you an error message, try downloading the .html and
.class files and using an applet viewer to view the applets.)
- (January 27) Simulation-style example:
RegisteredVehicleTest.java.
- (January 27) Sorted linked list:
OrderedIntList.java.
- (January 29) Cloning, wrong and right ways:
Clones.java.
- (February 3 and 5) Nested classes:
Nest1.java,
Nest2.java,
Nest3.java,
Nest4.java.
- (February 5) Use of core classes for parsing and formatting:
- (February 12) Use of other core classes:
- java.text.DecimalFormat:
TestDecimalFormat.java (simpler scheme for
formatting numeric output than the one in
TestNumberFormat above).
- System properties table (analogous to environment
variables in some other languages/systems):
ShowSystemProps.java.
ShowSystemProps.OUT show sample output on one of
the CISE Unix machines;
compare to output on your system.
- (February 12 and 17)
Use of Java input/output and related classes:
-
TestFile.java demonstrates use of File class
to get information about a file (or directory).
-
TestDataStreams.java demonstrates use of
Data* classes to read/write data in binary format,
ByteArray* classes to use a byte array as a
source/destination, and connecting streams in
sequence.
-
TestStreamTokenizer.java demonstrates use of
StreamTokenizer and PrintWriter classes to parse
input and print results.
-
TestFilters.java demonstrates user-defined
subclasses of Filter* classes to perform encoding
on input and output streams.
- (February 19) Threads:
- (February 24) Threads:
-
ProducerConsumer.java shows use of
synchronized methods, wait() and
notifyAll() to coordinate among
threads.
- (March 3) Threads:
-
TestInterrupt.java shows use of
interrupt(), suspend(), resume() to
coordinate among threads.
- (Not discussed in class.)
SimplerThread.java is an even simpler
threads example that demonstrates whether
your system performs timeslicing (if you're
curious).
- (March 5) The Java AWT:
-
ShowString1.java
creates a Frame object (window plus titlebar)
and displays a string in it.
-
ShowString2.java is ShowString1.java modified
to use my CloseableFrame class.
-
CloseableFrame.java defines a subclass of
Frame that (unlike its superclass)
responds to window-closing events.
- (March 17) The Java AWT:
- (March 19) The Java AWT:
-
CloseableFrameAlt.java is an alternate
version of the CloseableFrame class.
-
SwitchedButtonAlt.java is an alternate
version of SwitchedButton.java above, using
setEnabled().
-
PowerOf2GUI.java shows providing a GUI for
an object, using a named
adapter class and getActionCommand(), setActionCommand()
to process events.
-
PowerOf2GUIAlt.java is an alternate version of
PowerOf2GUI.java, using anonymous inner classes as
adapters.
- (Not discussed in class.)
ColorLabels.java shows setting different
foreground and background colors for different
components.
-
MirrorLine.java shows use of TextField class.
- (March 24) The Java AWT:
- (Not discussed in class.)
Layouts.java shows use of different layout
managers.
- (March 26) The Java AWT:
- (March 31) The Java AWT:
-
ScrollExample.java shows use of Scrollpane class.
-
DialogExample.java shows use of Dialog class.
- (Not discussed in class.)
ChooseFont.java shows use of Font class (allows
you to see effects of different font choices on
label text).
-
ShowThreads.java provides utility methods for
showing information about what threads are executing --
in case you are curious about what threads the AWT
-
DrawingFrame.java is an extension of the Frame
class that (1) responds to window-closing events and
(2) provides methods to get / set the size of the
frame's "drawing area" (area excluding borders).
-
TestPattern.java is an application version of
the TestPattern example in the text, demonstrating
the use of the Graphics class to draw geometric
figures.
-
TestPatternDB.java is an alternative version of
TestPattern above, using double buffering to reduce
flicker.
- (April 7) The Java AWT:
-
PolyDraw.java illustrates use of Canvas and
Graphics classes in a simple drawing example.
- (Not discussed in class.)
Scribble.java, a modified application version
of the DoodlePad example in the text,
illustrates use of Canvas and
Graphics classes in another simple drawing example.
- (April 9) The Java AWT:
-
ShowImage.java illustrates use of the Image class
to display an image from a GIF or JPEG file.
See sample GIF files
cise-logo.gif (CISE logo) and
distrib.gif (distribution of midterm grades).
- (April 9) Java and networking:
-
DateAtHost.java illustrates use of java.net.*
classes to retrieve time/date from a remote host.
-
ShowURLContents.java illustrates use of java.net.*
classes to retrieve and display URL contents.
- (April 14) Java and networking:
-
TinyHttpd1.java illustrates use of java.net.*
classes to set up a very simple Web server
(see comments in code).
-
TinyHttpd2.java illustrates use of java.net.*
classes to set up a slightly less simple Web server
(see comments in code).
- (April 16) Java and networking:
Hints for homeworks
Other classes
Classes from last term's CIS3020:
Other utility classes:
- LineInput class (readLine() method for standard input that
should work for all systems):
LineInput.java. Note that LineInput.readLine(), unlike
the methods of the Input class above, signals end of file
by returning a null reference rather than by throwing
an exception.
Miscellaneous short examples
Additional examples
Additional examples can be found linked from
Homeworks under
"Grading notes and sample solutions".