Using a User Defined Method to Find Powers

 

Introduction.  Currently, we will work through another page that does little more than find the power of one number raised to another.  This will be relatively simple.  To make it somewhat more sophisticated we will also use try and catch blocks to error trap for input errors.

You should call this JSP page find_power.jsp.

 

 

<html>
<head>
<title>A Sticky Form for Finding Powers</title>
</head>
<body bgcolor = "005533" text="cccccc">
<%
String str_base = request.getParameter("txt_base");
if (str_base == null) str_base = "";

String str_power = request.getParameter("txt_power");
if (str_power == null) str_power = "";

String str_base_output = str_base;

String str_power_output = str_power;

String str_result = request.getParameter("txt_result");
if (str_result == null) str_result = "";

long base = 0;
long power = 0;

// checking to see if this form has been submitted
if (request.getMethod().equals("POST"))
{

// establishing a boolean variable to denote whether required inputs are present
boolean inputsPresent = true;
String skipped_input = "";
// checking to ensure the required inputs have been entered
if (str_base.length( ) == 0)
{

skipped_input = skipped_input + "<font size = 4>You forgot to enter the base<br></font>";
inputsPresent = false;

}
else
{

try
{

base = Long.parseLong(str_base);

}
catch (NumberFormatException nfeInteger)
{

skipped_input = skipped_input + "<font size = 4>You need to enter an integer for the base<br></font>";
inputsPresent = false;

}

}
if (str_power.length( ) == 0)
{

skipped_input = skipped_input + "<font size = 4>You forgot to enter a power<br></font>";
inputsPresent = false;

}
else
{

try
{

power = Long.parseLong(str_power);

}
catch (NumberFormatException nfeInteger)
{

skipped_input = skipped_input + "<font size = 4>You need to enter an integer for the power<br></font>";
inputsPresent = false;

}

}
// if all of the required fields haven't been entered
// inform the user
if (!inputsPresent)
{

str_base = "";
str_power = "";
str_base_output = "";
str_power_output = "";
str_result = "";
out.println(skipped_input);

}
else
{

str_result = Long.toString(computePower(base, power));

}

}
%>
<form action="find_power.jsp" method = "POST">
<table>
<tr>
<td align="right"><font size=4>An integer for the base:</font>
</td>
<td><input type="text" name="txt_base" value="<%=str_base%>">
</td>
</tr>
<tr>
<td align="right"><font size=4>An integer for the power:</font>
</td>
<td><input type="text" name="txt_power" value="<%=str_power%>">
</td>
</tr>
<tr>
<td>&nbsp;
</td>
<td>
</td>
</tr>
<tr>
<td align="right"><font size=4>Compute:</font>
</td>
<td><input type="submit" name="cmd_compute" value="Compute!">
</td>
</tr>
<tr>
<td>&nbsp;
</td>
<td>
</td>
</tr>
<tr>
<td colspan = 2><input type="text" name="txt_base_output" size = 3 value="<%=str_base_output%>">
<font size=4>raised to the </font>
<input type="text" name="txt_power_output" size = 3 value="<%=str_power_output%>">
<font size=4> is: </font>
<input type="text" name="txt_result" value="<%=str_result%>">
</td>
</tr>
</table>
</form>
</body>
</html>
<%!
// a user defined method to compute the power
long computePower(long lower, long upper)
{

long accumulation = 1;
for (int i=1; i <= upper; i++)
accumulation = accumulation * lower;

return accumulation;

}
%>

 

After copying this code and uploading it and accessing it on your web you should see something like the following after leaving one entry blank and putting a non-integer in the other.

 

 

Notice how all of the entries have been blanked by the code.  It is also important that the page doesn't blow up!

After entering appropriate numbers we get something like the following.

 

 

The code has nothing particularly clever, but you should see how some of our earlier programs react to bad inputs.