Podcast
Questions and Answers
What is the primary action performed when a method is called?
What is the primary action performed when a method is called?
- The program executes the method's code block from bottom to top.
- The method's code block is ignored and the program continues.
- The program executes the method's code block from top to bottom. (correct)
- The program skips over the method's code block.
Why are methods considered convenient in programming?
Why are methods considered convenient in programming?
- They reduce the amount of memory the program uses.
- They automatically optimize code execution speed.
- They allow code to be reused without rewriting it. (correct)
- They make code run only once, preventing infinite loops.
In the context of a method, what does the term 'signature' refer to?
In the context of a method, what does the term 'signature' refer to?
- The code block within the method.
- The method's name and parameter list. (correct)
- The access modifiers of the method.
- The return type of the method.
What is the primary purpose of the return
keyword in a method?
What is the primary purpose of the return
keyword in a method?
What happens if a method's return statement is located inside an if
statement and the condition is false?
What happens if a method's return statement is located inside an if
statement and the condition is false?
What is the key characteristic of a method with a void
return type?
What is the key characteristic of a method with a void
return type?
What is typically included in a method's header?
What is typically included in a method's header?
In what order does the code within a method's block execute?
In what order does the code within a method's block execute?
Where should methods be written in relation to other methods and classes?
Where should methods be written in relation to other methods and classes?
What is a key benefit of using methods for debugging?
What is a key benefit of using methods for debugging?
What does the term 'parameter' refer to in the context of methods?
What does the term 'parameter' refer to in the context of methods?
In the public static boolean isPositive(int num)
method, what does boolean
signify?
In the public static boolean isPositive(int num)
method, what does boolean
signify?
What is meant by 'unreachable code' in a method?
What is meant by 'unreachable code' in a method?
How do you call a method in your program?
How do you call a method in your program?
In the context of methods, what is the significance of 'camelCase'?
In the context of methods, what is the significance of 'camelCase'?
Which part of the following method declaration is the method header? public static int calculateSum(int a, int b)
Which part of the following method declaration is the method header? public static int calculateSum(int a, int b)
How does using methods contribute to less code repetition?
How does using methods contribute to less code repetition?
Which components are considered part of a method's signature?
Which components are considered part of a method's signature?
Consider a method declared as public static void displayMessage(String message)
. What can you infer about this method?
Consider a method declared as public static void displayMessage(String message)
. What can you infer about this method?
What happens when a method returns a value?
What happens when a method returns a value?
If a method has a non-void return type, what must the method body always ensure?
If a method has a non-void return type, what must the method body always ensure?
Why must isPositive()
from the resource's content, return something?
Why must isPositive()
from the resource's content, return something?
Why might we want to store a method into a variable, instead of calling it by itself?
Why might we want to store a method into a variable, instead of calling it by itself?
What happens if a method declares a return after an if statement?
What happens if a method declares a return after an if statement?
How do we identify if something is as a method?
How do we identify if something is as a method?
What does declaring the method say about it?
What does declaring the method say about it?
What is a named block of code?
What is a named block of code?
If given a scenario of the same output, it is better to use a method instead of just running the code regularly. WhY?
If given a scenario of the same output, it is better to use a method instead of just running the code regularly. WhY?
Why is debugging considered to be easier when writing methods?
Why is debugging considered to be easier when writing methods?
Why is there are benefit towards less code repitition?
Why is there are benefit towards less code repitition?
If given the method public static void printWord(String w);
What can be said about it?
If given the method public static void printWord(String w);
What can be said about it?
If given the method public static boolean isDivisible(double d, int i);
what can we assume must happen?
If given the method public static boolean isDivisible(double d, int i);
what can we assume must happen?
Which of the following isn't true about writing good methods?
Which of the following isn't true about writing good methods?
What of the following would we call is the parameter in public static int getAge(int birthYear, String name);
What of the following would we call is the parameter in public static int getAge(int birthYear, String name);
Flashcards
What is a method?
What is a method?
A named block of code that performs a specific task.
Why use methods?
Why use methods?
Methods are convenient because they can be called over and over again without re-writing the code.
Access Modifiers (public, static)
Access Modifiers (public, static)
Keywords defining access level. Public means accessible from anywhere, static is associated with the class itself.
Return Type
Return Type
Signup and view all the flashcards
Method Name
Method Name
Signup and view all the flashcards
Method Parameters
Method Parameters
Signup and view all the flashcards
Method Body
Method Body
Signup and view all the flashcards
Return Keyword
Return Keyword
Signup and view all the flashcards
Void Return Type
Void Return Type
Signup and view all the flashcards
Parameter Matching
Parameter Matching
Signup and view all the flashcards
Where do I write methods?
Where do I write methods?
Signup and view all the flashcards
Benefits of writing methods
Benefits of writing methods
Signup and view all the flashcards
Study Notes
- Writing methods is about creating named blocks of code
What is a Method?
- A method is a named block of code that is executed when is called by its name.
- The code runs top to bottom
- The code runs until it returns a value or reaches the end of the code block.
- Methods are like miniature programs, they take in data as parameters, perform a function, and return a value.
- Methods contains the code that runs when called by name, and executes all the code within its code block.
- A code block consists of everything between the method's curly brackets.
- Methods can contain other code blocks, such as if statements and "for" loops.
- All code inside curly brackets directly under the method declaration forms the method.
Why Use Methods?
- Methods are convenient as they can be called repeatedly without rewriting the code.
- Methods prevent the need to rewrite "for" loops for similar calculations each time.
Basic Syntax
- public static void main(String[] args) is the basic syntax:
public
andstatic
are access modifiers.void
specifies the return type.main
is the method nameString[] args
represents the method's parameters.
- Running a program in a block of code is running a method.
- Right-clicking a class and running
void main(String[] args)
executes that method which contains all of the code. public void main(String[] args)
is a special method in Java.- When a method is called, the main program execution jumps to that method, completes its task, and then returns to the main program.
Parts of Method: Header
public static boolean isPositive(int num)
is the method's header.- The header declares a method and includes the return type, name, and parameters.
- The method's signature consists of the name and parameter list
Parts of Method: Access Modifier
public
andstatic
are access modifiers
Parts of Method: Return Type
boolean
is the return type- Once finished, the method return a boolean value to where it was called.
- Return type indicates the type of value or an answer gets sent back.
Parts of Method: Name/Identifier
isPositive
is the name of the method- Methods should follow variable naming conventions, such as being descriptive and using camelCase, starting with a lowercase letter.
- Methods are called using parentheses, differentiating them from variables
Parts of Method: Parameter List
int num
is the method's parameter list- Methods can have multiple parameters or none.
- Parameters are placeholders for values passed in by the caller.
- The parameter list specifies what information a method needs from the main program to execute.
Parts of Method: Body
- The method's body is the block of code between curly brackets, which runs when the method is called.
- Methods must return a value of the specified type before the closing curly bracket (unless the return type is
void
).
The Return Keyword
- The
return
keyword indicates that a method's job is done and it is immediately returning a value. - Methods must return the data type specified in the method header.
- Any code after a
return
is unreachable because the method has already returned to where it as called.
isPositive(int num)
Method
- If the
num
parameter is greater than 0, theisPositive(int num)
method needs to return a value oftrue
. - If the condition
num > 0
isn't met, the method won't return anything without fixing this. - To ensure the isPositive(int num) method always returns a value, an else statement can be used to return false when num is not greater than 0.
- Simplified versions of the
isPositive()
method achieve the same result.return num > 0;
- The isPositive() method can be used in a main program and called when needed.
More on the Return Keyword
- When the
return
keyword is encountered, the method stops executing and returns a value. - Code after a
return
statement is unreachable. - If a
return
statement is inside an if statement or a loop, there is a chance the statement won’t run. There must be a return statement in an else statement. - When a method returns a value, its job is done.
- Code that cannot be executed is called unreachable code
The Void Return Type
- A
void
return type means a method does not return a value. - Methods with a
void
return type still run its code block, but do not return a value. - Void methods can still return, but will not return a value.
Void
return type methods are the only methods where areturn
isn't explicitly required.- If a method returns a `boolean, a boolean must be returned.
- When a method returns
void
, it could return, but the compiler won’t complain if it returns at the end.
Calling Methods/Parameter Matching
avgOf2()
method call needs to use the method name.- The
avgOf2
method requires 2 parameters, so data needs to be sent over for it to complete its job. - The
avgOf2
method is designed to return adouble
, so the returned value must be stored to continue
Where to Write Methods
- Methods must be written outside of other methods, but inside a class.
Benefits of Writing Methods
- Methods allows for easier debugging since, any issues can be located quickly.
- Methods result in less code repetition, and called with new parameters.
- Methods are easier to read.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.