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 first input is the base of the expression
  • the second input is the exponent of the expression

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


import java.awt.Container;
import javax.swing.*;

public class GetPower extends JApplet

public void init( )
String output = "";

String expBase = JOptionPane.showInputDialog("Please enter the base of the exponent:");

String expPower = JOptionPane.showInputDialog("Please enter the exponent:");

// omitting error trapping
// parsing to data type long

long intExponentBase = Long.parseLong(expBase);
long intExponentPower = Long.parseLong(expPower);

JTextArea outputArea = new JTextArea( 10, 30 );

// get the applet's GUI component display area
Container c = getContentPane();

// attach output area to container c
c.add( outputArea );

// invoking the method
long expResult = findPower( intExponentBase, intExponentPower );

// displaying the results along with the inputs
output = "\nThe " + intExponentPower + "th power of " + intExponentBase + " is " + expResult + "\n";


} // end method init( )

// findPower method definition
public long findPower( long bottom, long top )

long accumulation = 1;

// running a for loop from one
// to the power = top

for (int i = 1; i <= top; i++)

accumulation = accumulation * bottom;

return accumulation;

} // end method findPower( )

} // end class GetPower


Before we discuss the program you should develop the HTML file called GetPower.html.


<applet code="GetPower.class" width=300 height=50>


The results of the program look like the following when computing 556.



The code can be described as the following.
  • The first section imports the necessary classes from the appropriate packages
    • all of the swing package
    • the Container classes from the package awt - abstract windowing toolkit
  • The inclusive class is defined as extending JApplet from the swing package
  • Variables and objects are declared and initialized
    • a string for output
    • the inputs are obtained from the user
    • the inputs are parsed as long
    • outputArea is an instantiation of the class JTextArea from the package swing
    • an object Container c is developed using the awt package
    • the JTextArea outputArea is added to the Container called c
  • the power is obtained by calling the method findPower( ) and returning the result
    • we pass in the base
    • we pass in the exponent
  • Each result and the input that drove it are added to the output string using control characters for display
  • the outputArea's contents are set to the the output string


  • the method public long findPower( long bottom, long top ) is declared and defined
    • notice that two long arguments are passed
    • notice that a single long is returned

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.