|
|
Resources to make teaching Java faster, more effective and more fun.
|
Quick Links |
|
|
The hsa package - Details
[Table of Contents] *
[HSA Class Library Overview]
Introduction
The hsa package consists of three applications and nine classes.
The applications make marking easier and provide some utilities
for users moving between different Java environments.
The classes make it easier for users to begin writing programs
that explore the basic concepts of computer science before coming
to terms with the input and output methods of the Java class
library.
To ensure the easiest possible transition from using the hsa
package to using only the Java class library, wherever possible we
have made the graphics methods in the Console class identical to
those in the Java Graphics class. The print and println methods of
the Console, Stdout, and TextOutputFile are also the same as those
in the Java PrintWriter class. In a number of cases the classes in the
hsa package contain extra methods.
To use the hsa classes, the following line must appear in the
class header:
import hsa.*;
The hsa directory containing all the ".class" files must
be located in one of the directories specified in the CLASSPATH.
The CLASSPATH is a list of directories that the Java Virtual Machine (JVM)
searches for Java ".class" files. Determining and setting the CLASSPATH
is dependent on the particular Java development environment.
[Table of Contents] *
[HSA Class Library Overview]
Executing the hsa Applications
To execute the Submit, PrettyPrint, or PrintFiles
programs from outside the environment, a Java
Virtual Machine must exist on your computer and the hsa
directory containing all the ".class" files must
be located in one of the directories specified in the CLASSPATH.
The CLASSPATH is a list of directories that the Java Virtual Machine (JVM)
searches for Java ".class" files. Determining and setting the CLASSPATH
is dependent on the particular Java development environment.
If you wish to be able to execute the programs from the DOS command line,
you must obtain the latest version of Sun's Java Runtime Environment
(JRE), which includes a Java Virtual Machine, free of charge from
www.javasoft.com. See the installation instructions for information
on where to copy the hsa directory and Sun's JRE for information
on the CLASSPATH environment variable.
To execute any of these program from Sun's JRE, you must
enter DOS, move to the directory containing the files to be
submitted, printed, or paragraphed, and type:
java hsa.Application-Name
where the Application-Name is Submit, PrettyPrint,
or PrintFiles.
Failure of the JRE to find the program indicates that the hsa
directory has not been correctly placed in one of the directories
listed in the CLASSPATH. Check the documentation for Sun's JRE
for more information on the CLASSPATH variable.
[Table of Contents] *
[HSA Class Library Overview]
hsa Applications - Setting Options
You can also enter a number of options to modify the behavior
of any of the hsa applications. These options are listed with the
instructions for each of the programs. You can enter options in one
of three ways.
The simplest way is to specify the options on the command
line. This is done by entering the option after the name of the
program, for example,
java hsa.Submit -nobold -noitalic
stops Submit from bolding keywords and italicizing identifiers
when it prints out Java programs.
The second way is to change the options in the Options dialog
box. Once you have started the program, each of the hsa
application programs comes up with an Options dialog box which
has checkboxes, radio buttons, and text fields for the basic options
you can select. It also has a menu labelled "Options". The Options
menu allows you to get information about the program, see only
the basic options, see the advanced options, and save the options so
the current set of options become the default values the next time
the program is run.
The third way to enter options is to edit a file called
hsa.config located in the your home directory. This file is read
for options every time any of the hsa programs are executed and
options are saved to this file when the user selects the Save Options
menu item. There is a section in the file for each of the three
applications. Each section is prefaced with the line "[Submit]",
"[PrettyPrint]" or "[PrintFiles]". An example
hsa.config file
might look like:
[Submit]
-nobold
-noitalic
[PrettyPrint]
-indent 2
Options selected in the Options dialog box override options
specified on the command line which override options specified in
the hsa.config file. To skip the Options dialog box, you can use
the option -nooptions in the command line or in the hsa.config
file.
Here is the list of all the available options. The default options
are listed in bold face. Items in curly braces can be included zero
or more times.
| |
|
|
| -program classname |
Submit |
| |
Sets the program to be submitted to classname. If this option
and -nooptions is set, the initial Submit dialog box
does not appear.
|
| -sourcedir sourcedirectory |
All |
| |
Sets the initial directory to sourcedirectory for the
Select Source Files (Submit), Select Files to Print
(PrintFiles), or Select Files To Indent (PrettyPrint)
dialog box. If no option is specified, the initial directory is
set to the current directory.
|
| -source sourcefile {sourcefile} |
All |
| |
Sets the source files to be printed (Submit, PrintFiles)
or paragraphed (PrettyPrint) to be the files specified. If this
option is used, the Select Source Files (Submit),
Select Files to Print (PrintFiles), or Select Files
To Indent (PrettyPrint) dialog box does not appear.
|
| -inputdir inputdirectory |
Submit |
| |
Sets the initial directory to inputdirectory for the
Select Input Files dialog box. If no option is specified,
the initial directory is set to the last directory used in the
Select Source Files dialog box.
|
| -input {inputfile} |
Submit |
| |
Sets the input data files to be used to run the submitted program.
If this option is used, the Select Input Files dialog box
does not appear. You do not need to specify any inputfile,
in which case the submitted program is run once with no input.
|
| -echo |
Submit |
| |
Echoes input read from file into the program's output.
|
| -noecho |
Submit |
| |
Does not echo input read from the file into the program's output.
|
| -indent |
Submit, PrintFiles |
| |
Indents the programs using PrettyPrint before printing them.
|
| -noindent |
All |
| |
Does not indent the programs before printing them.
|
| -print |
Submit, PrintFiles |
| |
Prints to the printer (see -preview for sending all output to a window
instead).
|
| -preview |
Submit, PrintFiles |
| |
Sends all output to a window rather than to the printer. This option is
useful for reviewing the results of a Submit without wasting paper.
|
| -bold |
Submit, PrintFiles |
| |
Prints Java keywords in bold face when printing programs.
|
| -nobold |
Submit, PrintFiles |
| |
Does not print Java keywords in bold face when printing programs.
|
| -italic |
Submit, PrintFiles |
| |
Prints identifiers in italics when printing programs.
|
| -noitalic |
Submit, PrintFiles |
| |
Does not print identifiers in italics when printing programs.
|
| -defaultmargin |
Submit, PrintFiles |
| |
Makes printing assume that Java has set the printer margins correctly.
Due to some irregularities in the way Java (all environments) does
printing, there is no way to know whether the upper-left corner of
the printing area is the upper-left corner of the page, or the
upper-left corner of the printable area.
If your printouts are being cut off on the top and left sides, use
the -margin option to set the margins.
|
| -margin {leftmargin, topmargin} |
Submit, PrintFiles |
| |
Causes Submit and PrintFiles to add their own
margins when printing.
leftmargin and topmargin are specified in inches. If
they are not specified (leftmargin and topmargin are
optional), the left and top margins are set to 0.35".
If your printouts are being cut off on the top and left sides, use
this option to set the margins. If necessary, specify leftmargin
and topmargin to get printing to be placed correctly on the page.
|
| -indentsize indentSize |
All |
| |
Sets the size of each indentation when paragraphing to indentSize
spaces. This defaults to 4. This option is only relevant if the
-toother option is selected (which it is by default).
|
| -usetabs |
PrettyPrint |
| |
Converts groups of 8 spaces into tabs when the paragraphed program is
saved. This option is only relevant if the -toother option is selected
(which it is by default).
|
| -usespaces |
PrettyPrint |
| |
Does not converts spaces into tabs.
|
| -fromva (from va) |
All |
| |
Makes hsa programs assume that source files have been exported
from the VisualAge for Java environment and that tabs
are set at 0.25" apart.
|
| -fromother (from other) |
All |
| |
Makes hsa programs assume that source files have come from other
environments and that tabs are set 8 spaces apart.
|
| -tova (to va) |
PrettyPrint |
| |
Saves PrettyPrinted (indented) file in format suitable for
importation into VisualAge for Java. Each level of
indentation is one tab, and tabs are assumed to be 0.25"
apart. Assumes lines are 70 columns across.
|
| -toother (to other) |
PrettyPrint |
| |
Saves PrettyPrinted (indented) file in a format suitable for
printing or use in another environment. Indentation is specified
by the -indentsize option and spaces are converted to tabs if the
-usetabs option is specified. Tabs are assumed to be 8 spaces apart.
Assumes lines are 80 columns across.
|
| -compact |
All |
| |
Leaves the opening curly brace at the end of a line.
|
| -expanded |
All |
| |
Places an opening curly brace on a line of its own, indented at the
same level as the previous line.
|
| -nooptions |
All |
| |
Does not display any options at all.
|
| -basic |
All |
| |
Displays the basic options for the hsa program.
|
| -advanced |
All |
| |
Displays all the available options for the hsa program.
|
[Table of Contents] *
[
HSA Class Library Overview]
hsa Applications
[Table of Contents] *
[hsa Applications] *
[
HSA Class Library Overview]
Submit
Introduction
A Java application program used to submit student's assignments for
marking.
This brings up dialog boxes to allow the user to enter the name of the
program to be submitted, the names of the Java text files that make up
the program, and the namesof the test data files. It then prints the
Java text files, the input files, and the output produced from running
the program.
Instructions
To execute Submit (which must be run from outside of
VisualAge for Java) you must run the program hsa.Submit.
The Submit dialog box appears (see
Submit dialog box). You must enter the name of the Java program
to be submitted. Enter only the class name without a ".class" or
".java" suffix. You can change any options at this point
as described in the Setting Options
section. Pressing the OK button in this dialog box
closes the Submit dialog box and makes the Select Source
Files dialog box appear.
Figure 1 The Submit Dialog Box
The Select Source Files dialog box (See
Select Source Files dialog box)
contains two lists of files, the files found on the disk and the list of
Java text files to be printed. The dialog box only lists directories and
those files that end in ".java" (which should be the case for all Java
text files). To select files to be printed, you can either double click
on the file name, or click once on the file name and then click the
Copy > button. To move to a different directory, click on the
directory name (all directory names are enclosed in square brackets).
The name of the current directory is listed above the file name list on
the left side. To switch to a different drive, select the drive from the
pop-up menu beneath the list of files in the current directory.
Figure 2 The Select Source Files Dialog Box
If you accidently select a Java text file to be printed that should
not be included, double clicking on the filename in the right hand
list of files will remove it. Once all the Java text files that the
program uses have been entered, click the Next > button. The
list of Java text files to be printed appears in a window labelled
Source Files in the upper-left corner of the screen. The
Select Input Files dialog box appears in the center of the
screen (See Select Input Files
dialog box).
Figure 3 The Select Input Files Dialog Box
This dialog box allows you to select files that will be used as
input to the submitted program when it runs. The submitted
program will be run once for each input file selected. For each run,
instead of reading from standard input (or the hsa Console class),
the submitted program will read from the input file. If no input
files are selected, the submitted program will be run once using no
input. Any text file can be used as an input file. Once all the files
are selected, click the Finish button.
At this point, the Submit program will close the
Select Input Files dialog box, display a small dialog box
called Test Input Files in the upper-right corner of the
screen listing the selected input files and start printing. The
printout contains a copy of all the Java text files
selected, and then for each input file, the input file and the output
from the submitted program using that input file. When it is
finished printing, the Submit program quits. The printout can be
submitted for marking by your instructor.
[Table of Contents] *
[hsa Applications] *
[
HSA Class Library Overview]
PrintFiles
Introduction
A Java application program used to print Java files. It can paragraph
(indent) the file, bold keywords, and italicize identifiers in the
printout if requested.
Instructions
To execute PrintFiles (which must be run from outside of
VisualAge for Java) you must run the program hsa.PrintFiles.
The PrintFiles Options dialog box appears (see
PrintFiles Options
dialog box). You can change any options at this point
as described in the Setting Options
section. Once the appropriate options are set (usually you
will not need to change any of them from the default setting), click
the OK button in the dialog box. The
PrintFiles Options dialog box closes and the
Select Files To Print dialog box appears.
Figure 4 The PrintFiles Options Dialog Box
The Select Files To Print dialog box (see
Select Files To Print dialog box)
contains two lists of files, the files found on the disk and the
list of Java text files to be printed. The dialog box only lists
directories and those files that end in ".java" (which all Java
text files should end in). To select files to be printed, you can
either double click on the file name, or
click once on the file name and then click the Copy > button. To
move to a different directory, click on the directory name (all
directory names are enclosed in square brackets). The name of the
current directory is listed above the file name list on the left side.
To switch to a different drive, select the drive from the pop-up
menu beneath the list of files in the current directory.
Figure 5 The Select Files to Print Dialog Box
If you accidently select a Java text file to be printed that should
not be included, double clicking on the filename in the right hand
list of files will remove it. Once all the Java text files that the
program uses have been entered, click the Finish button. The
selected files are now printed.
[Table of Contents] *
[hsa Applications] *
[
HSA Class Library Overview]
PrettyPrint
Introduction
A Java application program to indent (paragraph) Java programs for
use in VisualAge for Java or other Java environments.
Because VisualAge for Java has its own system for
handling tabs, there are paragraphing problems when importing
and exporting Java files to and from other environments.
PrettyPrint paragraphs (indents) Java files to make them
suitable for printing
or use in other environments, or suitable for importation into
VisualAge for Java. This program makes it easier to
transport Java files between the VisualAge for Java and other Java
environments.
Instructions
To execute PrettyPrint you must either run the PrettyPrint
program found in the hsa package in the Holt Software project from
within VisualAge for Java or run the program hsa.PrintFiles
using another environment such as Sun's JRE.
The PrettyPrint Options dialog box appears (see
PrettyPrint Options dialog box).
You can change any options at this point as described in the
Setting Options section. Once the
appropriate options are set (usually you will not need to change
any of them), click the OK button in the
dialog box. The PrettyPrint Options dialog box closes and the
Select Files To Indent dialog box appears.
Figure 6 The PrettyPrint Options Dialog Box
The Select Files To Indent dialog box (see
Select Files To Indent dialog box)
contains two lists of files, the files found on the disk and the
list of Java text files to be printed. The dialog box only lists
directories and those files that end in ".java" (which all Java
text files should end in). To select files to be printed, you can
either double click on the file name, or click once on the file
name and then click the Copy > button. To move to a
different directory, click on the directory name (all directory
names are enclosed in square brackets). The name of the current
directory is listed above the file name list on the left side.
To switch to a different drive, select the drive from the
pop-up menu beneath the list of files in the current directory.
Figure 7 The Select Files to Indent Dialog Box
If you accidently select a Java text file to be printed that should
not be included, double clicking on the filename in the right hand
list of files will remove it. Once all the Java text files that the
program uses have been entered, click the Finish button. The
selected files are then indented and saved with the same name as
before. You can now copy the Java text files to a different
environment.
[Table of Contents] *
[
HSA Class Library Overview]
hsa Classes
[Table of Contents] *
[hsa Classes] *
[
HSA Class Library Overview]
Console
[Table of Contents] *
[hsa Classes] *
[Console] *
[
HSA Class Library Overview]
Introduction
The Console window can hold 25 lines of 80 column text. The user can
input data directly in the Console window and the output of the program
appears there too. Formatted text output is provided. It supports all the
basic Java Graphics class methods plus two more to draw stars and maple
leaves (suitable for flags).
The Console class window has three buttons. The Save button saves the
contents of the screen as a ".bmp" (Windows Bitmap) file. The
Print button prints out the contents of the window. The Quit
button quits the program immediately. It gives visual notification when the
program is finished execution by changing the label in the Quit
button to Close.
[Table of Contents] *
[hsa Classes] *
[Console] *
[
HSA Class Library Overview]
General Methods and Constructors
-
Console ()
-
Constructor - Creates a Console window of 25 rows by 80 columns.
-
Console (int fontSize)
-
Constructor - Creates a Console window with the text size set to
fontSize.
-
Console (int rows, int cols)
-
Constructor - Creates a Console window with width of cols
columns and height of rows rows.
-
Console (int rows, int cols, int fontSize)
-
Constructor - Creates a Console window with width of cols columns,
height of rows rows, and the text size set to fontSize.
-
Console (String title)
-
Constructor - Creates a Console window and sets the window title to
title.
-
Console (int fontSize, String title)
-
Constructor - Creates a Console window with the text size set to
fontSize and sets the window title to title.
-
Console (int rows, int cols, String title)
-
Constructor - Creates a Console window with width of cols columns,
height of rows rows, and the window title set to title.
-
Console (int rows, int cols, int fontSize, String title)
-
Constructor - Creates a Console window with width of cols columns,
height of rows rows, the text size set to fontSize, and
the window title set to title.
-
void close ()
-
Closes the Console window and disposes of its contents.
[Table of Contents] *
[hsa Classes] *
[Console] *
[
HSA Class Library Overview]
Graphics Methods
-
void clearRect (int x, int y, int width, int height)
-
Clears the rectangle to the background color.
-
void copyArea (int x, int y, int width, int height,
int deltaX, int deltaY)
-
Copies the rectangle defined by the upper-left corner (x,
y) with width of width and height of height
to a position moved by deltaX and deltaY pixels.
-
void draw3DRect (int x, int y, int width, int height,
boolean raised)
-
Draws a 3-D rectangle. It appears raised if raised is true.
-
void drawArc (int x, int y, int width, int height,
int startAngle, int arcAngle)
-
Draws an arc. The arc is inscribed in the rectangle defined by the upper-
left corner (x, y) with width of width and height
of height. It starts at startAngle degrees and goes
counterclockwise for arcAngle degrees.
-
void drawLine (int x1, int y1, int x2, int y2)
-
Draws a line from (x1, y1) to (x2, y2).
-
void drawMapleLeaf (int x, int y, int width, int height)
-
Draws a maple leaf. The maple leaf is inscribed in the rectangle
defined by the upper-left corner (x, y) with width
of width and height of height.
-
void drawOval (int x, int y, int width, int height)
-
Draws an ellipse. The ellipse is inscribed in the rectangle defined
by the upper-left corner (x, y) with width of
width and height of height.
-
void drawPolygon (int[] xPoints, int[] yPoints, int numPoints)
-
Draws a polygon. The xPoints and yPoints arrays define
the coordinates of the array of vertices. numPoints specifies
the number of vertices in the polygon.
-
void drawRect (int x, int y, int width, int height)
-
Draws a rectangle with upper-left corner at (x, y) with
width of width and height of height.
-
void drawRoundRect (int x, int y, int width, int height,
int arcWidth, int arcHeight)
-
Draws a rectangle with rounded corners with upper-left corner at
(x, y) with width of width and height of
height. arcWidth and arcHeight are the
width and height of the ellipse used to draw the rounded corners.
-
void drawStar (int x, int y, int width, int height)
-
Draws a star. The star is inscribed in the rectangle defined by
the upper-left corner (x, y) with width of
width and height of height.
-
void drawString (String str, int x, int y)
-
Draws the string str at the starting point (x, y).
The y coordinate is the base line of the text.
-
void fill3DRect (int x, int y, int width, int height, boolean raised)
-
Draws a filled 3-D rectangle. It appears raised if raised is true.
-
void fillArc (int x, int y, int width, int height,
int startAngle, int arcAngle)
-
Draws a filled arc. The arc is inscribed in the rectangle defined by the
upper-left corner (x, y) with width of width and
height of height. It starts at startAngle degrees and
goes counterclockwise for arcAngle degrees.
-
void fillMapleLeaf (int x, int y, int width, int height)
-
Draws a filled maple leaf. The maple leaf is inscribed in the rectangle
defined by the upper-left corner (x, y) with width of
width and height of height.
-
void fillOval (int x, int y, int width, int height)
-
Draws a filled ellipse. The ellipse is inscribed in the rectangle
defined by the upper-left corner (x, y) with width
of width and height of height.
-
void fillPolygon (int[] xPoints, int[] yPoints, int numPoints)
-
Draws a filled polygon. The xPoints and yPoints arrays
define the coordinates of the array of vertices. numPoints
specifies the number of vertices in the polygon.
-
void fillRect (int x, int y, int width, int height)
-
Draws a filled rectangle with upper-left corner at (x, y)
with width of width and height of height.
-
void fillRoundRect (int x, int y, int width, int height,
int arcWidth, int arcHeight)
-
Draws a filled rectangle with rounded corners with upper-left corner
at (x, y) with width of width and height of
height. arcWidth and arcHeight are the
width and height of the ellipse used to draw the rounded corners.
-
void fillStar (int x, int y, int width, int height)
-
Draws a filled star. The star is inscribed in the rectangle defined
by the upper-left corner (x, y) with width of
width and height of height.
-
int getWidth ()
-
Returns the width of the Console window in pixels.
-
int getHeight ()
-
Returns the height of the Console window in pixels.
-
void setColor (Color c)
-
Sets the color of the graphics context. The color is used for any draw
methods.
-
void setFont (Font f)
-
Sets the font of the graphics context. The font is used with the
drawString method.
-
void setPaintMode ()
-
Sets the graphics context into paint mode. All drawing in the graphics
context draws over the background.
-
void setXORMode (Color c)
-
Sets the graphics context into XOR mode. All drawing in the graphics
context is XOR'd with the background. The color specified by c is a
special color so that any drawing done on a background of color c will
not be changed.
[Table of Contents] *
[hsa Classes] *
[Console] *
[
HSA Class Library Overview]
Text Input and Output Methods
-
void clear ()
-
Clears the entire Console window and sets the cursor to the upper-left
corner.
-
int getMaxColumns ()
-
Returns the width of the Console window in columns.
-
int getMaxRows ()
-
Returns the height of the Console window in rows.
-
int getColumn ()
-
Returns the column number of the current cursor position.
-
int getRow ()
-
Returns the row number of the current cursor position.
-
void print (byte b)
void print (short s)
void print (int i)
void print (long l)
void print (float f)
void print (double d)
void print (boolean b)
void print (char c)
void print (String s)
-
Outputs the argument to the Console window beginning at the cursor
position.
-
void print (byte number, int fieldSize)
void print (short number, int fieldSize)
void print (int number, int fieldSize)
void print (long number, int fieldSize)
-
Outputs number to the Console window in a field of width
fieldSize beginning at the cursor position. If the number
(when converted to String) is larger than fieldSize,
fieldSize is ignored. The output is right justified
within the field.
-
void print (float number, int fieldSize)
void print (double number, int fieldSize)
-
Outputs number to the Console window in a field of width
fieldSize beginning at the cursor position. The
method adjusts the number of decimal places displayed to
fit in the field, if possible. If the argument (when converted
to String) is larger than fieldSize, fieldSize
is ignored. The output is right justified within the field.
-
void print (boolean b, int fieldSize)
void print (char c, int fieldSize)
void print (String s, int fieldSize)
-
Outputs the argument to the Console window in a field of width
fieldSize beginning at the cursor position. If the
argument (when converted to String) is larger than fieldSize,
fieldSize is ignored. The output is left justified.
-
void print (double number, int fieldSize, int decimalPlaces)
void print (float number, int fieldSize, int decimalPlaces)
-
Outputs number to the Console window in a field of width
fieldSize with decimalPlaces number of decimal places
beginning at the cursor position. The field width is ignored if the
number will not fit. The output is right justified.
-
void println ()
-
Outputs a Return at the cursor position to the Console window.
-
void println (byte b)
void println (short s)
void println (int i)
void println (long l)
void println (float f)
void println (double d)
void println (boolean b)
void println (char c)
void println (String s)
void println (byte b, int fieldSize)
void println (short s, int fieldSize)
void println (int i, int fieldSize)
void println (long l, int fieldSize)
void println (float f, int fieldSize)
void println (double d, int fieldSize)
void println (boolean b, int fieldSize)
void println (char c, int fieldSize)
void println (String s, int fieldSize)
void println (float f, int fieldSize, int decimalPlaces)
void println (double d, int fieldSize, int decimalPlaces)
-
Identical to print method with same arguments, except that a Return is
output after the argument.
-
byte readByte ()
-
Returns the byte value (-128 to 127) read from the keyboard.
-
short readShort ()
-
Returns the 2-byte integer (-32,768 to 32,767) read from the keyboard.
-
int readInt ()
-
Returns the 4-byte integer (-2,147,483,648 to 2,147,483,647) read
from the keyboard.
-
long readLong ()
-
Returns the 8-byte integer read from the keyboard.
-
float readFloat ()
-
Returns the 4-byte float read from the keyboard.
-
double readDouble ()
-
Returns the 8-byte double read from the keyboard.
-
boolean readBoolean ()
-
Returns the boolean value (either true or false, case
insensitive) read from the keyboard.
-
char readChar ()
-
Returns the character read from the keyboard.
-
String readString ()
-
Returns the String read from the keyboard.
-
String readLine ()
-
Returns the entire line of input read from the keyboard without the
Return.
-
void setTextColor (Color c)
-
Sets the color for text output from print and println methods.
-
void setTextBackgroundColor (Color c)
-
Sets the background color for text output from print and println methods.
-
void showCursor ()
-
Shows the text cursor, if invisible.
-
void hideCursor ()
-
Hides the text cursor, if visible.
-
void setCursor (int row, int col)
-
Sets the cursor position to row row and column col.
[Table of Contents] *
[hsa Classes] *
[Console] *
[
HSA Class Library Overview]
Example
import java.awt.*;
import hsa.Console;
public class TestConsole
{
public static void main (String[] args)
{
Console c = new Console ();
c.print ("Enter the radius of the circle: ");
int radius = c.readInt ();
c.setColor (Color.green);
c.fillOval (c.getWidth () / 2 - radius, c.getHeight () / 2 - radius,
radius * 2, radius * 2);
} // main method
} /* TestConsole class */
[Table of Contents] *
[hsa Classes] *
[
HSA Class Library Overview]
TextConsole
[Table of Contents] *
[hsa Classes] *
[TextConsole] *
[
HSA Class Library Overview]
Introduction
The TextConsole window is a 25x80 window used for text output. As the
output scrolls off the top of the window, a scroll bar is activated that
allows users to view all output from the program. The TextConsole class
differs from the Console class in that all text output is kept and can
be saved or printed.
The user can input data directly into the Console window. The output of
the program also appears there. Formatted text output is provided.
The TextConsole class window has three buttons. The Save button saves
all output sent to the window to a text file. The Print
button prints out all of the output. The Quit button quits
the program immediately. It gives visual notification when the program
is finished execution by changing the label in the Quit button
to Close.
[Table of Contents] *
[hsa Classes] *
[TextConsole] *
[
HSA Class Library Overview]
General Methods and Constructors
-
TextConsole ()
-
Constructor - Creates a TextConsole window of 25 rows by 80 columns.
-
TextConsole (int fontSize)
-
Constructor - Creates a TextConsole window with the text size set to
fontSize.
-
TextConsole (int rows, int cols)
-
Constructor - Creates a TextConsole window with width of cols
columns and height of rows rows.
-
TextConsole (int rows, int cols, int fontSize)
-
Constructor - Creates a TextConsole window with width of cols
columns, height of rows rows, and the text size set to
fontSize.
-
TextConsole (String title)
-
Constructor - Creates a TextConsole window and sets the window title to
title.
-
TextConsole (int fontSize, String title)
-
Constructor - Creates a TextConsole window with the text size set to
fontSize and sets the window title to title.
-
TextConsole (int rows, int cols, String title)
-
Constructor - Creates a TextConsole window with width of cols
columns, height of rows rows, and the window title set to
title.
-
TextConsole (int rows, int cols, int fontSize, String title)
-
Constructor - Creates a TextConsole window with width of cols
columns, height of rows rows, the text size set to
fontSize, and the window title set to title.
-
void close ()
-
Closes the TextConsole window and disposes of its contents.
[Table of Contents] *
[hsa Classes] *
[TextConsole] *
[
HSA Class Library Overview]
Text Input and Output Methods
-
void print (byte b)
void print (short s)
void print (int i)
void print (long l)
void print (float f)
void print (double d)
void print (boolean b)
void print (char c)
void print (String s)
-
Outputs the argument to the Console window beginning at the cursor
position.
-
void print (byte number, int fieldSize)
void print (short number, int fieldSize)
void print (int number, int fieldSize)
void print (long number, int fieldSize)
-
Outputs number to the Console window in a field of width
fieldSize beginning at the cursor position. If the number
(when converted to String) is larger than fieldSize,
fieldSize is ignored. The output is right justified
within the field.
-
void print (float number, int fieldSize)
void print (double number, int fieldSize)
-
Outputs number to the Console window in a field of width
fieldSize beginning at the cursor position. The
method adjusts the number of decimal places displayed to
fit in the field, if possible. If the argument (when converted
to String) is larger than fieldSize, fieldSize
is ignored. The output is right justified within the field.
-
void print (boolean b, int fieldSize)
void print (char c, int fieldSize)
void print (String s, int fieldSize)
-
Outputs the argument to the Console window in a field of width
fieldSize beginning at the cursor position. If the
argument (when converted to String) is larger than fieldSize,
fieldSize is ignored. The output is left justified.
-
void print (double number, int fieldSize, int decimalPlaces)
void print (float number, int fieldSize, int decimalPlaces)
-
Outputs number to the Console window in a field of width
fieldSize with decimalPlaces number of decimal places
beginning at the cursor position. The field width is ignored if the
number will not fit. The output is right justified.
-
void println ()
-
Outputs a Return at the cursor position to the Console window.
-
void println (byte b)
void println (short s)
void println (int i)
void println (long l)
void println (float f)
void println (double d)
void println (boolean b)
void println (char c)
void println (String s)
void println (byte b, int fieldSize)
void println (short s, int fieldSize)
void println (int i, int fieldSize)
void println (long l, int fieldSize)
void println (float f, int fieldSize)
void println (double d, int fieldSize)
void println (boolean b, int fieldSize)
void println (char c, int fieldSize)
void println (String s, int fieldSize)
void println (float f, int fieldSize, int decimalPlaces)
void println (double d, int fieldSize, int decimalPlaces)
-
Identical to print method with same arguments, except that a Return is
output after the argument.
-
byte readByte ()
-
Returns the byte value (-128 to 127) read from the keyboard.
-
short readShort ()
-
Returns the 2-byte integer (-32,768 to 32,767) read from the keyboard.
-
int readInt ()
-
Returns the 4-byte integer (-2,147,483,648 to 2,147,483,647) read
from the keyboard.
-
long readLong ()
-
Returns the 8-byte integer read from the keyboard.
-
float readFloat ()
-
Returns the 4-byte float read from the keyboard.
-
double readDouble ()
-
Returns the 8-byte double read from the keyboard.
-
boolean readBoolean ()
-
Returns the boolean value (either true or false, case
insensitive) read from the keyboard.
-
char readChar ()
-
Returns the character read from the keyboard.
-
String readString ()
-
Returns the String read from the keyboard.
-
String readLine ()
-
Returns the entire line of input read from the keyboard without the
Return.
[Table of Contents] *
[hsa Classes] *
[TextConsole] *
[
HSA Class Library Overview]
Example
import java.awt.*;
import hsa.TextConsole;
public class TestTextConsole
{
public static void main (String[] args)
{
TextConsole c = new TextConsole ();
c.print ("Enter the times table: ");
int multiplier = c.readInt ();
for (int count = 1 ; count <= 50 ; count++)
{
c.print (count, 3);
c.print (count * multiplier, 4);
}
} // main method
} /* TestTextConsole class */
[Table of Contents] *
[hsa Classes] *
[
HSA Class Library Overview]
Stdin
[Table of Contents] *
[hsa Classes] *
[Stdin] *
[
HSA Class Library Overview]
Introduction
This is a non-instantiated class to allow users to read all the
Java primitive data types from standard input before they have
learned about exception handling and the intricacies of the Java
primitive wrapper classes. It also makes it possible to read
several primitives from one line of input without
having to know the StringTokenizer class.
[Table of Contents] *
[hsa Classes] *
[Stdin] *
[
HSA Class Library Overview]
Methods
-
static void close ()
-
Closes standard input to prevent further reading.
-
static boolean eof ()
-
Returns true if the next thing to be read from the keyboard is the
end-of-file marker. (You can generate an end-of-file by pressing
either Ctrl+Z or Ctrl+D.)
-
static byte readByte ()
-
Returns the byte value (-128 to 127) read from the keyboard.
-
static short readShort ()
-
Returns the 2-byte integer (-32,768 to 32,767) read from the keyboard.
-
static int readInt ()
-
Returns the 4-byte integer (-2,147,483,648 to 2,147,483,647) read
from the keyboard.
-
static long readLong ()
-
Returns the 8-byte integer read from the keyboard.
-
static float readFloat ()
-
Returns the 4-byte float read from the keyboard.
-
static double readDouble ()
-
Returns the 8-byte double read from the keyboard.
-
static boolean readBoolean ()
-
Returns the boolean value (either true or false, case
insensitive) read from the keyboard.
-
static char readChar ()
-
Returns the character read from the keyboard.
-
static String readString ()
-
Returns the String read from the keyboard.
-
static String readLine ()
-
Returns the entire line of input read from the keyboard without the
Return.
[Table of Contents] *
[hsa Classes] *
[Stdin] *
[
HSA Class Library Overview]
Example
See Stdout Example.
[Table of Contents] *
[hsa Classes] *
[
HSA Class Library Overview]
Stdout
[Table of Contents] *
[hsa Classes] *
[Stdout] *
[
HSA Class Library Overview]
Introduction
This is a non-instantiated class to allow users to output formatted
data to standard output without having to learn the
java.text.NumberFormat class. Users can write data to fixed length
fields and with a fixed number of decimal places.
[Table of Contents] *
[hsa Classes] *
[Stdout] *
[
HSA Class Library Overview]
Methods
-
static void print (byte b)
static void print (short s)
static void print (int i)
static void print (long l)
static void print (float f)
static void print (double d)
static void print (boolean b)
static void print (char c)
static void print (String s)
-
Outputs the argument to standard output.
-
static void print (byte number, int fieldSize)
static void print (short number, int fieldSize)
static void print (int number, int fieldSize)
static void print (long number, int fieldSize)
-
Outputs number to standard output in a field of width
fieldSize. If the number (when converted to String) is
larger than fieldSize, fieldSize is
ignored. The output is right justified within the field.
-
static void print (float number, int fieldSize)
static void print (double number, int fieldSize)
-
Outputs number to standard output in a field of width fieldSize.
The method adjusts the number of decimal places displayed to fit in the
field, if possible. If the argument (when converted to String) is
larger than fieldSize, fieldSize is ignored. The
output is right justified within the field.
-
static void print (boolean b, int fieldSize)
static void print (char c, int fieldSize)
static void print (String s, int fieldSize)
-
Outputs the argument to standard output in a field of width
fieldSize. If the argument (when converted to String)
is larger than fieldSize, fieldSize is ignored.
The output is left justified.
-
static void print (double number, int fieldSize,
int decimalPlaces)
static void print (float number, int fieldSize, int decimalPlaces)
-
Outputs number to standard output in a field of width
fieldSize with decimalPlaces number of decimal
places. The field width is ignored if the number will not fit.
The output is right justified.
-
static void println ()
-
Output a Return to standard output.
-
static void println (byte b)
static void println (short s)
static void println (int i)
static void println (long l)
static void println (float f)
static void println (double d)
static void println (boolean b)
static void println (char c)
static void println (String s)
static void println (byte b, int fieldSize)
static void println (short s, int fieldSize)
static void println (int i, int fieldSize)
static void println (long l, int fieldSize)
static void println (float f, int fieldSize)
static void println (double d, int fieldSize)
static void println (boolean b, int fieldSize)
static void println (char c, int fieldSize)
static void println (String s, int fieldSize)
static void println (float f, int fieldSize, int decimalPlaces)
static void println (double d, int fieldSize, int decimalPlaces)
-
Identical to print method with same arguments, except that a Return is
output after the argument.
[Table of Contents] *
[hsa Classes] *
[Stdin] *
[Stdout] *
[FatalError] *
[
HSA Class Library Overview]
Example
import hsa.*;
// Reads and writes number to standard input and output.
public class TestStdinStdout
{
public static void main (String[] args)
{
Stdout.println ("Calculate square roots");
while (true)
{
Stdout.print ("Enter a number: ");
if (Stdin.eof ()) break;
double number = Stdin.readDouble ();
if (number < 0)
{
// Instantiating a FatalError object displays an
// error window and then quits the program.
new FatalError ("Attempted to take the square " +
"root of a negative number");
// Never reaches here.
}
Stdout.print ("The square root of ");
Stdout.print (number)
Stdout.print (" is ");
Stdout.println (Math.sqrt (number), 8, 5);
}
} // main method
} /* TestStdinStdout class */
[Table of Contents] *
[hsa Classes] *
[
HSA Class Library Overview]
TextInputFile
[Table of Contents] *
[hsa Classes] *
[TextInputFile] *
[
HSA Class Library Overview]
Introduction
This class makes it easier to read from text files. It allows the
user to read any Java primitive data type from a text file and
does all the conversions, string tokenizing, and exception handling
automatically.
[Table of Contents] *
[hsa Classes] *
[TextInputFile] *
[
HSA Class Library Overview]
Constructors and Methods
-
TextInputFile ()
-
Constructor - Opens standard input as a TextInputFile.
-
TextInputFile (File file)
-
Constructor - Opens file as a TextInputFile.
-
TextInputFile (String fileName)
-
Constructor - Opens file with name fileName as a TextInputFile.
If fileName is "Standard Input", "Keyboard"
or "Stdin" (case irrelevant), standard input is opened as a
TextInputFile.
-
void close ()
-
Closes the TextInputFile to prevent further reading.
-
boolean eof ()
-
Returns true if the next thing to be read from the file is the
end-of-file marker. (When reading from standard input, you can
generate an end-of- file by pressing either Ctrl+Z or Ctrl+D.)
-
byte readByte ()
-
Returns the byte value (-128 to 127) read from the file.
-
short readShort ()
-
Returns the 2-byte integer (-32,768 to 32,767) read from the file.
-
int readInt ()
-
Returns the 4-byte integer (-2,147,483,648 to 2,147,483,647) read
from the file.
-
long readLong ()
-
Returns the 8-byte integer read from the file.
-
float readFloat ()
-
Returns the 4-byte float read from the file.
-
double readDouble ()
-
Returns the 8-byte double read from the file.
-
boolean readBoolean ()
-
Returns the boolean value (either true or false, case
insensitive) read from the file.
-
char readChar ()
-
Returns the character read from the file.
-
String readString ()
-
Returns the String read from the file.
-
String readLine ()
-
Returns the entire line of input read from the file without the
Return.
[Table of Contents] *
[hsa Classes] *
[TextInputFile] *
[
HSA Class Library Overview]
Example
import hsa.*;
// Reads a file of integers and calculate their square roots
public class TestTextInputFile
{
public static void main (String[] args)
{
String fileName;
TextInputFile f;
int lineNumber = 1;
Stdout.print ("Enter the file to be read: ");
fileName = Stdin.readLine ();
f = new TextInputFile (fileName);
while (!f.eof ())
{
double number = f.readDouble ();
Stdout.print (number, 10, 4);
Stdout.print (" ");
Stdout.println (Math.sqrt (number), 10, 4);
}
f.close ();
} // main method
} /* TestTextInputFile class */
[Table of Contents] *
[hsa Classes] *
[
HSA Class Library Overview]
TextOutputFile
[Table of Contents] *
[hsa Classes] *
[TextOutputFile] *
[
HSA Class Library Overview]
Introduction
This class makes it easier to write to text files. It's major
convenience is that it handles formatted output. Users can specify
field length when outputting primitive data type as well as the
number of decimal places when outputting real numbers.
[Table of Contents] *
[hsa Classes] *
[TextOutputFile] *
[
HSA Class Library Overview]
Constructors and Methods
-
TextOutputFile ()
-
Constructor - Opens standard output as a TextOutputFile.
-
TextOutputFile (File file)
-
Constructor - Opens file as a TextOutputFile.
-
TextOutputFile (String fileName)
-
Constructor - Opens file with name fileName as a TextOutputFile.
If fileName is "Standard Output", "Screen"
or "Stdout" (case irrelevant), standard output is opened as
a TextOutputFile.
-
TextOutputFile (File file, boolean append)
-
Constructor - Opens file as a TextOutputFile. If append
is true, any output will be appended to the file.
-
TextOutputFile (String fileName, boolean append)
-
Constructor - Opens file with name fileName as a TextOutputFile.
If append is true, any output will be appended to the file. If
fileName is "Standard Output", "Screen"
or "Stdout" (case irrelevant), standard output is opened
as a TextOutputFile.
-
void close ()
-
Closes the TextOutputFile to prevent further writing.
-
void print (byte b)
void print (short s)
void print (int i)
void print (long l)
void print (float f)
void print (double d)
void print (boolean b)
void print (char c)
void print (String s)
-
Outputs the argument to the file.
-
void print (byte number, int fieldSize)
void print (short number, int fieldSize)
void print (int number, int fieldSize)
void print (long number, int fieldSize)
-
Outputs number to the file in a field of width
fieldSize. If the number (when converted to String) is
larger than fieldSize, fieldSize is
ignored. The output is right justified within the field.
-
void print (float number, int fieldSize)
void print (double number, int fieldSize)
-
Outputs number to the file in a field of width fieldSize.
The method adjusts the number of decimal places displayed to fit in the
field, if possible. If the argument (when converted to String) is
larger than fieldSize, fieldSize is ignored. The
output is right justified within the field.
-
void print (boolean b, int fieldSize)
void print (char c, int fieldSize)
void print (String s, int fieldSize)
-
Outputs the argument to the file in a field of width
fieldSize. If the argument (when converted to String)
is larger than fieldSize, fieldSize is ignored.
The output is left justified.
-
void print (double number, int fieldSize,
int decimalPlaces)
void print (float number, int fieldSize, int decimalPlaces)
-
Outputs number to the file in a field of width
fieldSize with decimalPlaces number of decimal
places. The field width is ignored if the number will not fit.
The output is right justified.
-
void println ()
-
Output a Return to the file.
-
void println (byte b)
void println (short s)
void println (int i)
void println (long l)
void println (float f)
void println (double d)
void println (boolean b)
void println (char c)
void println (String s)
void println (byte b, int fieldSize)
void println (short s, int fieldSize)
void println (int i, int fieldSize)
void println (long l, int fieldSize)
void println (float f, int fieldSize)
void println (double d, int fieldSize)
void println (boolean b, int fieldSize)
void println (char c, int fieldSize)
void println (String s, int fieldSize)
void println (float f, int fieldSize, int decimalPlaces)
void println (double d, int fieldSize, int decimalPlaces)
-
Identical to print method with same arguments, except that a Return is
output after the argument.
[Table of Contents] *
[hsa Classes] *
[TextOutputFile] *
[
HSA Class Library Overview]
Example
import hsa.*;
// Saves a Times table to a file.
public class TestTextOutputFile
{
public static void main (String[] args)
{
String fileName;
TextOutputFile f;
int lineNumber = 1;
Stdout.print ("Enter the file to be written: ");
fileName = Stdin.readLine ();
f = new TextOutputFile (fileName);
f.print (" ", 6); // Print 6 spaces
for (int i = 1 ; i <= 12 ; i++) // Print numbers across top
{
f.print (i, 4);
}
for (int i = 1 ; i <= 12 ; i++)
{
f.print (i, 4);
f.print (" ");
for (int j = 1 ; j <= 12 ; j++)
{
f.print (i * j, 4);
}
f.println ();
}
f.close ();
} // main method
} /* TestTextOutputFile class */
[Table of Contents] *
[hsa Classes] *
[
HSA Class Library Overview]
FatalError
[Table of Contents] *
[hsa Classes] *
[FatalError] *
[
HSA Class Library Overview]
Introduction
This is a class to display fatal errors. When instantiated, it
displays a window with a specified message and a button labelled
Quit. When the button is pressed, the program immediately exits.
Helpful for users who want to add error checking to their program but
are not yet familiar with Frames and the rest of the Java class library.
[Table of Contents] *
[hsa Classes] *
[FatalError] *
[
HSA Class Library Overview]
Constructors and Methods
-
FatalError (String message)
-
Constructor - Displays message in a window titled
Fatal Error centered on the screen. When the Quit
button is pressed, the program exits immediately.
-
FatalError (String message, Frame frame)
-
Constructor - Displays message in a window titled
Fatal Error centered on the Frame frame. When the
Quit button is pressed, the program exits immediately.
[Table of Contents] *
[hsa Classes] *
[FatalError] *
[
HSA Class Library Overview]
Example
See Stdout Example.
[Table of Contents] *
[hsa Classes] *
[
HSA Class Library Overview]
Message
[Table of Contents] *
[hsa Classes] *
[Message] *
[
HSA Class Library Overview]
Introduction
This is a class to display messages. When instantiated, it displays a
window with a specified message and a button labelled OK. When the
button is pressed, the program continues execution.
[Table of Contents] *
[hsa Classes] *
[Message] *
[
HSA Class Library Overview]
Constructors and Methods
-
Message (String message)
-
Constructor - Displays message in a untitled window centered
on the screen. When the OK button is pressed, execution returns
from the call to the constructor.
-
Message (String message, String title)
-
Constructor - Displays message in a window titled
title centered on the screen. When the OK button is
pressed, execution returns from the call to the constructor.
-
Message (String message, Frame frame)
-
Constructor - Displays message in a untitled window centered
on Frame frame. When the OK button is pressed, execution
returns from the call to the constructor.
-
Message (String message, String title, Frame frame)
-
Constructor - Displays message in a window titled
title centered on Frame frame. When the OK button
is pressed, execution returns from the call to the constructor.
-
static void beep ()
-
Causes the computer to beep. Useful for catching the user's attention.
[Table of Contents] *
[hsa Classes] *
[Message] *
[
HSA Class Library Overview]
Example
import hsa.*;
// Display four messages, one after another.
public class TestMessage
{
public static void main (String[] args)
{
new Message ("This is the first message", "First message");
new Message ("This is the second message", "Second message");
new Message ("This is the third message", "Third message");
new Message ("This is the last message", "Last message");
} // main method
} /* TestMessage class */
[Table of Contents] *
[hsa Classes] *
[
HSA Class Library Overview]
Status
[Table of Contents] *
[hsa Classes] *
[Status] *
[
HSA Class Library Overview]
Introduction
A quick way of displaying changing status messages without having to
learn about Frames, and so on.
[Table of Contents] *
[hsa Classes] *
[Status] *
[
HSA Class Library Overview]
Constructors and Methods
-
Status (String message)
-
Constructor - Displays message in a untitled window
centered on the screen. The window only disappears when the
hide () method is called or the user clicks the window's Close
button (the X on the right hand side of the window's title bar).
Execution returns from the call to the constructor immediately.
-
Status (String message, String title)
-
Constructor - Displays message in a window titled
title centered on the screen. The window only disappears
when the hide () method is called or the user clicks the window's
Close button (the X on the right hand side of the window's title bar).
Execution returns from the call to the constructor immediately.
-
Status (String message, Frame frame)
-
Constructor - Displays message in a untitled window centered
on Frame frame. The window only disappears when the hide ()
method is called or the user clicks the window's Close button (the
X on the right hand side of the window's title bar). Execution
returns from the call to the constructor immediately.
-
Status (String message, String title, Frame frame)
-
Constructor - Displays message in a window titled
title centered on Frame frame. The window
only disappears when the hide () method is called or
the user clicks the window's Close button (the X on the
right hand side of the window's title bar). Execution returns
from the call to the constructor immediately.
-
void setMessage (String message)
-
Changes the message in the window to message.
-
void hide ()
-
Hides the window.
-
void show ()
-
Shows the window.
-
void dispose ()
-
Hides the window and disposes of its contents.
[Table of Contents] *
[hsa Classes] *
[Status] *
[
HSA Class Library Overview]
Example
import hsa.*;
// Count number of lines in a file
public class TestStatus
{
public static void main (String[] args)
{
String fileName;
TextInputFile f;
Status status;
int lineNumber = 1;
Stdout.print ("Enter the file to have lines counted: ");
fileName = Stdin.readLine ();
f = new TextInputFile (fileName);
// Open status window.
status = new Status ("Reading " + fileName);
while (!f.eof ())
{
f.readLine ();
// Change status message.
status.setMessage ("Reading line number " + lineNumber);
lineNumber++;
}
// Close status window.
status.dispose ();
f.close (); // Close the text file.
Stdout.println ("There were " + lineNumber +
" lines in " + fileName);
} // main method
} /* TestStatus class */
[ Java Home ] *
[ Top of Page ] *
[ Feedback ]