Java  1.0
Classes | Public Member Functions | Protected Member Functions | Protected Attributes | Private Attributes | List of all members
TextIOApplet Class Referenceabstract

An abstract base class for writing applets that simulate programs that use TextIO or standard I/O. More...

Inheritance diagram for TextIOApplet:
Inheritance graph
[legend]
Collaboration diagram for TextIOApplet:
Collaboration graph
[legend]

Classes

class  AbortException
 The applet includes an "Abort Program" button. More...
 
class  BogusSystem
 A class for implementing the "System.out/System.in" kludge. More...
 
class  Console
 A "Console" is a panel which simulates standard input/output. More...
 
class  ProgRun
 
class  TextIOObject
 The TextIO member variable refers to an object of this class, which implements all the functionality of TextIO (but in non-static instead of static methods). More...
 

Public Member Functions

 TextIOApplet ()
 
void init ()
 Creates and starts the program running thread when the applet is inited. More...
 

Protected Member Functions

abstract void runProgram ()
 A subclass must override this method. More...
 
String getDefaultAppletTitle ()
 The return value of this method is used as the initial message that appears at the top of the applet. More...
 
String getTitle ()
 This returns the message that is currently displayed at the top of the applet. More...
 
void setTitle (String title)
 This sets the message that is displayed at the top of the applet. More...
 
void doDelay (int milliseconds)
 You can call this to insert a delay of a specified number of milliseconds into your program. More...
 
void checkAbort ()
 When the user presses the "Abort Program" button, it does not actually stop the program; it merely sets an "abort flag" to tell the program to stop. More...
 

Protected Attributes

TextIOObject TextIO
 This is the object that takes the place of the usual TextIO class. More...
 
BogusSystem System
 This is the object that your program is referring to when it says "System.out" or "System.in", instead of referring to the usual standard output and input. More...
 

Private Attributes

volatile Thread runner
 
volatile boolean aborted
 
volatile boolean programIsRunning
 
JButton runAgainButton
 
JButton abortButton
 
JLabel message
 
String inputFileName
 
String outputFileName
 
JFileChooser fileDialog
 
final BufferedReader standardInput
 
final PrintWriter standardOutput
 
BufferedReader in
 
PrintWriter out
 
boolean readingStandardInput = true
 
boolean writingStandardOutput = true
 
int inputErrorCount
 
int outputErrorCount
 
Matcher integerMatcher
 
Matcher floatMatcher
 
final Pattern integerRegex = Pattern.compile("(\\+|-)?[0-9]+")
 
final Pattern floatRegex = Pattern.compile("(\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))((e|E)(\\+|-)?[0-9]+)?")
 
String buffer = null
 
int pos = 0
 
Console console
 

Detailed Description

An abstract base class for writing applets that simulate programs that use TextIO or standard I/O.

This class was designed for use the on-line textbook at http://math.hws.edu/eck/cs124/javanotes6/. It might also be useful for other cases when you want an applet that simulates a text-oriented command-line-style program, but it was written for my own specific purposes and no guarantees are made that it will be useful for anything else.

