Section 7c

Functions as Factories


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Creating and Using More Complicated Methods

The methods with which you worked in the previous topic were pretty simple. They did a simple output process, and yet even though it was simple, it improved the quality of the program, it improved the readability of the program, and it reduced the potential for program errors. You will next consider a method that actually does some work for your program, and returns a result, but before you do that, consider the following.

The factory metaphor. You can think of a method as "factory" that does things for you. If you are a car manufacturer, you can call upon a tire manufacturer to send you tires. Their tire factory will create tires, but it needs to know how many tires to create. You order five tires for your new car, and the factory gives you five new tires.

However, sometimes you need to do a little more processing than just creating or displaying some number of things. Sometimes your method factory might need to take in some information, process it for you, and give it back to you in a new form. For example, say that your program needed two integers multiplied together so you could get the result, which is the mathematical product of the two. In this case, you need a place to put the necessary information in, and a place to get the necessary result out.

The question is, what is it that needs to go in and/or out? This leads to the developmental questions, which are: “What is the same?” and “What is different?” In the case of a multiplier method, the multiplication process will always occur; that remains the same. However, the two numbers will vary with each use of the process; these will be different. When we can identify the things that will be different for each use of the method then we will have identified what the method needs to know to do its job, as well as what it doesn’t need to know. Since we have identified the two numbers as the “different” part, we will know to provide those values to the methods as parameters. You have seen these previously but now you can see how the design process needs to be applied to identify them.

The method's name will be multiplier and it will accept two parameters and provide one method output result, as shown here.

// define variables
int value_1, value_2, product;

// assign the values
value_1 = 5;
value_2 = 4;

// conduct the process
product = multiplier( value_1, value_2 );

// output the results
conIO.printString( "The result is: " + product );
conIO.printEndline();

You should be able to read this code segment (and all code segments) like a book. Some values (i.e.,value_1 and value_2) are declared and initialized; a multiplication process is conducted on the two values, and the results of that multiplication are assigned to another variable called product; and finally, the result is output with some leading text so the user can see (and understand) the result. Note that this process uses some of the input/output methods you were provided in earlier topics.

Here is what the process might look like at a method factory that is generalized for a variety of inputs and outputs.

Image of a general function factory

Here is what the process might look like specifically at the multiplier method factory.

Image of factory that multiplies two numbers

As long as you think of the method as a little factory with data (i.e., necessary information) coming in the right, and the results going out the left, you will be able to use methods just fine. As you recall from the printing methods, some methods do not output things to the left; they just take some kind of action in the program without creating a processed result. Java allows this with all methods, but as you noticed above, when methods are actually designed not to output anything, they are called void methods, and in general, these are called procedures.

The methods that were used to output asterisks and spaces were void methods. Methods can return any individual data type that you want them to, but note that methods can only return one thing. This is an important thing to remember.

Watch this video to extend your understanding of the concept of methods and functional abstraction.