Tải bản đầy đủ (.pdf) (20 trang)

Java Programming for absolute beginner- P27 ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (317.3 KB, 20 trang )

Java SDK
Click on the Java SDK button to get to this page. From this page, you can run the
installation file, which will install Java SDK, version 1.3.1. There are different files
depending on which operating system you have. Look for the file that is right for
your operating system. If you have any Windows version, look under the
Microsoft section. If you are using Linux, look under the Linux x86 section. If you
want to install the SDK on a Solaris machine, look under the Solaris SPARC/x86
section. You can find the instructions for installing and setting up the SDK for
your system in Chapter 1, “Getting Started.”
You can also find the Java Documentation installation here. You can either install
the documentation on your system or browse it online on Sun’s Web site by using
the URL />Source Code
All of the source code of the examples in this book can be found on the CD-ROM.
Click on the Source Code button to get to the source code page. The source code
and class files are organized under their respective chapters. Each chapter gives
you the option to either browse or download the files. If you chose to download,
click the Download button. The download file is in ZIP format, so you will need
an unzip tool, such as Winzip (included on the CD-ROM), to unpack the files into
the local directory of your choosing. If you choose to browse the files without sav-
ing them to your local disk, click Explore. A new window will open up that con-
tains the files associated with that chapter. The file extension for the source code
is .java. You can open this file with any text editor since it is a straight text file.
It is still possible to run the example programs without installing them locally.
To do this, you need to open up a shell (command prompt) and change to the
class file directory. For example, if you’re using Windows and your CD-ROM drive
letter is D, to run the
HelloWorld example from Chapter 1, you need to change to
the directory D:\Source Code\Chapter 1\. Next, you need to run the
java com-
mand on the program. (Note: you can only do this after you’ve installed the SDK.)
For example, this is what you type at your command prompt to run the


Hel-
loWorld
application: java HelloWorld.
478
J
a
v
a
P
r
o
g
r
am
m
i
n
g
f
o
r t
h
e A
b
s
o
l
ut
e B
e

gi
n
n
e
r
JavaProgAbsBeg-50A.qxd 2/25/03 8:58 AM Page 478
TEAM LinG - Live, Informative, Non-cost and Genuine!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Web Links
The Web Links section includes links to some useful Internet sites. To get to this
section, simply click the Web Links button. The links you will find there are:
Sun Java This takes you directly to Sun’s Java Web site.
().
Sun Microsystems This takes you to Sun’s main Web site. Sun
Microsystems is the innovator behind Java
technology. ().
Sun Educational Services Here you can learn how to further your Java
education and become a Sun-certified Java
programmer! ().
East Coast Games A site written by programmers that you can
use as a resource for your video game pro-
gramming endeavors. (t-
coastgames.com).
NetBeans NetBeans is an open source integrated devel-
opment environment (IDE) that you can
download and use to facilitate your Java appli-
cation programming projects (-
beans.org).
Programs
This section includes some programs that you can install and use on your system.

To get to this section, click on the Programs button. For installation instructions,
visit the Web site link, which is under the program name listing on this page. The
programs are:
Cool Edit Pro This is a demo version of a very cool audio
editing program.
The GIMP The GIMP is a powerful image-editing tool.
Just install it and thank me later. It rocks.
Internet Explorer 5.5 Microsoft’s very popular Internet browser.
You can install this latest version and run the
applet examples from the book with it.
Winzip 8.0 Winzip is a great tool for packaging and
unpacking file archives.
479
A
p
p
e
n
d
i
x
A U
s
i
n
g
t
h
e
C

D
-
R
O
M
JavaProgAbsBeg-50A.qxd 2/25/03 8:58 AM Page 479
TEAM LinG - Live, Informative, Non-cost and Genuine!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
This page intentionally left blank
TEAM LinG - Live, Informative, Non-cost and Genuine!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Reserved Words
The following list included Java’s reserved words, also known as key-
words. You cannot use them to name identifiers.
abstract default if private this
boolean do implements protected throw
break double import public throws
byte else instanceof return transient
case extends int short try
catch final interface static void
char finally long strictfp volatile
class float native super while
const for new switch
continue goto package synchronized
The boolean literals true and false, and the null literal, although not
technically keywords, cannot be used as identifiers either.
J
a
v
a

