|
Beginner’s Guide to Web Development – Writing Java Methods
Sometimes when writing JSP code you will find that you use the same piece of code over and over again. For example,
let’s say you are writing a page that needs to calculate the money paid for a few different stocks given the share price,
number of shares and commission. One way to do this would be to write logic each time you needed to do a calculation.
For example, let’s say stock ABC was bought at 10 dollars a share, 100 shares were bought, and the commission is 10 percent of the
total price.
Let’s also say that stock XYZ was bought at 5 dollars a share, 50 shares were bought and the commission is 10 percent of the total
price. You could do this:
double price1 = (100 * 10) + (100*10*0.1);
double price2 = (50 * 5) + (50*5*0.1);
Notice that you are essentially writing that same code twice. What happens if your equation for stock price needs to change and
you need to add another factor, such as an interest rate. Then, you have to change the code in two places. However, if you would have
used a method to write the code, you would only need to make the change once. Methods give you the
ability
to reuse code, and by doing so, reduce maintenance time.
We have yet to say what a method is. A method is simply a segment of code that performs an operation. The values a method uses
to perform
this operation can either be passed into the method or contained within the method. Let’s start with an example:
double getMoneyPaid (int shares, double price, double commission)
{
double x;
x = (shares * price) + (shares * price * commission);
return x;
}
The above method takes three arguments – the number of shares, the price of shares, and the commission. The method then
performs calculations on these arguments and returns the calculated values. The data type we use is the double since there
is the possibility we are dealing with decimal points.
The structure of a method is the following:
<return type> <name of method> ( <arg1 type arg1name>, <arg2 type arg2 name, . . .)
{
<method code>
return <return type>
}
Notice the line of code "return x;" in the example method above. Since the return type of this method is a
double, we have to return a double. You can return a value with the "return" statement. The only type of method in which
you would not return a value would be a method of type "void". Void simply means the method does not return any
data. The return type of a method can be any Java primitive or Object.
Let’s go back to our original example and change our code to use the above method.
Our code now looks like this:
double price1 = getMoneyPaid (100, 10.00, 0.1);
double price2 = getMoneyPaid (50, 5.00, 0.1);
As you can see, to call a method you simply type the method name and pass it the appropriate values. If the method
has a return type other than void, you must create a variable to accept the return value from the method. The variables
we created in the example were price1 and price2. After execution of the two lines of code above, price1 will have the
value 1100.0 and price2 will have the value 275.0.
Now that we’ve gone over methods, let’s look at how we would use methods inside of a JSP page.
Type the following into your text editor:
<%!
double getMoneyPaid (int shares, double price, double commission)
{
double x;
x = (shares * price) + (shares * price * commission);
return x;
}
%>
<html>
<head>
<title>Method JSP</title>
<head;>
<body>
<%
double price1 = getMoneyPaid (100, 10.00, 0.1);
out.print ("price1 is " +price1);
%>
<br>
<br>
<%
double price2 = getMoneyPaid (50, 5.00, 0.1);
out.print ("price2 is " +price2);
%>
</body>
</html>
Save this file as method.jsp under the c:\tomcat\webapps\examples\jsp\work\ directory. Make sure that tomcat is
running, and go to the following URL:
http://localhost:8080/examples/jsp/work/method.jsp
This concludes our overview of methods. You will see more examples of methods in the sample application a little further on
in the tutorial. Now it's time to recap what we've covered so far.
|