Using a User Defined Method to Find Factorials and Combinations

 

Introduction.  Now we will work through another page that traps for errors and makes use of one method within another.  It's not a big difference from what we've done, but it provides another example.

You should call this JSP page enumeration.jsp.

 

 

<html>
<head>
<title>A Sticky Form for Enumerating Subsets</title>
</head>
<body bgcolor = "003344" text="cccccc">
<%
String str_setsize = request.getParameter("txt_setsize");
if (str_setsize == null) str_setsize = "";

String str_subsetsize = request.getParameter("txt_subsetsize");
if (str_subsetsize == null) str_subsetsize = "";

String str_factorial = request.getParameter("txt_factorial");
if (str_factorial == null) str_factorial = "";

String str_combinations = request.getParameter("txt_combinations");
if (str_combinations == null) str_combinations = "";

long setsize = 0;
long subsetsize = 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_setsize.length( ) == 0)
{

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

}
else
{

try
{

setsize = Long.parseLong(str_setsize);

}
catch (NumberFormatException nfeInteger)
{

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

}

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

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

}
else
{

try
{

subsetsize = Long.parseLong(str_subsetsize);

}
catch (NumberFormatException nfeInteger)
{

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

}

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

str_factorial = "";
str_combinations = "";
str_setsize = "";
str_subsetsize = "";
out.println(skipped_input);

}
else
{

str_factorial = Long.toString(computeFactorial(setsize));
str_combinations = Long.toString(computeCombinations(setsize, subsetsize));

}

}
%>
<form action="enumeration.jsp" method = "POST">
<table>
<tr>
<td><font size=4>An integer for the size of the overall set:</font>
</td>
<td><input type="text" name="txt_setsize" value="<%=str_setsize%>">
</td>
</tr>
<tr>
<td><font size=4>An integer for the size of the subsets:</font>
</td>
<td><input type="text" name="txt_subsetsize" value="<%=str_subsetsize%>">
</td>
</tr>
<tr>
<td><font size=4>Compute:</font>
</td>
<td><input type="submit" name="cmd_compute" value="Compute!">
</td>
</tr>
<tr>
<td><font size=4>The number of orderings of the set:</font>
</td>
<td><input type="text" name="txt_factorial" value="<%=str_factorial%>">
</td>
</tr>
<tr>
<td><font size=4>The number of subsets of the set:</font>
</td>
<td><input type="text" name="txt_factorial" value="<%=str_combinations%>">
</td>
</tr>
</table>
</form>
</body>
</html>
<%!
// a user defined method to compute the factorial
long computeFactorial(long setsize)
{

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

return accumulation;

}

// a user defined method to compute the combinations
long computeCombinations(long upperLimit, long subset)
{

return computeFactorial(upperLimit)/(computeFactorial(subset)*computeFactorial(upperLimit - subset));

}
%>

 

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 other than it uses the computeFactorial( ) method within the computeCombinations( ).