Programming and Problem Solving with Java

(やまだぃちぅ) #1

(^664) | Applets
To stop execution, the user closes on the applet window (or browser window).
See what we mean about the factorial becoming very large very fast? 16! is 10 digits. 17!
cannot possibly be a negative number. What happened? The result was too large to store in
an integer variable, so overflow occurred. The JVM just kept going with a “garbage” value for
the number, which in this case was displayed as a negative number.


Calculator


Now let’s look at a little more complex example. In Chapter 8, we created a calculator that
performs addition and subtraction. Let’s convert it into an applet. Look back at that program
as you read through this code. As before, we embed textual comments at important places.
importjava.applet.Applet; // Applet class
importjava.awt.event.*; // Event-handling classes
importjava.awt.*; // User interface classes
The outer publicclass both extends the class Appletand implements the ActionListener
interface. Thus Calculator2is its own listener class. This fact is reflected here in the heading
and in the code where thisis the argument for the addActionListenermethod. Notice that
Free download pdf