Sams Teach Yourself C in 21 Days

(singke) #1
More Java Techniques 763

BD6


Lines 17 through 20 define a method that will respond to mouse events—in this case,
specifically to the mouse being clicked over the applet. The code in this method simply
toggles the useRedflag from truetofalseor from falsetotrue, and then it calls the
repaintmethod.Repaintis a method of the Appletsuperclass. One of the things it does
is call theAppletTestobject’s paintmethod to redisplay the text in the new color.

<APPLET>is one of many tags used by HTML, or hypertext markup language,
to create Web pages. You’ll need to learn a bit about HTML to effectively
create Web pages, although some Web design tools do most of the work of
generating tags for you.

Caution


Summary ............................................................................................................


Today you wrapped up your introduction to Java. It differs from C and C++ in that it
requires your programs to be object-oriented. This makes it a little trickier to learn. After
you get started, however, you’ll see that Java really makes a lot of programming tasks
easier. It also reduces the effort required for debugging and maintaining the program
down the road. In addition, with applets, your Java expertise can be extended to the Web.
In tomorrow’s final bonus day, you’ll learn about one of the newest programming lan-
guages, C#.

Q&A ....................................................................................................................


Q How does Java handle runtime errors?
AIn Java, errors are called exceptions. Any section of code that may generate an
exception should be enclosed in a tryblock. Every tryblock must be accompa-
nied by one or more catchblocks to deal with the exceptions that may have
occurred (been thrown) in that code.
Q Do you have to use exception handling in your Java programs?
AIt depends. Some Java classes are defined to require exception handling when the
classes are used, for example the classes that relate to file input and output (where
the possibilities of exceptions occurring are high). In other situations, exception
handling is not absolutely required, but as with any programming language it’s
always a good idea to include code to permit your programs to deal with runtime
errors gracefully.

41 448201x-Bonus6 8/13/02 11:23 AM Page 763

Free download pdf