CS 3291 (Java with Internet Applications):
Sample Programs
"Hello" examples
Examples I used in teaching a previous Java course
- Examples of processing command-line arguments:
Echo.java.
Sum.java.
- Simple class example:
NamedArray.java.
- Simulation-style example:
RegisteredVehicleTest.java.
- Sorted linked list:
OrderedIntList.java.
- Cloning, wrong and right ways:
Clones.java.
- Nested classes:
Nest1.java,
Nest2.java,
Nest3.java,
Nest4.java.
- Use of core classes for parsing and formatting:
- 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.
- 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.
- Threads:
-
SimpleThread.java and
SimpleThreadObj.java show two
implementations of a simple program with two
independent threads.
-
SharingThread.java shows use of a synchronized
method to make sure at most one thread at a time
executes.
-
ProducerConsumer.java shows use of
synchronized methods, wait() and
notifyAll() to coordinate among
threads.
-
TestInterrupt.java shows use of
interrupt(), suspend(), resume() to
coordinate among threads.
-
SimplerThread.java is an even simpler
threads example that demonstrates whether
your system performs timeslicing (if you're
curious).
- 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.
-
SwitchedButton.java shows use of Button
class, ActionListener interface.
-
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.
-
ColorLabels.java shows setting different
foreground and background colors for different
components.
-
MirrorLine.java shows use of TextField class.
-
Layouts.java shows use of different layout
managers.
-
MirrorArea.java shows use of TextArea class
and a variety of listener adapters.
Compare with MirrorLine previously.
-
ListExample.java shows use of List class.
ListExampleAlt.java is an alternate
implementation.
-
ChoiceExample.java shows use of Choice class.
-
MenuExample.java shows use of Menu class.
-
PopupMenuExample.java shows use of PopupMenu class.
-
CheckboxExample.java shows use of Checkbox
and CheckboxGroup classes.
-
ScrollExample.java shows use of Scrollpane class.
-
DialogExample.java shows use of Dialog 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.
-
PolyDraw.java illustrates use of Canvas and
Graphics classes in a simple drawing example.
-
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.
-
ShowImage.java illustrates use of the Image class
to display an image from a GIF or JPEG file.
See sample GIF files
tu_seal.gif (Trinity U. seal) and
smiley.gif (smiley face).
-
ShowImageApplet.java is an applet version of the
preceding example, with its corresponding HTML file
ShowImageApplet.html.
-
ShowImageWithObserver.java illustrates use of the
Image class to display an image from a GIF or JPEG file,
specified via a URL.
This program includes a mostly gratuitous
ImageObserver that reports on progress as the image
is loaded.
-
ColorPan.java illustrates generating an Image
from pixel data.
-
BlackWhiteStatic.java illustrates generating
a succession of images from pixel data.
-
ColorSep.java illustrates the use of filters
with images.
- 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.
-
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).
-
UDPserver.java
and
UDPclient.java
illustrate use of java.net.*
classes for simple client/server communication
using UDP.
-
Request.java,
RequestServer.java,
and
RequestClient.java
illustrate use of java.net.*
classes and object serialization for more complex
client/server communication using TCP.
(
TestRequest.java tests the Request class
and its subclasses.)
Other possibly useful classes
- Input class (methods for reading primitives and strings
from standard input):
Input.java.
See
TestInput.java,
TestFileInput.java,
TestInputLoop.java
for additional examples of usage.
- Assertions class (methods to check assertions at runtime):
Assertions.java.
- Closeable Frame class (version of java.awt.Frame):
CIS3020CloseableFrame.java.
- IsItArt example application (example of simple Java graphics):
IsItArt.java.
Requires Input, Assertions, and CIS3020CloseableFrame above,
and also
RandomInts.class.
Note that the latter is a .class file; no source is
available.
- 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.