Lesson 4.2 Producing Values
Value Producing Methods
Many methods produce a value instead of, or in addition to, performing an action. When a method produces a value, you can use that method as an operand in an expression instead of a literal or variable value.
In Java, a method may produce a single primitive value or return a reference to an object. In this lesson you'll first learn how to create methods that produce primitive values [which are useful for expressions]. Then you'll learn how to create methods that return objects. Such methods are often called object factories.
Producing Primitive Values
To write a method that produces a value, you need do only two things:
- Add the type of value to be produced to the method header.
- Return a value from inside the method using the return keyword
The simplest value-producing method just returns a primitive value. Let's try that by writing a method that returns the value of pi. Because we want the maximum precision, we'll make our method a double method.
import java.awt.*;
import java.applet.*;
public class PiOne extends Applet
{
public double pi()
{
return 3.141592653589793;
}
} |
Defining the Method
Notice the two differences from the simple methods you studied in Lesson 4.1.
- First, simple methods don't return a value, so they use the keyword void before the method name. This method returns a double value, so the keyword double is used before the method name. This is called the return type, and the method pi() is said to be of type double.
- Second, simple methods don't use the keyword return. A simple method is, actually, allowed to use the keyword return, although we haven't done so; when used with a simple method, it means "end the method at this point." The meaning is the same when used with a method that returns a value, except that "and send this value back to the caller" is tacked onto the end.
In a value-producing method, the return statement both ends the method and transports the method's value back to the caller; in a simple method, return simply ends the method.
Using the Method
Once we've written our value-producing method pi(), we can call the method just exactly the same way we would a simple method. Even though the pi() method returns a value, you can choose to ignore the returned value, like so:
For a method like pi(), you may save the returned value, so you can use it in your calculations, like this, which calculates the area of a circle, given its radius, r:
double p = pi();
double area = p * r * r; |
The real advantage of value-producing methods, however, is that you usually don't do either of these; instead, you call the method in place of a value, like this:
| double area = pi() * r * r; |
For a simple value like that produced by pi(), a regular attribute would be a better choice, but for a more complex calculation, involving, perhaps, real-time information, such a method is preferred.
Producing Reference Values
In Lesson 3, you used the NumberFormat class to create human-readable numeric output. To create a NumberFormat object, you didn't use the new operator, but used factory methods instead. Factory methods create objects, and then return them via the return statement.
Suppose you have an application that contains many buttons, and that all of the buttons must be blue, and use 14-point type. You could use the regular methods supplied by the Button class to create each Button object, but that would mean changing the color and setting the font on every button you create.
Creating a Factory Method
A better idea is to create a factory method to construct your buttons for you. To create a factory method you follow these three steps:
- In your method header, use a return type of Button. If you were to create some other type of object, you'd put that return type here instead.
- Inside your method, create a local Button object using the new operator and then change its font and text color as normal.
- Use the return statement to return your modified Button object.
Let's call the factory method bigBlueButton(). It should look something like this:
// bigBlueButton factory method
public Button bigBlueButton()
{
Font f;
f = new Font("Serif", Font.BOLD, 14);
Button b = new Button("I'm Blue");
b.setForeground(Color.blue);
b.setFont(f);
return b;
} |
Using bigBlueButton()
With your factory method, you can create your buttons using bigBlueButton() in place of using new. Here's an applet that creates three big blue buttons using this method.
import java.awt.*;
import java.applet.*;
public class BlueButtons extends Applet
{
Button b1 = bigBlueButton();
Button b2 = bigBlueButton();
Button b3 = bigBlueButton();
public void init()
{
b1.setLabel("I'm the FIRST");
b2.setLabel("But I'm the bluest");
b3.setLabel("I'm the LAST");
add(b1);
add(b2);
add(b3);
}
// --------------------------------
// bigBlueButton() method goes here
// -------------------------------------
}
|
The real advantage of using a factory method like bigBlueButton(), instead of using new Button and the Button methods, is that next week when the job specs are changed to require 18 point type instead of 14, you have to change only a single line of code.
|
"But, my Buttons aren't blue"
|
| If you run BlueButtons on some platforms, you may not get blue buttons, as you'd expect. This is one of those cases where Java's "cross-platform portability" is a little less than it might be.
Because the Java Button object is really a "wrapper" around the real O/S dependent button, you only get blue buttons if your O/S allows that. Some [like the Mac] don't. |
Of course bigBlueButton() would be a lot more useful if you could tell it what text you wanted to display, or even what color to use. For that, however, you'll have to wait for the next installment, Lesson 4.3, "Arguments."
Something to Talk About
Why don't you try customizing the BlueButtons applet? Here's how:
- Create an applet using the code listed above as BlueButtons.
- Add the bigBlueButton() method to BlueButtons.
- Compile and test it to make sure it runs.
How can you change the applet so that it makes yellow buttons with green, 18-point text? Please continue to the next section of this lesson.
|