<< Chapter < Page | Chapter >> Page > |
Revised: Sun Mar 27 20:13:16 CDT 2016
This page is included in the following Books:
This module is part of a collection of modules designed to help you learn to program computers.
It provides a gentle introduction to Java programming methods.
In addition to an Internet connection and a browser, you will need the following tools (as a minimum) to work through the exercises in these modules:
The minimum prerequisites for understanding the material in these modules include:
I recommend that you open another copy of this document in a separate browser window and use the following links to easily find and view the listings while you are reading about them.
Methods have been used in computer programming since the early days of programming. Methods are often called functions, procedures, subroutines, and various other names.
Calculate the square root
Suppose that your program needs to calculate the square root of a number. Referring back to your high-school algebra book, you could refresh your memory on how to calculate a square root. Then you could construct the algorithm describing that process.
Having the algorithm available, you could write the code to calculate the square root and insert it into your program code. Then you could compile, and run your program. If you did it all correctly, your program should calculate the square root. (For reasons that will become apparent later, I will refer to the code that you inserted as in-line code.)
Oops, need to do it all over again
Suppose that further on in your program you discover that you need to calculate the square root of another number. And later, you discover that you need to calculate the square root of still another number. Obviously, with a few changes, you could copy your original code and insert it as in-line code at each location in your program where you need to calculate the square root of a number.
Is there a better way?
However, after doing this a few times, you might start asking if there is a better way. The answer is "yes, there is a better way."
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?