The document defines 30 terms related to concepts in behavior analysis. It provides definitions for the following terms: functional relation, applied, generality, conceptually systematic, explanatory fiction, behaviour, environment, stimulus, interresponse time, momentary time sampling, whole-interval recording, partial-interval recording, baseline, positive reinforcement, negative reinforcement, motivating operation, stimulus control, Premack principle, reinforcer assessment, unconditioned reinforcer, conditioned reinforcer, automatic reinforcement, discriminative stimulus (SD), noncontingent reinforcement, high-probability request sequence, extinction, extinction burst, spontaneous recovery, and differential reinforcement. The document instructs the reader to provide definitions for each of the 30 terms.
3. 5-2
5.1 Introduction to Methods
5.2 Calling Methods
5.3 Class Members
Instance Members
static Members
5.4 Value-Returning & Value-Receiving
Methods
5.5 Notes on Declaring & Using Methods
5.7 Passing Arguments to a Method
5.11 Scope of Variable Declarations
5.12 Method Overloading
Source: Gaddis, Tony, Starting Out with Java From Control
Structures through Objects 4th Edition, Pearson Education Inc.,
2010, Chapter 5.
Chapter 5 Topics
2
5-3
5.1 Why Write Methods?
Divide and Conquer: Methods break a problem down into small
manageable pieces.
A method
4. 1contains code that performs a specific task.
2simplifies programs because a method can be used in several
places in a program. This is known as code reuse (method
coded once, used many times).
facilitates the design, implementation, operation and
maintenance of large programs.
Programs can be created from standardized methods rather than
by building customized code.
Dividing a program into meaningful methods makes the program
easier to debug and maintain.
Source: Gaddis, Tony, Starting Out with Java From Control
Structures through Objects 4th Edition, Pearson Education Inc.,
2010, Chapter 5.
Source: 1Linda Shepherd, Dept. of ISCS, COB, UTSA.
Modified By: 2Linda Shepherd, Dept. of ISCS, COB, UTSA.
3
5-4
A method can have its own variables (local variables).
These variables are declared in the body of the method and
belongs only to that method.
Declaration Format:
dataType variableName = initializationValue;
A method also can use fields (class/global variables).
Fields can be used by more than one method.
6. Header
public static void displayMessage ()
{
System.out.printf(“%nHello“);
}//This method receives nothing and returns
//nothing.
Method Modifiers
Return Type
Method Name
Parameter List
empty,
i.e., no para-
meter variables
9. Return Type
Method Name
Parameter List
Source: Gaddis, Tony, Starting Out with Java From Control
Structures through Objects 4th Edition, Pearson Education Inc.,
2010, Chapter 5.
with 1 parameter variable
7
5-8
Source: Gaddis, Tony, Starting Out with Java From Control
Structures through Objects 4th Edition, Pearson Education Inc.,
2010, Chapter 5.
public static int sum(int num1, int num2)
{
int result = 0;
result = num1 + num2;
return result;
}//This is a value-returning & value-receiving
//method.
5.1 Parts of a Method Header
10. Method Modifiers
Method Name
Parentheses
Return Type
contain a parameter list
8
5-9
1A void method is one that simply performs a task and then
terminates.
System.out.printf(“%nHi!");
2It can receive or not receive data.
3It definitely doesn’t return data.
4It has to be static if it’s in the same class as another static
method that calls it.
Source: 1Gaddis, Tony, Starting Out with Java From Control
12. }//END setName()
}//END APPLICATION CLASS DemoStaticVoidMethod
5.1 static and void Methods
10
5-11
A value-returning method not only performs a task, but also
sends a value back to the code that called it.
name = input.nextLine();
System.out.printf(“Hi %s!”, name); OR
System.out.printf(“Hi %s!”, getName());
Source: Gaddis, Tony, Starting Out with Java From Control
Structures through Objects 4th Edition, Pearson Education Inc.,
2010, Chapter 5.
Method that returns a name
see slides 5-6
5.1 Value-Returning Methods
nextLine is a method that captures a name and returns it to be
stored in the variable name; therefore,
nextLine is a value-returning method.
13. 11
5-12
A value-receiving method not only performs a task, but also
gets a value or values from the code that calls it.
name = input.nextLine();
System.out.println(“Hi “ + name + “!” );
OR
System.out.printf(“Hi %s!”, getName());
Source: Gaddis, Tony, Starting Out with Java From Control
Structures through Objects 4th Edition, Pearson Education Inc.,
2010, Chapter 5.
5.1 Value-Receiving Methods
“Hi “ plus the value in name plus “!” are all concatenated
(joined) then sent to println which is a value-receiving method
This String value/literal containing a name is sent to printf()
which in turn displays the greeting; therefore, printf is a value-
receiving method, but printf is also value-returning because it
returns the formatted output which is then printed.
19. 18
5-19
Methods can … be declared static by placing the keyword static
between the access modifier and the return type of the method.
public static double milesToKilometers(double miles)
{…}
When a class contains a static method, it is not necessary to
create an instance (copy) of the class in order to use the method.
double kilosPerMile = Metric.milesToKilometers(1.0);
Examples: Metric.java, MetricDemo.java
Source: Gaddis, Tony, Starting Out with Java From Control
Structures through Objects 4th Edition, Pearson Education Inc.,
2010, Chapter 9.
5.3 static Methods
19
5-20
static methods are convenient because they may be called at the
class level, i.e., using the class name.
They are typically used to create utility classes, such as the
Math class in the Java Standard Library (Java API).
Source: Gaddis, Tony, Starting Out with Java From Control
21. }//END main()
}//END APPLICATION CLASS TestDemoStaticVoidMethod
//NO IMPORT STMT NEEDED IN Test IF BOTH CLASSES IN
SAME DIRECTORY
21
5-22
public class DemoStaticVoidMethod
{
private static Scanner input = new Scanner (System.in);
public static String setName()
{
System.out.print(“Enter your name: “);
return input.nextLine();
}//END setName()
}//END CLASS DemoStaticVoidMethod
5.3 static and void Methods
public class TestDemoStaticVoidMethod
{
public static void main(String[] args)
{ DemoStaticVoidMethod demoProg =
new DemoStaticVoidMethod();
String greeting = “Hello”;
System.out.printf(“%s %s!”, greeting,
demoProg.setName());
24. public static int sum(int num1, int num2)
{
int result = 0;
result = num1 + num2;
return result;
}
This [variable] must be of the same data type as the return type
Return type
The return statement causes the method to end execution and it
returns a value back to the statement that called the method.
Source: Gaddis, Tony, Starting Out with Java From Control
Structures through Objects 4th Edition, Pearson Education Inc.,
2010, Chapter 5.
5.4 Defining a Value-Returning and Value-Receiving Method
25
5-26
public static int sum(int num1, int num2)
25. {
int result = 0;
result = num1 + num2;
return result;
}
1Parameter List
contains a list of variables the method takes in for processing.
Parameter variables are local.
5.4 Defining a Value-Returning and Value-Receiving Method
Source: Gaddis, Tony, Starting Out with Java From Control
Structures through Objects 4th Edition, Pearson Education Inc.,
2010, Chapter 5.
1Linda Shepherd, Dept. of ISCS, COB, UTSA.
26
5-27
5.4 Returning a Value from a Method
Data can be passed into a method by way of the parameter
variables. Data may also be returned from a method, back to
the statement that called it.
26. int num = Integer.parseInt("700");
The String literal “700” is passed to the parseInt method.
The int value 700 is returned from the method and assigned to
the num variable.
Source: Gaddis, Tony, Starting Out with Java From Control
Structures through Objects 4th Edition, Pearson Education Inc.,
2010, Chapter 5.
27
5-28
total = sum(value1, value2);
}
public static int sum(int num1, int num2)
{
int result = 0;
result = num1 + num2;
return result;
}
20
27. 40
60
Source: Gaddis, Tony, Starting Out with Java From Control
Structures through Objects 4th Edition, Pearson Education Inc.,
2010, Chapter 5.
5.4 Calling a Value-Returning and Value-Receiving Method
public static void main(String[] args)
{
int value1 = 20;
int value2 = 40;
int total = 0;
These are variable arguments.
28
5-29
Sometimes we need to write methods to test arguments for
validity and return true or false
public static boolean isValid( int number )
{
28. boolean status = false;
if(number >= 1 && number <= 100)
{
status = true;
}
return status;
}
Calling code:
int value = 20;
if(isValid(value))
System.out.printf(“%nThe value is within range.“);
else
System.out.printf(“%nThe value is out of range.“);
5.4 Returning a boolean Value
Source: Gaddis, Tony, Starting Out with Java From Control
Structures through Objects 4th Edition, Pearson Education Inc.,
2010, Chapter 5.
29
5-30
5.4 Returning a Reference to a String Object
customerName = fullName("John“, "Martin“);
public static String fullName(String first, String last)
{
29. String name = null;
name = first + " " + last;
return name;
}
See example:
ReturnString.java
Local variable name holds the reference to the object. The
return statement sends a copy of the reference back to the call
statement and it is stored in customerName.
address
“John Martin”
Source: Gaddis, Tony, Starting Out with Java From Control
Structures through Objects 4th Edition, Pearson Education Inc.,
2010, Chapter 5.
These are literal arguments.
33. Structures through Objects 4th Edition, Pearson Education Inc.,
2010, Chapter 5.
1,2Linda Shepherd, Dept. of ISCS, COB, UTSA
5.7 Passing Arguments to a Value-Receiving Method
35
5-36
5.7 Passing 5 to the displayValue Method
displayValue(5);
public static void displayValue(int num)
{
System.out.printf(“The value is %d.“, num);
}
The argument 5 is an integer literal that is copied to the
parameter variable num.
The method will display The value is 5.
Source: Gaddis, Tony, Starting Out with Java From Control
Structures through Objects 4th Edition, Pearson Education Inc.,
2010, Chapter 5.
34. 36
5-37
5.7 Argument and Parameter Data Type Compatibility
When you pass an argument to a method, be sure that the
argument’s data type is compatible with the parameter
variable’s data type.
Java will automatically perform widening conversions, but
narrowing conversions will cause a compiler error.
double d = 1.0;
displayValue(d);
Error! Can’t convert double to int
Source: Gaddis, Tony, Starting Out with Java From Control
Structures through Objects 4th Edition, Pearson Education Inc.,
2010, Chapter 5.
37
35. 5-38
5.7 Passing Multiple Arguments
showSum(5, 10);
public static void showSum(double num1, double num2)
{
double sum = 0; //to hold the sum
sum = num1 + num2;
System.out.printf(“%nThe sum is %.0f“, sum);
}
The argument 5 is copied to the num1 parameter.
The argument 10 is copied to the num2 parameter.
NOTE: Order matters!
method call
Source: Gaddis, Tony, Starting Out with Java From Control
Structures through Objects 4th Edition, Pearson Education Inc.,
2010, Chapter 5.
36. 38
5-39
5.7 Arguments Passed by Value or by Reference
Passed by Value: In Java, all arguments of the primitive data
types are passed by value, which means that a copy of an
argument’s value is passed to a parameter variable.
If a parameter variable is changed inside a method, it has no
affect on the original argument.
See example: PassByValue.java
Passed by Reference: In Java, objects are passed by reference
which means the address (memory location) of the object is
passed/sent to a parameter variable.
Can the original object be affected if it’s changed inside the
method?
A method’s parameter variables are separate and distinct from
the arguments that are listed inside the parentheses of a method
call.
Source: Gaddis, Tony, Starting Out with Java From Control
Structures through Objects 4th Edition, Pearson Education Inc.,
2010, Chapter 5.
39
5-40
38. because the methods cannot see each other’s local variables.
A method’s local variables exist only while the method is
executing. When the method ends, the local variables and
parameter variables are destroyed and any values stored are
lost.
Local variables are not automatically initialized with a default
value and must be given a value before they can be used.
Source: Gaddis, Tony, Starting Out with Java From Control
Structures through Objects 4th Edition, Pearson Education Inc.,
2010, Chapter 5.
5.11 Scope of Variable Declarations Local Variables
41
5-42
A class variable or class field is declared at the class level right
after the class header, following the open brace for the class.
It is accessible to all statements (methods) within the class and
is known as a global variable.
It is declared using access specifiers
private – which means only the statements inside the class can
use the field and is primarily used when declaring class fields
Useful in promoting data hiding
Protects variables from corruption or malicious alteration
public – which means statements inside or outside the class can
use the field
46. 54
/**
The sum method returns the sum of its two parameters.
@param num1 The first number to be added.
@param num2 The second number to be added.
@return The sum of num1 and num2.
*/
5-55
You can provide a description of the return value in your
documentation comments by using the @return tag.
General format
@return Description
See example: ValueReturn.java
The @return tag in a method’s documentation comment must
appear after the general description. The description can span
several lines.
Source: Gaddis, Tony, Starting Out with Java From Control
Structures through Objects 4th Edition, Pearson Education Inc.,
2010, Chapter 5.
47. 55
5-56
You can provide a description of each parameter in your
documentation comments by using the @param tag.
General format
@param parameterName Description
See example: TwoArgs2.java
All @param tags in a method’s documentation comment must
appear after the general description.The description can span
several lines.
Source: Gaddis, Tony, Starting Out with Java From Control
Structures through Objects 4th Edition, Pearson Education Inc.,
2010, Chapter 5.
/**
The sum method returns the sum of its two parameters.
@param num1 The first number to be added.
@param num2 The second number to be added.
@return The sum of num1 and num2.
*/