The User-Defined Method for Finding Powers
Finding Powers.
Now we take a slight step up in
difficulty in order to obtain two different inputs from a user to find ab
where a and b are integers. But in order to improve the
effectiveness of the program we declare everything as long integers.
A short discussion of the inputs follows.
The init( ) method is used to obtain the inputs, call the method that computes the exponent and display the results. This first set of code is the program for GetPower.java. |
import java.awt.Container; import javax.swing.*; public class GetPower extends JApplet {
} // end class GetPower |
Before we discuss the program you should develop the HTML file called GetPower.html. |
<html> <applet code="GetPower.class" width=300 height=50> </applet> </html> |
The results of the program look like the following when computing 556. |
The code can be described as the following.
Notice how the variables bottom. top, accumulation and i used within the findPower( ) method are not used anyplace else. Their scope is said to be the method findPower( ). They are local variables, local to that method. The variable String output 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. |