<< Chapter < Page Chapter >> Page >
This module provides a gentle introduction to Java methods.

Revised: Sun Mar 27 20:13:16 CDT 2016

This page is included in the following Books:

Table of contents



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 Sun/Oracle Java Development Kit (JDK) (See (External Link) )
  • Documentation for the Sun/Oracle Java Development Kit (JDK) (See (External Link) )
  • A simple IDE or text editor for use in writing Java code.

The minimum prerequisites for understanding the material in these modules include:

  • An understanding of algebra.
  • An understanding of all of the material covered in the earlier modules in this collection.

Viewing tip

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.


  • Listing 1 . The price of beans.
  • Listing 2 . Compute the square root of the price of beans.
  • Listing 3 . Display the square root value.
  • Listing 4 . Calling the same methods again.
  • Listing 5 . The program named SqRt01.
  • Listing 6 . A batch file for compiling and running the program named SqRt01.

Discussion and sample code


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."

Questions & Answers

a diagram of an adult mosquito
mubarak Reply
what are white blood cells
Mlungisi Reply
white blood cell is part of the immune system. that help fight the infection.
what about tissue celss
Cells with a similar function, form a tissue. For example the nervous tissue is composed by cells:neurons and glia cells. Muscle tissue, is composed by different cells.
I need further explanation coz celewi anything guys,,,
Calvin Reply
hey guys
on what?
is air homogenous or hetrogenous
damiane Reply
why saying homogenous?
explain if oxygen is necessary for photosynthesis
Allice Reply
explain if oxygen is necessary for photosynthesis
Allice Reply
Yes, the plant does need oxygen. The plant uses oxygen, water, light, and produced food. The plant use process called photosynthesis.
By using the energy of sunlight, plants convert carbon dioxide and water into carbohydrates and oxygen by photosynthesis. This happens during the day and sunlight is needed.
no. it s a product of the process
yet still is it needed?
no. The reaction is: 6CO2+6H20+ solar energy =C6H12O6(glucose)+602. The plant requires Carbon dioxyde, light, and water Only, and produces glucose and oxygen( which is a waste).
what was the question
the specific one
the study of non and living organism is called.
Is call biology
what Is ecology
Musonda Reply
what is a cell
Emmanuel Reply
A cell is a basic structure and functional unit of life
what is biolgy
Hawwi Reply
is the study of living and non living organisms
may u draw the female organ
i dont understand
me too
anabolism and catabolism
Sani Reply
Anabolism refers to the process in methabolism in which complex molecules are formed "built" and requires energy to happen. Catabolism is the opposite process: complex molecules are deconstructed releasing energy, such as during glicolysis.
Explain briefly independent assortment gene .
Otu Reply
hi I'm Anatalia
what do you mean by pituitary gland
draw and lable the cell
Ameh Reply
why homones are destroyed soon after completing their role
Nyirenda Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?