Chapter 6: Methods Flashcards
How should this expression (ternary operator) be read?
boolean-expression ? expression1 :expression2
Evaluate the boolean-expression. If it is true, then use expression1. Otherwise, use expression2.
What is a method conceptually?
A collection of statements grouped together to perform an operation.
What is a method that belongs to a class and not a specific instance called?
Static. The name of the class must be used in the scope of them.
Think Math.sqrt(…) and Math.sin(…)
Why might we want to use methods?
Improve program readability.
Facilitate modular program development by breaking down programs into smaller, manageable pieces promoting incremental development practices.
Use stub methods to outline functionality before implementation (i.e., method definitions without statements written yet).
Avoid redundant code by centralizing logic in reusable methods.
Simplify program maintenance by requiring changes only in a single method.
What are the advantages of modularity?
- Controlling complexity
- Maintain programmer sanity
- Readability
- Implementation details are hidden in the method
- Ease of debugging
- Easily disable parts of the program
What is unit testing?
The process of individually testing a small part or unit of a program, typically a method.
What are the parts of unit testing?
A testbench (a.k.a. test harness) is a separate program designed specifically to verify that a method produces the correct output for various input values. Each unique set of input values is called a test vector, and an effective test vector includes border cases.
What are some additional considerations for test harnesses/testbenches?
A good test harness only displays messages when output values are incorrect. Manually examining a program’s output is both cumbersome and error-prone.
How do you define a method?
With a method header within a class. The header specifies the modifiers, the return type (possibly void), the name of the method, and the method parameter list.
modifiers returnValue Type methodName (list parameters) {
method Body;
return; //terminates the method
}
What are method parameters?
Parameters are assigned with argument values by position: First parameter with first argument, second with second, etc. A method definition may have multiple parameters, separated by commas.
Can a method be defined with no parameters?
Yes, but a method definition with no parameters must still have the parentheses.
public static double printVolume( ) {
..
}
How does a method return work?
It basically returns a value using a return statement.
Ex: return numToSquare * numToSquare; A method can only return one item. A return statement may appear as any statement in the method, not just the last. Multiple return statements may exist in a method. Control goes back to the calling program as soon as the first return statement is reached.
How does the void return type work?
- If the return type is void, the return statement is optional.
- A return statement may appear as any statement in the method, not just the last.
What is a method signature?
The method name and the parameters list together.
What’s the program that calls the methods called?
Caller.
What are the requirements for a method?
- The name of the method
- Argument (actual parameters to pass values) list. Could be a list of expressions or possibly empty.
What are Hierarchical (Nested) Method Calls?
- A method’s statements may include method calls.
- For example: main() is a method, and it typically calls many other methods.
What happens when a program calls a method?
Program control transfers to the called method, which runs and then returns control to the caller.
When does a method return control to the caller?
When it reaches a return statement or the closing brace of the method.
What is an activation record?
A memory record that stores a method’s parameters and variables, placed in the call stack.
Where are activation records stored?
In a special memory area called the call stack (also known as execution stack, runtime stack, or machine stack).
What happens when a method finishes executing in relation to the activation record and the call stack?
Its activation record is removed from the call stack.
What order does the call stack follow?
Last-In, First-Out (LIFO)—the last method called is the first one to finish and be removed.
What are the five steps when a method is invoked?
- Memory is allocated for parameters and local variables.
- Argument expressions are evaluated.
- Argument values are assigned to parameters.
- The method executes.
- Upon return, parameter and local variable memory is released.