The User-Defined Method for Finding Squares
Finding Squares.
Now we start with a relatively
simple applet to find squares of a series of numbers. The squares
are computed within a method. The other innovation in the code is
that the output is displayed within the applet window rather than in a
JOptionPane. This first set of code is the program for SquareInt.java. |
import java.awt.Container; import javax.swing.*; public class SquareInt extends JApplet {
} // end class SquareInt |
Before we discuss the program you should develop the HTML file called SquareInt.html. |
<html> <applet code="SquareInt.class" width=300 height=350> </applet> </html> |
The results of the program look like the following. |
The code can be described as the following.
Notice how the variable y used within the square( ) method is not used anyplace else. It's scope is said to be the method square( ). It is a local variable, local to that method The variable int i is declared within the init( ) method and has scope only within it. It is local to that method. This terminology is important and we will start to consistently discuss these sorts of issues. |