Tutorial 6 Summary
When programming, we need a way to break up our thoughts of what we want the program to do. To do this, we use methods, or what we call in some other languages functions or subroutines.
Let's say that you're writing a program that controls a robotic dog. You don't want the dog to be too smart, so you only want to put in the effort to make the dog do some simple things. Those things are: walk, run, wag tail, and bark. You could write one big program in a huge chunk to do the job, but it would probably get very confusing. The code would look like it went on forever, and you would either need to do a lot of copy-pasting of code, or you would have to use control flow statements in a strange way.
This is a bad thing, of course. So to help us organize ideas and clean up our code, we have "methods" to come to our rescue. A method is a place where we can put a chunk of code that is related and we might want to reuse. In our example, we would have a "walk" method, a "run" method, a "wag tail" method, and a "bark" method. We have already seen one type of method, the "main" method:
public static void main(String[] args) {
// fun stuff...
}
As we know, this is where our program begins when we start the program. However, we can make other methods too. A basic method looks like this:
public static returnType methodName(arguments) {
// things we want the method to do
}
At this point, don't worry about what "public" or "static" mean. The
returnType is the type of the thing that is returned by the
method. After we run the method, we can give something back to
wherever the method was called. If returnType is
void, that means that the method is not returning
anything. We will look more at this in a minute. The
methodName is the name of our method (like walk, run, etc.).
The arguments are things we can give to the method. Let's
write the methods for our robotic dog:
public static void walk() {
System.out.println("The dog is now walking");
}
public static void run() {
System.out.println("The dog is now running");
}
public static int wagTail() {
System.out.println("The dog wags it's metal tail!");
return 70;
// we are returning number of times the dog wags
// its tail per minute
}
public static void bark(int typeOfBark) {
if (typeOfBark == 1) {
System.out.println("woof woof!!");
} else if (typeOfBark == 2) {
System.out.println("ruff ruff!!");
} else {
// we didn't understand the type of
// bark that was given as an argument
System.out.println("bark bark!!");
}
}
Now we have the basic actions for our dog. To finish the program for
the dog, we can write the main method for our dog:
public static void main(String[] args) {
boolean programIsStillGoing = true;
while (programIsStillGoing) {
walk();
run();
int rate = wagTail();
System.out.println("The dog is wagging its tail " +
"at a rate of " + rate + " per minute");
bark(1);
}
}
Now if we wanted to change the order that things were done, we would
only need to change a few lines of code in the main
method, as opposed to moving lots of code around. Methods are
actually much more important than just time savers, but we will see
their real power when we look at object oriented programming.