L
a
n
g
u
a
g
e
S
u
m
m
a
r
y
B
APPENDIX
JavaProgAbsBeg-50B.qxd 2/25/03 8:59 AM Page 481
TEAM LinG - Live, Informative, Non-cost and Genuine!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Primitive Data Types
Type Number of Bits
boolean 1
byte 8
short 16
char 16
int 32
float 32
long 64
double 64

Ranges for Integral Types
Type Minimum Maximum
byte -2
7
2
7
– 1
short -2
15
2
15
– 1
int -2
31
2
31
– 1
long -2
63
2
63
– 1
char 02
16
– 1
Floating-Point Constants
Float.NEGATIVE_INFINITY
Float.POSITIVE_INFINITY
Float.NaN
Double.NEGATIVE_INFINITY

Double.POSITIVE_INFINITY
Double.NaN
NaN
stands for “not a number” and represents values of undefined operations
such as
0.0 / 0.0.
482
J
a
v
a
P
r
o
g
r
am
m
i
n
g
f
o
r t
h
e A
b
s
o
l

ut
e B
e
gi
n
n
e
r
JavaProgAbsBeg-50B.qxd 2/25/03 8:59 AM Page 482
TEAM LinG - Live, Informative, Non-cost and Genuine!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
A
p
p
e
n
d
i
x
B J
a
v
a
L
a
n
g
u
a
g

e
S
u
m
m
a
r
y
Comments
There are three types of comments used in Java (single line, multi-line, and
javadoc).
This is a single line comment example:
// this is a single line comment
This is a multi-line comment example:
/* this is a multi-line
comment */
This is a javadoc comment example:
/**
* This is a javadoc comment
*/
Literals
Literals are used in Java to represent values that are of the primitive, String, or
null types in source code. The following sections summarize their syntaxes. Any-
thing within square braces (
[ and ]) is optional and the bar character (|) sepa-
rates different options. For example,
[+|-] means the code can include +, -, or
neither (because they are within square brackets).
Integer Literals
Integer literals can be expressed in octal (base 8), decimal (base 10), or hexadeci-

mal (base 16). Octal digits can be any digits from 0 to 7. Decimal digits can be any
digits ranging from 0 to 9. Hexadecimal digits can be any digit ranging from 0 to
9 and also any letter (case-insensitive) from A to F. A=10, B=11, C=12, D=13, E=14,
F=15. The syntax for an integer literal is:
[+|-][0[X|x]]number[L|l]
Example Description
67 int literal having value 67
+67 int literal having value 67
-67 negative int literal having value –67
012 octal int literal having value 10
483
JavaProgAbsBeg-50B.qxd 2/25/03 8:59 AM Page 483
TEAM LinG - Live, Informative, Non-cost and Genuine!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
-0X27C hexadecimal int literal having value –636
1234567890L long literal having value 1,234,567,890
-0XBEL hexadecimal long literal having value -190
Floating-Point Literals
A floating-point literal can be either a float or a double. The syntax for a float-
ing-point number is:
[+|-]number.number[[+|-]Eexponent|[+|-]eexponent][F|D|f|d]
Example Description
-10. double literal having value -10.0
+.01 double literal having value 0.01
1.23 double literal having value 1.23
1.23d double literal having value 1.23
1.23f float literal having value 1.23
2E4 double literal having value 20,000.0 (2x10
4
)

-133e-2F
float literal having value -1.33 (-133x10
-2
)
Boolean Literals
Boolean literals must be either true or false.
Character Literals
A character literal is a single character or escape sequence enclosed in single
quotes. The data type of a character literal is always
char. A Unicode character
escape sequence is in the form
\unnnn, where nnnn is the hexadecimal represen-
tation of the character. The syntax for a character literal is (exactly one character
or escape sequence must appear within the single quotes):
'character|escape_sequence'
Example Description
'a' char literal a
'$' char literal $
'\u003F' char literal ?
'\'' char literal ' (single quote)
'\"' char literal " (double quote)
484
J
a
v
a
P
r
o
g

r
am
m
i
n
g
f
o
r t
h
e A
b
s
o
l
ut
e B
e
gi
n
n
e
r
JavaProgAbsBeg-50B.qxd 2/25/03 8:59 AM Page 484
TEAM LinG - Live, Informative, Non-cost and Genuine!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
A
p
p
e