This class can be used as follows: Take (almost) any program that uses TextIO. Change the "public static void main(String[] args)" to "protected void runProgram()". Remove the "static" from any static methods, static member variables, and static nested classes in your class. (It's not necessary to make final static member variables non-static.) Declare your class to be a sub-class of TextIOApplet (and probably change its name). Your class can then be used as an applet on a web page, and the applet will have the same functionality as the original program. (Note that you have to include on your web site all the .class files that are created when TextIOApplet.java is compiled, along with the .class files from your own class. Since this file compiles to a lot of class files, using a jar archive to contains all the class files is recommended.)

Your program can use all the methods in TextIO, and it can also use System.out and System.in. The identifiers TextIO, System.out, and System.in have been redefined in TextIOApplet and its sub-classes so that they actually do I/O in the applet instead of using the standard I/O streams. (This is really a rather nasty kludge, but it does make it easy to write TextIO applets.) If you really want to write to standard output, use java.lang.System.out. (Note that in this applet and its subclasses, System.out refers to a PrintWriter rather than to an OutputStream, so the behavior might not be exactly the same as standard output. Similarly, System.in refers to a BufferedReader rather than an InputStream.)

There are some things that won't work in TextIO applets. For one thing, no way is provided for simulating command line parameters, so programs that require command line parameters can't be run in the applet. For another, when an applet is run in the usual way, it cannot access the file system, so TextIO.readFile(), TextIO.writeFile(), TextIO.readSelectedFile() and TextIO.writeUserSelectedFile() will cause an exception if you try to use them, at least in an applet that is running on a web page.

Definition at line 46 of file TextIOApplet.java.

Constructor & Destructor Documentation

◆ TextIOApplet()

TextIOApplet.TextIOApplet ( )

Member Function Documentation

◆ checkAbort()

void TextIOApplet.checkAbort ( )
protected

When the user presses the "Abort Program" button, it does not actually stop the program; it merely sets an "abort flag" to tell the program to stop.

This flag is checked automatically every time the program does any input or output. However, if the program is doing a long computation during which it does no I/O, it will not automatically detect the abort flag during that time. If you want the program to respond to aborts during such computations, you can insert calls to this "checkAbort()" method. If the user has pressed the Abort button, this method will throw an exception of type AbortException that will terminate your runProgram() method.

Definition at line 141 of file TextIOApplet.java.

References console.

◆ doDelay()

void TextIOApplet.doDelay ( int  milliseconds)
protected

You can call this to insert a delay of a specified number of milliseconds into your program.

Calling this method also checks whether the user has pressed the abort button, so that the abort can take effect.

Parameters
millisecondsThe number of milliseconds to stop. If the value is less than or equal to zero, then instead of inserting a delay, Thread.yield() is called.

Definition at line 116 of file TextIOApplet.java.

References console.

◆ getDefaultAppletTitle()

String TextIOApplet.getDefaultAppletTitle ( )
protected

The return value of this method is used as the initial message that appears at the top of the applet.

You can override this method to provide a different initial string. For example, you can return a name for your program.

Definition at line 88 of file TextIOApplet.java.

Referenced by TextIOApplet.ProgRun.run(), and TextIOApplet().

◆ getTitle()

String TextIOApplet.getTitle ( )
protected

This returns the message that is currently displayed at the top of the applet.

Definition at line 95 of file TextIOApplet.java.

References message.

◆ init()

void TextIOApplet.init ( )

Creates and starts the program running thread when the applet is inited.

Definition at line 298 of file TextIOApplet.java.

References programIsRunning, and runner.

◆ runProgram()

abstract void TextIOApplet.runProgram ( )
abstractprotected

A subclass must override this method.

This is the replacement for the "main routine" and should contain the code that you want to run when the program is executed.

Referenced by TextIOApplet.ProgRun.run().

◆ setTitle()

void TextIOApplet.setTitle ( String  title)
protected

This sets the message that is displayed at the top of the applet.

Note that when the program is restarted by the user, the string automatically reverts to the string that is returned by the getDefaultAppletTitle() method. Only a short, one-line message can be displayed.

Definition at line 105 of file TextIOApplet.java.

References message.

Member Data Documentation

◆ abortButton

JButton TextIOApplet.abortButton
private

Definition at line 186 of file TextIOApplet.java.

Referenced by TextIOApplet.ProgRun.run(), and TextIOApplet().

◆ aborted

volatile boolean TextIOApplet.aborted
private

Definition at line 182 of file TextIOApplet.java.

Referenced by TextIOApplet.ProgRun.run(), and TextIOApplet().

◆ buffer

String TextIOApplet.buffer = null
private

◆ console

Console TextIOApplet.console
private

◆ fileDialog

JFileChooser TextIOApplet.fileDialog
private

◆ floatMatcher

Matcher TextIOApplet.floatMatcher
private

◆ floatRegex

final Pattern TextIOApplet.floatRegex = Pattern.compile("(\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))((e|E)(\\+|-)?[0-9]+)?")
private

Definition at line 1574 of file TextIOApplet.java.

Referenced by TextIOApplet.TextIOObject.readRealString().

◆ in

BufferedReader TextIOApplet.in
private

◆ inputErrorCount

int TextIOApplet.inputErrorCount
private

◆ inputFileName

String TextIOApplet.inputFileName
private

◆ integerMatcher

Matcher TextIOApplet.integerMatcher
private

◆ integerRegex

final Pattern TextIOApplet.integerRegex = Pattern.compile("(\\+|-)?[0-9]+")
private

Definition at line 1573 of file TextIOApplet.java.

Referenced by TextIOApplet.TextIOObject.readIntegerString().

◆ message

JLabel TextIOApplet.message
private

◆ out

PrintWriter TextIOApplet.out
private

◆ outputErrorCount

int TextIOApplet.outputErrorCount
private

◆ outputFileName

String TextIOApplet.outputFileName
private

◆ pos

int TextIOApplet.pos = 0
private

◆ programIsRunning

volatile boolean TextIOApplet.programIsRunning
private

Definition at line 183 of file TextIOApplet.java.

Referenced by init(), TextIOApplet.ProgRun.run(), and TextIOApplet().

◆ readingStandardInput

boolean TextIOApplet.readingStandardInput = true
private

◆ runAgainButton

JButton TextIOApplet.runAgainButton
private

Definition at line 185 of file TextIOApplet.java.

Referenced by TextIOApplet.ProgRun.run(), and TextIOApplet().

◆ runner

volatile Thread TextIOApplet.runner
private

Definition at line 181 of file TextIOApplet.java.

Referenced by init().

◆ standardInput

final BufferedReader TextIOApplet.standardInput
private

◆ standardOutput

final PrintWriter TextIOApplet.standardOutput
private

◆ System

BogusSystem TextIOApplet.System
protected

This is the object that your program is referring to when it says "System.out" or "System.in", instead of referring to the usual standard output and input.

This System.out writes to the applet, and this System.in reads data typed by the user in the applet. (Note that you could still access the real System class by using its full name, "java.lang.System".) The BogusSystem class is a nested class, defined below.

Definition at line 81 of file TextIOApplet.java.

Referenced by TextIOApplet().

◆ TextIO

TextIOObject TextIOApplet.TextIO
protected

This is the object that takes the place of the usual TextIO class.

It has exactly the same methods as the TextIO class, but they do IO in the applet instead of using standard input and output.

Definition at line 72 of file TextIOApplet.java.

◆ writingStandardOutput

boolean TextIOApplet.writingStandardOutput = true
private

The documentation for this class was generated from the following file: