![]() |
Java
1.0
|
An abstract base class for writing applets that simulate programs that use TextIO or standard I/O. More...
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 |
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.
TextIOApplet.TextIOApplet | ( | ) |
Definition at line 189 of file TextIOApplet.java.
References abortButton, aborted, console, getDefaultAppletTitle(), in, message, out, programIsRunning, runAgainButton, standardInput, standardOutput, and System.
|
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.
|
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.
milliseconds | The 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.
|
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().
|
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.
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.
|
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().
|
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.
|
private |
Definition at line 186 of file TextIOApplet.java.
Referenced by TextIOApplet.ProgRun.run(), and TextIOApplet().
|
private |
Definition at line 182 of file TextIOApplet.java.
Referenced by TextIOApplet.ProgRun.run(), and TextIOApplet().
|
private |
Definition at line 1576 of file TextIOApplet.java.
Referenced by TextIOApplet.TextIOObject.emptyBuffer(), TextIOApplet.TextIOObject.fillBuffer(), TextIOApplet.TextIOObject.lookChar(), TextIOApplet.TextIOObject.readChar(), TextIOApplet.TextIOObject.readIntegerString(), and TextIOApplet.TextIOObject.readRealString().
|
private |
Definition at line 1579 of file TextIOApplet.java.
Referenced by checkAbort(), doDelay(), TextIOApplet.TextIOObject.errorMessage(), TextIOApplet.TextIOObject.readUserSelectedFile(), TextIOApplet.ProgRun.run(), TextIOApplet(), and TextIOApplet.TextIOObject.writeUserSelectedFile().
|
private |
Definition at line 1557 of file TextIOApplet.java.
Referenced by TextIOApplet.TextIOObject.readUserSelectedFile(), and TextIOApplet.TextIOObject.writeUserSelectedFile().
|
private |
Definition at line 1572 of file TextIOApplet.java.
Referenced by TextIOApplet.TextIOObject.fillBuffer(), and TextIOApplet.TextIOObject.readRealString().
|
private |
Definition at line 1574 of file TextIOApplet.java.
Referenced by TextIOApplet.TextIOObject.readRealString().
|
private |
|
private |
Definition at line 1568 of file TextIOApplet.java.
Referenced by TextIOApplet.TextIOObject.errorMessage(), TextIOApplet.TextIOObject.getBoolean(), TextIOApplet.TextIOObject.getDouble(), TextIOApplet.TextIOObject.getFloat(), TextIOApplet.TextIOObject.readFile(), TextIOApplet.TextIOObject.readInteger(), TextIOApplet.TextIOObject.readStandardInput(), TextIOApplet.TextIOObject.readStream(), and TextIOApplet.TextIOObject.readUserSelectedFile().
|
private |
Definition at line 1554 of file TextIOApplet.java.
Referenced by TextIOApplet.TextIOObject.errorMessage(), TextIOApplet.TextIOObject.fillBuffer(), TextIOApplet.TextIOObject.getInputFileName(), TextIOApplet.TextIOObject.readChar(), TextIOApplet.TextIOObject.readFile(), TextIOApplet.TextIOObject.readStandardInput(), TextIOApplet.TextIOObject.readStream(), and TextIOApplet.TextIOObject.readUserSelectedFile().
|
private |
Definition at line 1571 of file TextIOApplet.java.
Referenced by TextIOApplet.TextIOObject.fillBuffer(), and TextIOApplet.TextIOObject.readIntegerString().
|
private |
Definition at line 1573 of file TextIOApplet.java.
Referenced by TextIOApplet.TextIOObject.readIntegerString().
|
private |
Definition at line 187 of file TextIOApplet.java.
Referenced by TextIOApplet.TextIOObject.errorMessage(), getTitle(), TextIOApplet.TextIOObject.outputError(), TextIOApplet.ProgRun.run(), setTitle(), and TextIOApplet().
|
private |
Definition at line 1563 of file TextIOApplet.java.
Referenced by TextIOApplet.TextIOObject.errorMessage(), TextIOApplet.TextIOObject.put(), TextIOApplet.TextIOObject.putf(), TextIOApplet.TextIOObject.putln(), TextIOApplet.TextIOObject.skipWhitespace(), TextIOApplet(), TextIOApplet.TextIOObject.writeFile(), TextIOApplet.TextIOObject.writeStandardOutput(), TextIOApplet.TextIOObject.writeStream(), and TextIOApplet.TextIOObject.writeUserSelectedFile().
|
private |
|
private |
Definition at line 1555 of file TextIOApplet.java.
Referenced by TextIOApplet.TextIOObject.getOutputFileName(), TextIOApplet.TextIOObject.outputError(), TextIOApplet.TextIOObject.writeFile(), TextIOApplet.TextIOObject.writeStandardOutput(), TextIOApplet.TextIOObject.writeStream(), and TextIOApplet.TextIOObject.writeUserSelectedFile().
|
private |
Definition at line 1577 of file TextIOApplet.java.
Referenced by TextIOApplet.TextIOObject.fillBuffer(), TextIOApplet.TextIOObject.lookChar(), TextIOApplet.TextIOObject.readChar(), TextIOApplet.TextIOObject.readIntegerString(), and TextIOApplet.TextIOObject.readRealString().
|
private |
Definition at line 183 of file TextIOApplet.java.
Referenced by init(), TextIOApplet.ProgRun.run(), and TextIOApplet().
|
private |
Definition at line 1565 of file TextIOApplet.java.
Referenced by TextIOApplet.TextIOObject.errorMessage(), TextIOApplet.TextIOObject.fillBuffer(), TextIOApplet.TextIOObject.readChar(), TextIOApplet.TextIOObject.readFile(), TextIOApplet.TextIOObject.readStandardInput(), TextIOApplet.TextIOObject.readStream(), TextIOApplet.TextIOObject.readUserSelectedFile(), and TextIOApplet.TextIOObject.skipWhitespace().
|
private |
Definition at line 185 of file TextIOApplet.java.
Referenced by TextIOApplet.ProgRun.run(), and TextIOApplet().
|
private |
Definition at line 181 of file TextIOApplet.java.
Referenced by init().
|
private |
Definition at line 1559 of file TextIOApplet.java.
Referenced by TextIOApplet.TextIOObject.readStandardInput(), and TextIOApplet().
|
private |
Definition at line 1560 of file TextIOApplet.java.
Referenced by TextIOApplet.ProgRun.run(), TextIOApplet(), and TextIOApplet.TextIOObject.writeStandardOutput().
|
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().
|
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.
|
private |
Definition at line 1566 of file TextIOApplet.java.
Referenced by TextIOApplet.TextIOObject.errorMessage(), TextIOApplet.TextIOObject.outputError(), TextIOApplet.TextIOObject.skipWhitespace(), TextIOApplet.TextIOObject.writeFile(), TextIOApplet.TextIOObject.writeStandardOutput(), TextIOApplet.TextIOObject.writeStream(), and TextIOApplet.TextIOObject.writeUserSelectedFile().