Introduction to Java using NetBeans

Section 2.3 : User Defined Methods

Learning Goals

What we are Learning .......

- what is a method
- naming a method
- how to create and effectively use user defined methods

Success Criteria

What I'm Looking For......

- bug free java applications that efficiently solve the problems presented
- problem solving skills (try before you ask)
- efficient algorithm implementation
-modular programs
- formated and documented code
- organized website with code and executables

Curriculum Expectations

Programming Concepts and Skills
- demonstrate the ability to use different data types and expressions
- use proper code maintenance techniques when creating computer programs.

Software Development
- manage the software development process effectively

Designing Modular Programs
- apply modular design concepts


What are Methods?

A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. Think of a method as a subprogram that acts on data and often returns a value.

Each method has its own name. When that name is encountered in a program, the execution of the program branches to the body of that method. When the method is finished, execution returns to the area of the program code from which it was called, and the program continues on to the next line of code.


Good programmers write in a modular fashion which allows for several programmers to work independently on separate concepts which can be assembled at a later date to create the entire project. The use of methods will be our first step in the direction of modular programming.

Methods are time savers, in that they allow for the repetition of sections of code without retyping the code. In addition, methods can be saved and utilized again and again in newly developed programs


Naming a Method

Although a method name can be any legal identifier, code conventions restrict method names. By convention, method names should be a verb in lowercase or a multi-word name that begins with a verb in lowercase, followed by adjectives, nouns, etc. In multi-word names, the first letter of each of the second and following words should be capitalized. Here are some examples:



Simple Methods without Parameters



Adding a Menu


Assignment 2.7 Methods

Combine assignments 2.1 - 2.6 into one project.
Each assignment should be it's own method.

Create a menu method that is called first. Ask the user which method they want to run. After running a method display the menu again.

Add a println at the start of each method to indicate which method is running.

Above and Beyond is not Required