Section 7b

Creating a Method


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Creating and Using Simple Methods

Starting Out

A short review. The first two things to learn about methods are already history to you. You have been using methods all along in this course. You implemented commands in the form of methods in order to draw pictures with asterisks, as shown below.

printSpace();
printSpace();
printSpace();
printSpace();
printAsterisk();
printAsterisk();
printAsterisk();
printEndLine();

The above methods created the following output, and you can watch this video to see the program work.

Simple program with single method calls

This was pretty straightforward, and thanks to the "English-like" characteristics of the Java programming language, it made pretty good sense what was happening.

How to Write a Method

Suppose now that you were asked to create a box of characters that was three asterisks wide and three asterisks tall. Wouldn't it be helpful if you could abstract the process of creating one line on the screen as a method, and then just call this three times to make the box? Here is how you can do this, in step-by-step form.

First create the header of the method. The header has five primary parts: 1) the scope or availability of the method (public/private/etc., which will be discussed later), 2) whether it is static or a class member method, 3) the return data type, 4) the method identifier or name, and 5) the method parameter(s) which may also be called the parameter list, which are located in the parentheses following the identifier. The scope, parameters, and return quantity will be discussed in a later topic, but for now you can observe what the simplest method looks like, and how to put it together. In addition, be advised that this method only does output, and does not return any processed results. So, it returns nothing . . . and the word for returning nothing in Java is void. You are already familiar with the other parts, so here is what the method header looks like.

public static void printOneLine()

To review the parts: 1) this method is public, or available to any programmer to use, 2) it is static, meaning it is only created once and associated with the main method, 3) the return data type is void, 4) the identifier is printOneLine, and 5) for this method, there is no need for parameters since the number of spaces and asterisks will be fixed inside the method. However, note that the parentheses are still required, and are always required for methods.

The next part of writing the method is to create the empty body. This is easy; just go to the next line, type three spaces, type an open curly brace, type two or three ENTERs to make vertical space, then type three spaces, and type a close curly brace. Unless you want to spend hours chasing down missing curly braces, you will always create the empty body first, which then includes both the open and closed curly braces. Here is the whole package.

public static void printOneLine()
{


}

Once you have the empty code block, you can go to work. In this case, you will write in the statements that are necessary to solve the problem; these are written in English, or pseudocode so that you can verify that your method will do what it is supposed to do before you write any code. To wit:

public static void printOneLine()
{
// print four spaces
// print three asterisks
// print an endline
}

Finally, with the method actions clearly specified, it is trivial to write in the code. Here are the results.

public static void printOneLine()
{
// print four spaces
printSpace();
printSpace();
printSpace();
printSpace();

// print three asterisks
printAsterisk();
printAsterisk();
printAsterisk();

// print an endline
printEndLine();
}

Now some consideration as to what has happened. You wrote a header, made an empty code block, wrote in what you wanted to do, and then filled in the code. This is not terribly difficult, but there are a few more things to note.

  1. There is no semicolon after the method header. As mentioned earlier in this reference, under some circumstances when a code block follows some initial code segment, the statement is not completed until the end of the code block (i.e., the closing curly brace). This is that circumstance.

  2. The code that is written to make the method do what it is supposed to do is called method implemention code, or usually just the method implementation. Remember this term as you will be asked many times to create a method implementation.

  3. The method implementation is located, well . . . , in the method implementation part of your program. This is the area you have already been identifying with comments. Now you have something to put there.

Watch this video on how to create the printOneLine method. As always, practice along with it. You will be writing a lot of methods from now on. This will also show you where to put your method implementation, and it will show you how to call the method in order to test it. The resulting output is shown here (and looks a lot like the one you viewed previously).

Simple program with single method calls

Try it again. Your next method can be simpler because you will have abstracted part of the process with the method you have created. The abstracted method prints one line with four advanced spaces and three asterisks. Now what if you want to take this tool and make a box. The process is very easy, but take a look anyway. First, you create the header and succeeding code block, as shown here. Once again, never type an open curly brace without a closed one right after; this will save you hours later on.

public static void printBox()
{


}

Now, design the method, as shown.

public static void printBox()
{
// display the first line of the box
// method: printOneLine

// display the second line of the box
// method: printOneLine

// display the third line of the box
// method: printOneLine
}

Finally, drop in the code. As you have seen before, this is trivial as long as you have taken the time to design and write the method first.

public static void printBox()
{
// print three lines with spaces and asterisks
// method: printOneLine
printOneLine();

printOneLine();

printOneLine();
}

Watch this video to see this method created and then the method used in the program. When the program is run and this method is called, the following box is displayed.

Image of box created with asterisks

Of course, the method must be called within a program, so there is a little more to the larger process as you saw in the video. Shown below is the whole program including the parts you wrote. You can assume that the printAsterisk, printSpace, and printEndLine methods were created for your use in the lower part of the WorkingTestClass. Acquiring tools from other files was addressed in the formatted command-line and formatted console I/O topics. You will see this strategy applied for other tools later on as well.

package p4_package;

public class WorkingTestClass
{
public static final char ASTERISK_CHAR = '*';
public static final char SPACE_CHAR = ' ';

public static void main(String[] args)
{
printBox();
}

public static void printBox()
{
// print three lines with spaces and asterisks
// method: printOneLine
printOneLine();
printOneLine();
printOneLine(); }

public static void printOneLine()
{
// print four spaces
// method: printSpace
printSpace();
printSpace();
printSpace();
printSpace();

// print three asterisks
// method: printAsterisk
printAsterisk();
printAsterisk();
printAsterisk();

// print end line
// method: printEndline
printEndline();
}

// printAsterisk, printSpace, and printEndline methods defined here
}

Now that you are learning to develop methods, you can see how the method parts (i.e., the method implementation and usage) are placed in the larger program.

The final point is the big one, and it is related to the title of this chapter. By definition, your printAsterisk, printSpace, and printEndLine methods are abstracted because you only know what they look like and what they do; you don’t have to know how they work. Next, you abstracted one line of display by creating a method that displayed one line (i.e, printOneLine); you took away the details and gave yourself a single identifier that represented those details, and printed one line. But you didn’t stop there. You then created a printBox method that abstracted the process of printing a box and left out the details of printing one line three times, and printing asterisks, spaces, and endlines. As you will see later, this functional abstraction process is the most powerful design tool you will be given for developing programs, and it will serve you well.