Method definition is composed of the following formula (see method Header see: page 272) is composed by:
<encapsulation type><modifiers><return type><id>(<parameter data type><id>, ...)
where the following items:
encapsulation type: this is the visibility of the method according to the scope of the class or the package.
public
private
protected
package (will not be used in methods only in classes in CS II)
modifiers: allows you to perform operations inside/outside the class. It allows you to overwrite methods and overload
static
final
abstract
interface
return type: what this method "gives back" once you called the method.
Eight primitive types (int, short, long, float, double, char, boolean, byte)
Object, e.g., String
void
Id: the name of the method
An identifier that makes the method meaningful (camel case style)
For parameters (if available): these are the values that are inside the parenthesis
Data type: i.e., eight primitive types, any object (e.g., String)
Id: the name of the parameter
E.g., Consider the functions from pre-calculus that you needed to solve back in middle school
f(x) = x2 + 3
f(2) = (2)^2 + 3
= 4 + 3
= 7
f(x) = x2 + 3
If I called the function with f(2), I would get: 7
Name of function: f
Parameter: x
The data type of the parameter: int
What will the function return once evaluated: an int
By using the previous components mentioned above, we obtain:
public static int f(int x) // method header
By elaborating the body of the method: Notice that we state that our returning data type is an int; it is recommended to create a place holder for that and return it immediately as follows:
public static int f(int x){
int result = 0;
return result;
}
This way, your code can compile without any problem. Not the remaining piece of code is the actual calculation of the function (i.e., x2 + 3). We can simply make the statement in Java as "x * x + 3", resulting in the following method:
public static int f(int x){
int result = 0;
result = x * x + 3;
return result;
}
public static void main(String [] args){
for(double x = 1; x <= 20; x+=0.1){
double y = f(x);
System.out.println("f("+x+"): \t"+y);
}
}
We can provide flexibility to the computer user to choose whatever option he/she wants to use. The method f takes an int as a parameter, but what about if we would like to provide a double? We can have another method with the same name but with a different header. The header is the part of the method that will provide the parameter data type. For example:
public static double f(double x){
double result = 0;
result = Math.pow(x,2) + 3;
return result;
}
Noticed in this version, we exercise the invocation of the Math.pow() method. This is an example of how to invoke methods inside of another method.
How can we define a method in Java?
To guarantee a successful compilation and successful execution of your program, we recommend doing the following steps. We are going to keep using the function f(x) as an example:
Identify all the elements of the
Move outside the scope of the main, but stay inside the scope of the program
public class MethodExample01{
public static void main(String [] args){
}
public static int f(int x){
}
}
3. In order to compile your program successfully. You must return the data type that you claim you will use in the method header
public static int f(int x){
int result = 0;
return result;
}
4. For the rest, use the variable that you declared internally to perform operations; in this case, will perform the actual operation of f(x)
result = x * x + 3;
5. Combine Step 3 and Step 4 in the method:
public static int f(int x){
int result = 0;
result = x * x + 3;
return result;
}
AP CS A
[Using Methods]
MOD-2.C.3 A precondition is a condition that must be true just prior to the execution of a section of program code in order for the method to behave as expected. There is no expectation that the method will check to ensure preconditions are satisfied.
MOD-2.C.4 A postcondition is a condition that must always be true after the execution of a section of program code. Postconditions describe the outcome of the execution in terms of what is being returned or the state of an object.
MOD-2.C.5 Programmers write method code to satisfy the postconditions when preconditions are met.
[Documentation Comments]
MOD-2.C.3 A precondition is a condition that must be true just prior to the execution of a section of program code in order for the method to behave as expected. There is no expectation that the method will check to ensure preconditions are satisfied.
MOD-2.C.4 A postcondition is a condition that must always be true after the execution of a section of program code. Postconditions describe the outcome of the execution in terms of what is being returned or the state of an object.
MOD-2.C.5 Programmers write method code to satisfy the postconditions when preconditions are met.
ACM CCECC
[Using Methods]
SDF-14. Analyze programming code that utilizes preconditions, postconditions, and invariants.
Explain the importance of algorithms in the problem-solving process.
Demonstrate how a problem may be solved by multiple algorithms, each with different properties.
Read a given program, and explain what it does
Trace the flow of control during the execution of a program (both correct and incorrect).
Use appropriate terminology to identity elements of a program (e.g., identifier, operator, operand)
Reading and explaining code
Basic concepts such as variables, primitive data types, expression evaluation, assignment, etc.
Apply basic programming style guidelines to aid readability of programs such as comments, indentation, proper naming of variables, etc.
Basic testing (perhaps using suitable frameworks) including test case design
Basic concepts such as variables, primitive data types, expression evaluation, assignment, etc.
Key modularity constructs such as functions (and methods and classes, if supported in the language) and related concepts like parameter passing, scope, abstraction, data encapsulation, etc.