Methods were designed to:
Increase the readability of the program
Increase the ability to debug a program
Reduce the redundancy of code
Incorporate the concept of Separation of Duties/Divide and conquer
Examples of these methods are:
println()
nextLine()
nextInt()
charAt()
length()
split()
toUpperCase()
hasNext()
In reality, methods support the idea of the divide-and-conquer technique, which consists of breaking the problem into sub-problems and solving them independently to solve a larger problem.
There are two concepts that you need to understand for methods
Method definition
Method call (invocation)
Once the methods are defined, the code of your traditional classes will change:
When you define, a class method MUST be inside the class's scope but outside of the primary method.
Before Methods:
public class Whatever{
public static void main(String [] args){
//... all your code is in the main
//...
}
}
Methods use the divide and conquer technique. The divide and conquer technique means that the problem is broken into "small pieces" and each piece performs specific operations/tasks. For example, an example of a divide and conquer strategy is to break the main method into different methods and then call each method to perform specifics. Consider the code Whatever.java from the previous section now a skeleton of the class might look like this:
public class Whatever{
public static void main(String [] args){
// method calls belong here
}
m1()
{
// ...
}
m2()
{
// ...
}
// …
}
Here is a list of methods that you have been using for the past 8 weeks.
next()
nextLine()
nextInt()
nextDouble()
println(a String)
pow(an int, an int)
sqrt(int)
toUpperCase()
toLowerCase()
compareTo(String)
equals(String)
equalsIgnoreCase(String)
charAt(int)
length()
showMessageDialog(null, String)
1. In order to make it work, you need the "." at the beginning of each method
2. Parenthesis/arguments. All of them have parenthesis. However, there are two types.
a. (): they are called empty arguments. This means that there are no arguments for this method
b. (data type): inside the parenthesis, you can provide arguments. You need to specify each argument data type.
3. Camel case style. The name of the methods follows the format of the camel case.
Depending on your program's context, you can call the methods that you define in the previous section. Usually, the methods are called from the main.
1. Call the method with an actual value. Do not provide the data type in the parameters
public static void main(String [] args){
f(2);
}
2. Anticipate the data type you will receive from the method call. You can find out the data type by checking the return type of the method definition
public class MethodExample01{
public static void main(String [] args){
int solution = f(2);
}
public static int f(int x){
// .. code omitted
}
}
3. Report intermediate steps from the main. In this particular example, notice that the variable solution in the main now contains the result obtained by calling the method.
public class MethodExample01{
public static void main(String [] args){
int solution = f(2);
System.out.println("The solution is: "+solution);
}
Whatever happens under the scope of the method lives and stays there. Once the method returns the value that is specified in the method header, that operation expires.
For example, in our case, notice that we call the variable in the main that will hold the value solution AFTER the method f is called with the value 2. In the method f, the variable result holds the quick calculation of the function f. Once it hits the return, all the variables (including the result) disappear.
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.