Java Reference
In-Depth Information
might try to divide by zero (an illegal operation for whole-number arithmetic in Java).
This would cause the Java program to display an error message. If this occurred, you'd
have to return to the edit phase, make the necessary corrections and proceed through the
remaining phases again to determine that the corrections fixed the problem(s). [
Note:
Most programs in Java input or output data. When we say that a program displays a mes-
sage, we normally mean that it displays that message on your computer's screen. Messages
and other data may be output to other devices, such as disks and hardcopy printers, or even
to a network for transmission to other computers.]
Common Programming Error 1.1
Errors such as division by zero occur as a program runs, so they're called
runtime errors
or
execution-time errors
.
Fatal runtime errors
cause programs to terminate immediately
without having successfully performed their jobs.
Nonfatal runtime errors
allow pro-
grams to run to completion, often producing incorrect results.
In this section, you'll run and interact with your first Java application. The
Painter
appli-
cation, which you'll build over the course of several exercises, allows you to drag the mouse
to “paint.” The elements and functionality you see here are typical of what you'll learn to
program in this topic. Using the
Painter
's graphical user interface (GUI), you can control
the drawing color, the shape to draw (line, rectangle or oval) and whether the shape is filled
with the drawing color. You can also undo the last shape you added to the drawing or clear
the entire drawing. [
Note:
We use fonts to distinguish between features. Our convention
is to emphasize screen features like titles and menus (e.g., the
File
menu) in a semibold
sans-serif Helvetica
font and to emphasize nonscreen elements, such as file names, pro-
gram code or input (e.g.,
ProgramName.java
), in a
sans-serif
Lucida
font.]
The steps in this section show you how to execute the
Painter
app from a
Command
Prompt
(Windows),
Terminal
(OS X) or shell (Linux) window on your system.
Throughout the topic, we'll refer to these windows simply as
command windows
. Perform
the following steps to use the
Painter
application to draw a smiley face:
1.
Checking your setup.
Read the Before You Begin section to confirm that you've
set up Java properly on your computer, that you've copied the topic's examples
to your hard drive and that you know how to open a command window on your
system.
2.
Changing to the completed application's directory.
Open a command window and
use the
cd
command to change to the directory (also called a
folder
) for the
Painter
application. We assume that the topic's examples are located in
C:\examples
on
Windows or in your user account's
Documents/examples
folder on Linux/OS X.
On Windows type
cd C:\examples\ch01\painter
, then press
Enter
. On Linux/
OS X, type
cd ~/Documents/examples/ch01/painter
, then press
Enter
.
3.
Running the
Painter
application.
Recall that the
java
command, followed by the
name of the application's
.class
file (in this case,
Painter
), executes the appli-
cation. Type the command
java Painter
and press
Enter
to execute the app.
Figure 1.11 shows the app running on Windows, Linux and OS X, respective-
ly—we shortened the windows to save space.