Methods Chapter 7 Review

advertisement
Methods
Chapter 7 Review
Program Development with Methods
• In the software development process,
top-down development is a problem-solving
approach that breaks a task down into smaller
subtasks and then further into units.
• Implementing these tasks with methods is
called procedural abstraction.
Calling a Method
• Methods are executed when they are called.
• A method call consists of the method name
followed by parentheses. For example,
fahrenheitToCelsius();
Writing Methods
parameters
access _ level
class method
return_type
name
public static void drawBar(int length, String mark) {
for (int i = 1; i <= length; i++) {
System.out.print(mark);
}
System.out.println();
}
Writing Methods
A public method can be
called by any other method.
drawBar is the name, that will
be used to call this method
public static void drawBar(int length, String mark) {
}
passed data
The return type void means that
the method will not return a value
The keyword static declares the
method a class method.
Method Parameters
• A method declaration can include method
parameters, which accept values from the
method call.
• The data passed to the method can then be used
inside the method to perform its task.
Method Parameters
• In this example, this method has two incoming
parameters (length, mark)
public static void drawBar(int length, String mark) {
for (int i = 1; i <= length; i++) {
System.out.print(mark);
}
System.out.println();
}
Method Parameters
• When you call this method you need to include
data for the two parameters (length, mark)
drawBar(5, “*”);
public static void drawBar(int length, String mark) {
for (int i = 1; i <= length; i++) {
System.out.print(mark);
}
System.out.println();
}
Method Overloading
• Method overloading is when more than one
method of the same name is included in a
class.
• Method names do not have to be unique as
long as the parameters are different for
methods with the same name.
The return Statement
• It is possible for a method to return a value.
• This is done with a return statement, which sends
a value back to the calling statement.
• A method that returns a value must include the
return type in the method declaration.
public static
double
double xCubed;
xCubed = x * x * x;
return(xCubed);
}
cubeOf(double x) {
The returned value?
• A method that returns a value is called from a
statement that will make use of the returned
value.
• For example, cube =cubeOf(num); returns the
value which is assigned to the variable cube.
Documenting Methods
• Careful documentation is important for the
reader of a program to understand a method.
• Method documentation is enclosed by /** */
and includes a brief description of the
method, a precondition, and a postcondition.
/**
* Print a bar of asterisks across the screen.
* pre: length > 0
* post: Bar drawn of length characters, insertion
* point moved to next line.
*/
Pre & Post Condition Guidelines
• The precondition states what must be true at the
beginning of a method for the method to work properly.
• The postcondition states what must be true after the
method has executed if the method has worked
properly.
• Preconditions and postconditions should not state facts
that the compiler will verify. They should also not refer to
variables or information outside the method.
• The postcondition should not state how the method
accomplished its task.
Download