Using a User Defined Method to Convert Temperatures

 

Introduction.  Now we will slightly modify what we did in the last webpage to format the output so that it displays only two decimal places.  We will do this by importing the DecimalFormat classes of the .text package and creating an additional user defined method.  The actual changes to the previous code will be quite small.

You should call this JSP page sticky_temperature_formatted.jsp.

 

 

<html>
<head>
<title>A Sticky Form for Converting Temperatures</title>
</head>
<body bgcolor = "003344" text="cccccc">

<%@ page import = "java.text.DecimalFormat" %>

<%
String str_degrees_fahrenheit = request.getParameter("txt_degrees_fahrenheit");
if (str_degrees_fahrenheit == null) str_degrees_fahrenheit = "";

String str_degrees_celsius = request.getParameter("txt_degrees_celsius");
if (str_degrees_celsius == null) str_degrees_celsius = "";

// 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 tempFahrenheitPresent = true;
String skipped_input = "";
// checking to ensure the temperature to be converted has sbeen entered
if (str_degrees_fahrenheit.length() == 0)
{

skipped_input = "<font size = 4>You forgot to enter a temperature in fahrenheit!<br></font>";
tempFahrenheitPresent = false;
str_degrees_celsius = "";

}


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

out.println(skipped_input);

}
else
{

str_degrees_celsius = convertToCelsius(str_degrees_fahrenheit);

}

}
%>
<form action="sticky_temperature.jsp" method = "POST">
<table>
<tr>
<td><font size=4>Degrees Fahrenheit:</font>
</td>
<td><input type="text" name="txt_degrees_fahrenheit" value="<%=str_degrees_fahrenheit%>">
</td>
</tr>
<tr>
<td><font size=4>Calculate Conversion:</font>
</td>
<td><input type="submit" name="cmd_convert" value="Convert!">
</td>
</tr>
<tr>
<td><font size=4>Degrees Celsius:</font>
</td>
<td><input type="text" name="txt_degrees_celsius" value="<%=str_degrees_celsius%>">
</td>
</tr>
</table>
</form>
</body>
</html>
<%!
// a user defined method
String convertToCelsius(String str_tempf)
{

double degrees_fahrenheit = Double.parseDouble(str_tempf);
double degrees_celsius = (degrees_fahrenheit - 32.0) * 5.0 / 9.0;
return twoDigitString(degrees_celsius);

}

String twoDigitString(double input)
{
// developing a format to set the number of digits to be displayed
DecimalFormat twoDigits = new DecimalFormat("0.00");
return twoDigits.format(input);

}
%>

 

After copying this code and uploading it and accessing it on your web you should see something like the following after entering the initial input and submitting the form for computation.

 

 

Notice how we added the page import

<%@ page import = "java.text.DecimalFormat" %>

We also added the method, twoDigitString( ) that returns a String to format a number with two decimal places.