Historically, the phrase "input-output" (or, "I/O")
referred to the operations involved in securing input data from a keyboard
and displaying information on a screen or printer. As file processing
became prevalent in the early years of computing, I/O was expanded to
encompass using disk files for input and output. In today's world of
graphical user interfaces, the term I/O has been expanded even further
to include all of the operations involved in communicating between a
computer and its users. As we have already seen, these operations
include retrieving text that has been entered into text containers and
processing the text using Java's built-in String
class.
In this
lablet we present you with a remarkably simple, but remarkably realistic,
word-processing program (described as class
WordPro
) that
illustrates many of these facets of I/O. This program also illustrates the
use of three additional Java features that we have yet to explore. First,
the class WordPro
is not an applet, but is another type of
container called a Frame
. This means that we describe the
class in terms of a constructor function and a main()
member
function. It also means that the WordPro
program cannot be
embedded in a Web page, for example, but can be run as a stand-alone
"application." Second, we chose to make WordPro
a frame so
that we could attach a menu bar to it (which we can't do to an applet).
So, the program shows you how to describe and respond to menu choices.
Finally, because we wanted to make this program handle file I/O in a
familiar way, we make use of Java's class that implements file dialog boxes.
![]() |
In this lab, you will: | ||
![]() |
Run the Chapter 10 lablet,
|
||
![]() |
Introduce some common errors into
|
||
![]() |
Extend the program to implement a variety of common word-processing operations. |
WordPro
still suffers from a slightly annoying user interface problem, since
the menu items are always enabled. For example, it really makes no sense
to have the Paste item enabled if there's no text in the clipboard,
and it's a waste of time to have Save enabled if the current text has
been saved and isn't dirty. Play with a commercial-quality word processor,
recording when menu items are enabled and disabled, and incorporate these
features into WordPro
. Go ahead--since you're not going to distribute
your version of WordPro
, you won't have to worry about getting
embroiled in a "look and feel" lawsuit with Microsoft, Apple, or Corel.
OrderPlease
. Whenever
an exception was encountered in OrderPlease
, a message was
posted in the same text area used for display of the order. A more
realistic way of reporting such errors is by means of a dialog box that
must be acknowledged by "clicking" it away. Extend OrderPlease
to make use of dialog boxes when reporting exceptions.
Keyboard
which performs basic, type-specific input
operations through the standard system input-output window. That is, your
class should implement the following functions, which could be used by any
other program to accomplish a traditional style of console-based keyboard input.
Hint: System.in
is an InputStream
amd System.out
is a PrintStream
.
// Prompts the user with String s, // in the standard I/O window. public static void prompt(String s); // Prompts the user with String s, // then reads and returns a single integer. public static int readInt(String s); // prompts the user with String s, // then reads and returns a double public static double readDouble(String s); // Prompts the user with String s, // then reads and returns a line of text up to a return. public static String readLine(String s); // Prompts the user with String s, // then reads and returns a word as delimited // by a space or a return. public static String readWord(String s);
WordPro
into a
fully-functioning word processor by giving it the ability to print
a document. You'll want to look at the documentation of the
PrintJob
, Toolkit
, and Properties
classes.