n
d
i
x
B J
a
v
a
L
a
n
g
u
a
g
e
S
u
m
m
a
r
y
'\b' char literal for backspace
'\f' char literal for form-feed
'\n' char literal for new line
'\r' char literal for carriage return
'\t' char literal for tab
'\\' char literal for backslash (\)
String Literals

A string literal consists of a string of characters and/or escape sequences within
double quotes. The data type is always
String. The syntax for a String literal is:
"[characters&|escape_sequences]"
Example Description
"" The empty string
"Abc" String literal Abc
"\"Java\"" String literal "Java"
"C:\\My Documents\\myfile.txt" String
literal C:\My
Documents\myfile.txt
Null Literal
The null literal is null.
Operators
In the following table, arg refers to any variable or value. Some operators only
take certain types of arguments. For example,
! only works on Boolean types.
Type Syntax Description
Unary
+arg, -arg Sign (positive or negative)
++variable Prefix increment
variable++ Postfix increment
variable Prefix decrement
variable Postfix decrement
!arg Boolean compliment (Not)
~arg Bitwise inversion
(type)arg Cast
485
JavaProgAbsBeg-50B.qxd 2/25/03 8:59 AM Page 485
TEAM LinG - Live, Informative, Non-cost and Genuine!

Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Arithmetic arg + arg Addition
arg - arg Subtraction
arg * arg Multiplication
arg / arg Division
arg % arg Modulus
Shift arg >> arg Left shift
arg >> arg Right shift
arg >>> arg Unsigned right shift
Comparison
arg < arg Less than
arg > arg Greater than
arg <= arg Less than or equal to
arg >= arg Greater than or equal to
arg == arg Equal to
arg != arg Not equal to
arg instanceof Instance of
class
Bitwise arg & arg Bitwise AND
arg | arg Bitwise OR
arg ^ arg Bitwise XOR
Logical arg && arg Logical AND
arg || arg Logical OR
Ternary condition ?
val_if_true :
val_if_false
Conditional operator
Assignment Operators
Assignment operators store a value into a variable. This section covers the opera-
tors that included the equals sign (=), however, the increment (

++) and decrement
(
) operators perform assignments as well. The assignment operator can be just
the equals sign or the equals sign followed by an additional operator (although
the combination of the two constitutes one single operator). The syntax for the
assignment operator is:
variable =[op] arg
486
J
a
v
a
P
r
o
g
r
am
m
i
n
g
f
o
r t
h
e A
b
s
o

l
ut
e B
e
gi
n
n
e
r
JavaProgAbsBeg-50B.qxd 2/25/03 8:59 AM Page 486
TEAM LinG - Live, Informative, Non-cost and Genuine!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
A
p
p
e
n
d
i
x
B J
a
v
a
L
a
n
g
u
a

g
e
S
u
m
m
a
r
y
If = is combined with another operator, the operation is logically equivalent to:
variable = variable op arg
The following are all assignment operators:
= +=-=*=/=&=|=^=%=<<=
>>= >>>=
Loops
Name Syntax
for loop for([init, init, …];[condition];[update, update, …])
{ body }
while
loop while(condition) { body }
do
loop do { body } while (condition);
Break and Continue
The break and continue statements redirect the flow of loops. break takes con-
trol out of the loop and
continue returns control to the top of the loop.
Conditionals
The conditional statements are if, switch, and also the Ternary operator
described in the “Operators” section. The syntax for the
if conditional is (the

square brackets [] indicate that the
else if and else statements are optional and
are not part of the syntax):
if (condition) {
statements_condition_true;
}
[else if (other_condition) {
statements_other_condition_true;
}, …]
[else {
statements_no_condition_true;
}]
487
JavaProgAbsBeg-50B.qxd 2/25/03 8:59 AM Page 487
TEAM LinG - Live, Informative, Non-cost and Genuine!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
The syntax for the switch conditional is:
switch (test) {
case value: [statements_test_equals_value] [break;]

[default: [statements_test_equals_no_case_value]
}
Try… Catch Blocks
Try… catch blocks are used for exception handling. Here is the syntax:
try {
statements_that_might_cause_an_exception;
} catch (exception_type exception_variable_name) {
[statements_that_execute_if_exception_is_caught]
}
Class Definition

The syntax for defining a class follows:
[package package_name;]
[[import Imported_class_or_package;] …]
[access_modifier] class class_name [extends super_class_name] [implements
implemented_Interface, …] {
[class_definition]
}
Class Modifiers
Modifier Description
public Access modifier
final Class cannot be subclassed
abstract Must be subclassed (cannot be instantiated)
Constructor Definition
The syntax for a constructor is as follows. The first line of the constructor body
must be either an explicit call to the superclass’s constructor using
super(), an
488
J
a
v
a
P
r
o
g
r
am
m
i
n

g
f
o
r t
h
e A
b
s
o
l
ut
e B
e
gi
n
n
e
r
JavaProgAbsBeg-50B.qxd 2/25/03 8:59 AM Page 488
TEAM LinG - Live, Informative, Non-cost and Genuine!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
A
p
p
e
n
d
i
x
B J

a
v
a
L
a
n
g
u
a
g
e
S
u
m
m
a
r
y
implied call to the superclass’s no-arg constructor, or a call to another construc-
tor within this class using
this():
[access_modifier] class_name([arg, …]) { [constructor_body] }
Variable Declaration
The syntax for declaring a variable is as follows:
[access_modifier] [modifier, ] type variable_name [ = initial_value];
Variable Modifiers
Modifier(s) Description
public protected private Access modifiers
static Indicates this is a class variable
final Indicates a constant

transient Indicates a variable that cannot be serialized
volatile Indicates that variable may be modified
asynchronously
Method Definition
The syntax for defining a method is as follows. Unless the return type is void, the
last executable statement of any logical path of execution within the method
must be a
return statement that returns a value of type return_type:
[access_modifier] [modifier, …] return_type method_name([arg, …]) [throws
exception, …] { [body] }
Method Modifiers
Modifier(s) Definition
public protected private Access modifiers
static Indicates this is a class method
final Indicates that this method cannot be over-
ridden
abstract Indicates the body must be defined in a
subclass
489
JavaProgAbsBeg-50B.qxd 2/25/03 8:59 AM Page 489
TEAM LinG - Live, Informative, Non-cost and Genuine!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
native Indicates the method body is defined out-
side of Java in a native library
synchronized Indicates that, at most, only one thread can
have control of this method at any given
time
490
J
a

v
a
P
r
o
g
r
am
m
i
n
g
f
o
r t
h
e A
b
s
o
l
ut
e B
e
gi
n
n
e
r
JavaProgAbsBeg-50B.qxd 2/25/03 8:59 AM Page 490

TEAM LinG - Live, Informative, Non-cost and Genuine!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
// (double slashes), 18
/ (division operator), 37
- (subtraction operator), 37
+ (addition), 37
$ (dollar sign), 31
% (modulus operator/division remainder),
37
* (multiplication operator), 37
_ (underscore), 31
3D rectangles, drawing, 319–320
A
abstract classes, 435–436
Abstract Windowing Toolkit. See AWT
access modifiers, 143–145
ActionEvents class, 247–249
action commands, 252–255
knowing source of, 248–252
methods, list of, 248–249
addition (+), 37
AdjustmentEvents class, 258–260
animations
double buffering, 367–369
ShootingRange game example, 370–376
Sprite class
overview, 363–364
testing, 365–367
applets
Applet class, 282–284

applets versus applications, 279
creating, 20–21
defined, 20, 279
destroy() method, 290–292
functions of, 20
HelloWeb applet example program,
280–281
HTML document, incorporating in,
21–22
images and, 304–306
init() method, 290–292
methods, list of, 281
QuizShow applet example, 306–310
running, 21–22
sound files, 302–304
start() method, 290–292
status messages, printing, 293
stop() method, 290–292
in Web pages, 282–284
applet HTML tag, 284–285
passing parameters to applets,
284–288
security restrictions, 290–291
using frames with, 288–290
writing Java programs as, 294
rewriting MadInputPanel example,
295–298
rewriting MadLib game example,
298–301
appleviewer utility, 20–22

applications
applications versus applets, 279
examples of. See example code
playing sound from, 369–370
arcs, drawing, 321–324
arguments, command-line arguments,
46–47
I
n
d
e
x
JavaProgAbsBeg-90Index.qxd 2/25/03 8:59 AM Page 491
TEAM LinG - Live, Informative, Non-cost and Genuine!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
arrays
ArrayTest program example, 88–89
declaring, 85–86
defined, 85
elements, assigning values to, 85–88
looping on, 104–106
multidimensional, 87–89
subscripts, 85–86
assigning values to variables, 31–34
AWT (Abstract Windowing Toolkit), 172–173
components
buttons, 184–187
canvas, 202–203
checkbox, 198–202
choice, 192–195

dialog, 211–214
labels, 182–185
list, 195–198
listof, 173–176
menu, 203–207
panel, 208–209
pop-up menu, 206–208
scrollbar, 209–213
text area, 190–193
textfield, 186–191
events, 177
Frame component, 178–180
graphics, 177–178
package classes, 172–173
B
blackjack card game
addUpPoints() method, 168
Card class, writing, 147–151
CardDeck class, 150–153
deal() method, 152
dealerBlackJack() method, 168
deck’s list() method, 126–127
explanation of, 122–123
hitDealer() method, 168
hitPlayer() method, 168
isPictureCard() method, 167
play() method, 167–168
playerBlackJack() method, 168
private hit() method, 168
RandomCardDeck class, 158–160

reset() method, 152
shuffle() method, 158–160
SimpleCardDeck class example, 123–127
source code, example of, 162–167
updatePoints() method, 168
Vector class, 160–163
Block Game
block area, 378–380
block shape, 378–380
BlockGame.java, source code listing,
423–427
BlockGrid class, creating
addBlock() method, 385–386
block area, 384–386
blockOutOfBounds() method, 387–388
collision detection, 388–389
methods, list of, 386–387
removeBlock() method, 385–386
setBlock() method, 385–386
source code listing, 389–392
Block.java application, source code
listing, 381–384
blockOutOfArea() method, 388–389
BlockTest.java application, source code
listing, 384
concept of, 378
controls, 378
createBlock() method, 423–424
introduceNextBlock() method, 423–424
PlayArea class

block movements, 401–403
EventThread inner class, 407–409
falling blocks, 404–407
inner classes, 398–401
492
I
n
d
e
x
JavaProgAbsBeg-90Index.qxd 2/25/03 8:59 AM Page 492
TEAM LinG - Live, Informative, Non-cost and Genuine!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
PlayArea.java, source code listing,
409–414
PlayAreaTest.java, source code listing,
414–415
PlayArea Event model, 394
building from scratch, 395–396
PlayAreaEvent class, 397
PlayAreaEvents, firing, 398–399
PlayAreaListener interface, 397–398
PlayAreaListeners, registering, 398
rotateClockwise() method, 380–381
rotateCountClockwise() method, 380,
381–382
ScoreInfoPanel class, 415–423
block statements, 16, 67–68
boolean isEnabled() method, 173–174
boolean isFocusTraversable() method,

173–174
boolean isLightWeight() method, 173–174
boolean logical operators, 70–72
BorderLayout layout manager, 227–229
break statement, loops and, 112–116
brighter() method, Color class, 337–338
BufferedReader class, 40–42
Button class, 184–187
buttons (AWT), 176
byte code, 5
bytes, converting strings to, 45
C
calculations, percentages, 34–35
Canvas component (AWT), 176, 202–203
CardLayout layout manager, 238–242
casting data types, 28–29
catch clause, exception handling and, 43
cd command, 12–13
CD-ROM
Java SDK, 478
programs, list of, 479
source code examples, 778
Web links, 479
character escape codes, 29–31
Checkbox component (AWT), 198–202
Choice component (AWT), 176, 192–195
class variables, 126–127
classes
abstract, 435–436
BufferedReader, 40–42

extending, 22, 154–157
importing, 22
inner, 398–399
InputStreamReader, 40
Math, 57
nested, 398–399
outer, 398–399
String, 48
subclasses, 22
super classes, 22
Vector, 160–163
CodeBase() method, 302–303
collision detection, Block Game example,
388–389
Color class
brighter() method, 337–338
color values, 340–344
ColorCanvas application, 340–341
ColorChanger application, 340–342
ColorSliders application, 343
ColorTest application, source code,
337–339
darker() method, 337–338
methods, list of, 337–338
setColor() method, 136
command-line arguments, 46–47
command-line input, 38–39
commands. See also methods
cd, 12–13
javac, 21

493
I
n
d
e
x
JavaProgAbsBeg-90Index.qxd 2/25/03 8:59 AM Page 493
TEAM LinG - Live, Informative, Non-cost and Genuine!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
comments
adding to code, 17–19
documentation comment, 435–438
ending, 18
free text, 19
javadoc, 435–438
multi-line, 18
single-line, 18
starting, 18
components
AWT, list of, 175–176
class methods, list of, 173–176
heavyweight, 431
lightweight, 431–433
compound increment assignment operator,
99–100
concatenation, 48–49
conditional operators, 68–69
conditional statements, 66–67
constant fields, declaring, 129
constructor methods, 140–142

createStory() method, 272–273
D
darker() method, Color class, 337–338
data types
casting, 28–29
defined, 26
primitive, 27
declaring
arrays, 85–86
constant fields, 129
methods, 136–137
packages, 434–435
variables, 31–32
destroy() method, 281, 290–292
Dialog component (AWT), 211–214
dispose() method, 181, 244
division operator (/), 37
do loop, 109–111
documentation comment, 435–438
dollar sign ($), 31
double buffering, 367–369
double slashes (//), 18
drawing
arcs, 321–324
images
drawImage() method, 332–333
getHeight() method, 334–335
getImage() method, 333–334
getWidth() method, 334–335
ImageTest application, source code,

333–334
ImageTest2 application, source code,
334–336
size command-line arguments, 336
lines, 315–316
ovals, 321–322
polygons, 323–326
rectangles, 317–320
strings, 325–327
drive() method, 435–436
E
encapsulation, 146–148
endless loop, 112
escape codes, 29–31
event handling
ActionEvents, 247–249
action commands, 252–255
knowing source of, 248–252
methods, list of, 248–249
AdjustmentEvents, 258–260
FocusEvents, 254–256
ItemEvents, 255–259
KeyEvents, 265–268
MouseEvents, 261–265
overview, 241–242
TextEvents, 260–262
WindowEvents, 242–245
494
I
n

d
e
x
JavaProgAbsBeg-90Index.qxd 2/25/03 8:59 AM Page 494
TEAM LinG - Live, Informative, Non-cost and Genuine!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
events, event listener interfaces, 177
example codes
AdjustmentTest application, source code,
258–260
ArcTest application, source code,
322–323
ArrayTest program, 88–89
blackjack card game. See blackjack card
game
Block Game. See Block Game
BorderLayout layout manager, source
code, 228–229
CardLayoutTest application, 238–242
CountByFive program, 99
CountDown program, 101–103
DiceRoller application, 65–67
encapsulation, 146–148
FlowLayout layout manager, source code,
223–225
FocusTest application, source code,
254–255
FortuneTeller program, 90–911
FrameTestApplet application, source
code, 289

HelloWeb applet program, 280–281
ImageTest application, source code,
333–334
ImageTest2 application, source code,
334–336
ItemTest application, source code,
255–258
KeyTest application, source code,
267–268
LineTest application, source code,
315–316
LowTemp program, 73–74
MadLib game. See MadLib game
ManyTemps program, 79–80
Math game, 43
Mine Cell. See Mine Cell
MouseTest application, source code,
262–264
NameGame example, 26
source code, 49–52
NumberGuesser application, 94
source code, 118–120
operator precedence, 37
OvalTest application, source code,
321–322
PolyTest application, source code,
323–325
QuizShowApplet, source code, 306–310
Racer program, 95–96
RectText application, source code,

318–319
ShootingRange game, source code,
370–376
SoundApplication, source code, 369–370
StringTest application, source code,
325–326
TextTest application, source code,
261–262
TipAdder program, 34–36
TipCalculator program, 45–46
user input, 38–39
exception handling
catch clause, 43
IOException, 43
loops and, 115–117
overview, 41–42
try-catch block, 116–117
extending classes, 22, 154–157
extends() keyword, 154
F
field access modifiers, 144–145
field modifiers, 130–132
final keyword, 129
floating-point numbers, 34–35
FlowLayout layout manager, 223–226
495
I
n
d
e

x
JavaProgAbsBeg-90Index.qxd 2/25/03 8:59 AM Page 495
TEAM LinG - Live, Informative, Non-cost and Genuine!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
FocusEvents, 254–256
fonts
Font class, 326–328
Font constructor method, 326–327
FontMetrics class, 331–333
getFontMetrics() method, 327–328
methods, list of, 328–329
source code, 328–331
setFont() method, 326–327
for loop, 96
counting backwards, 101–103
nested, 102–105
overview, 96
Frame component (AWT), 176
GUIFrame class, creating, 237–238
methods, list of, 177–178
using with applets, 288–290
G
getAppletInfo() method, 281, 285–286
getBackground() method, 173–174
getBound() method, 173–174
getFont() method, 173–174
getFontMetrics() method, 327–328
getForeground() method, 173–174
getHeight() method, 173–174, 334–335
getID() method, 455

getImage() method, 333–334
getInteriorSize() method, 447–448
getItem() method, 257–258
getKeyCode() method, 267
getParameter() method, 284–285
getParameterInfo() method, 285–286
getPreferredSize() method, 173–174
getSize() method, 173–174
getSource() method, 244, 465–466
getStateChange() method, 257–258
getStringArray() method, 268–269
getUserInput() method, 167
getWidth() method, 173–174, 334–335
getX() method, 173–174
getY() method, 173–174
Graphics class
Color class
ColorTest application, source code,
337–339
methods, list of, 337
drawing
arcs, 321–324
lines, 315–316
ovals, 321–322
polygons, 323–326
rectangles, 317–319
rectangles, 3D, 319–320
strings, 325–327
Memory Game example, 343–344
Memory class, creating, 348–351

MemoryCell class, creating, 344–349
overview, 314–315
paint(Graphics method), 314–315
GridBagLayout layout manager, 229–236
GridLayout layout manager, 225–228
GUI Frame class, creating, 237–238
H
handling events
ActionEvents, 247–249
action commands, 252–255
knowing source of, 248–252
methods, list of, 248–249
AdjustmentEvents, 258–260
FocusEvents, 254–256
ItemEvents, 255–259
KeyEvents, 265–268
MouseEvents, 261–265
overview, 241–242
TextEvents, 260–262
WindowEvents, 242–245
handling exceptions. See exception
handling
496
I
n
d
e
x
JavaProgAbsBeg-90Index.qxd 2/25/03 8:59 AM Page 496
TEAM LinG - Live, Informative, Non-cost and Genuine!

Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
heavyweight components, 431
HTML document, incorporating into
applets, 21–22
I
if-else statement, 74, 75–76
flowchart, 74–75
indentation, 80–81
ManyTemps program example, 79–80
nested, 77–79
syntax conventions, 80–81
if statement, random numbers and, 66–68
images
applets and, 304–306
drawing
drawImage() method, 332–333
getHeight() method, 334–335
getImage() method, 333–334
getWidth() method, 334–335
ImageTest application, source code,
333–334
ImageTest2 application, source code,
334–336
size command-line arguments, 336
import statement, 22
importing classes, 22
increment operators, 96–97
assignment operator, 99
compound assignment, 99–100
postfix, 96–97

prefix, 97–98
incremental values, 98–99
index of strings, 48–49
infinite loop, 112
inheritance, 154
init() method, 280, 290–292
inner classes, 398–399
input
BufferedReader class, 40–42
InputStreamReader class, 40
user input, example code, 38–39
InputStreamReader class, 40
installing Java Software Development Kit
Linux installation, 10–11
Solaris installation, 10
Windows (Win32) installation, 8–10
instance variables, 126–127
integer bitwise operators, 71–72
Integer.parseInt() method, 44–45
interrupt() method, 361–362
ItemEvents class, 255–259
J
Java
byte code, 5
history of, 28–29
Java Software Development Kit,
installing
Linux installation, 10–11
Solaris installation, 10
Windows (Win32) installation, 8–10

overview, 3–4
platform independence, 4–5
procedure-oriented program, 5
reasons for using, 7–8
syntax, 15
Java run-time environment (JRE), 5
javac command, 21
javadoc tags, 435–438
JRE ( Java run-time environment), 5
K
KeyEvents class, 265–268
keywords
extends, 154
final, 129
protected, 144–145
public, 16
synchronized, 360–361
this, 143
497
I
n
d
e
x
JavaProgAbsBeg-90Index.qxd 2/25/03 8:59 AM Page 497
TEAM LinG - Live, Informative, Non-cost and Genuine!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

×