Methods Flashcards
public static void main (String []args)
Identify the parts of the method.
void - return type
main - method name
(String []args) - parameter
What is the format of a method?
public static return_type methodName ([parameters])
It is a variable that will receive the value being passed in calling the method.
Parameters
This return type will not return any value.
void
a collection of statements that are grouped together to perform an operation.
Method
a collection of statements that are grouped together to perform an operation.
Method
Examples of defined methods in the Java library
- System.out.println
- Integer.parseInt,
- Double.parseDouble
- System.exit
- Math.pow
- Math.random.
Syntax in declaring method
[accesskeyword] static returntype methodname (parameter) {
}
can be public, private, or protected.
Access keyword
What will be the default form of access if no access modifier is declared?
Package
indicates the type of value returned or reported back to the calling method, and it is most often used in conjunction with the assignment of a value to a variable in the calling method.
Return type
What can the return type be?
- any Java primitive data type
- an object
- void
This return type denotes hat the method has no return value
Void
What naming rules does the method name follow?
Follows the same naming rules as variables and will generally begin with a lowercase letter.
What are methods and variables caused because they are both contained inside classes?
They are called class members
receives the value being passed when a function is called
Parameter
What is used to separate parameters in a parameter list?
Comma
states the data type of the parameter, which can be either a primitive data type or an object.
First entry in a parameter
declares the name to be used when referencing the parameter within the method.
Second entry in a parameter
a variable inside the method
Parameter
Name the parts of the method
public static int max(int num1, int num2)
public static int max(int num1, int num2) - method header
public - access keyword
static - modifier
int - returntype
max - method name
(int num1, int num2) - parameter list
num1 & num2 - parameter
What would the output be?
public static void main(String[] args) {
System.out.println(“Main Methods”);
DisplayMessage();
System.out.println(“Back to Main Methods”);
}
static void DisplayMessage(){
System.out.println(“Programming is great fun!”);
}
Main Methods
Programming is great fun!
Back to Main Methods
What would the output be?
public static void main(String[] args) {
DisplayNumbers();
DisplayMessage();
}
static void DisplayMessage(){ System.out.println("Programming is great fun!"); } static void DisplayNumbers(){ System.out.println("One"); System.out.println("Two"); System.out.println("Three"); }
One
Two
Three
Programming is great fun!
TRUE OR FALSE: Methods can also be called in a hierarchical, or layered fashion.
TRUE
What would the output be?
public static void main(String[] args) {
DisplayMessage();
DisplayNumbers();
}
static void DisplayMessage(){
System.out.println(“Programming is great fun!”);
Looping();
}
static void DisplayNumbers(){
System.out.println(“One”);
System.out.println(“Two”);
System.out.println(“Three”);
}
static void Looping(){
for (int x=1; x<=5; x++)
System.out.println(x + “ “);
}
Programming is great fun!
1 2 3 4 5
One
Two
Three
TRUE OR FALSE: A method may be written so it accepts arguments. Data can then be passed into the method when it is called.
TRUE
Values that are sent into a method
Arguments
a special variable that holds a value being passed into a method.
Parameter variable
What would the output be?
public static void main(String[] args) {
DisplayNumber(5);
}
static void DisplayNumber(int num){ System.out.println("The value is " + num); }
The value is 5
What would the output be?
public static void main(String[] args) {
int a=3, b = 4;
DisplayNumber(5);
DisplayNumber(4 * 3-1);
DisplayNumber(a * 2);
DisplayNumber(a+b);
}
static void DisplayNumber(int num){
System.out.println(“The value is “ + num);
}
The value is 5
The value is 11
The value is 6
The value is 7
What would the output be?
public static void main(String[] args) {
ShowNum(23.4, 45.7);
}
static void ShowNum(double num1, double num2){
double sum= num1 + num2;
System.out.println(“Num1 : “ + num1);
System.out.println(“Num2 : “ + num2);
System.out.println(“The sum is “ + sum);
}
Num1 : 23.4
Num2 : 45.7
The sum is 69.1
TRUE OR FALSE: Each parameter variable in a parameter list doesn’t need to have a data type listed before its name.
FALSE, a compiler error would occur if not all of the parameter variables doesn’t have a data type listed before its name
declared inside a method and is not accessible to statements outside the method.
Local Variables
Can different methods can have local variables with the same names?
Yes, because the methods cannot see each other’s local variables
Identify the local variables and method parameters.
public static void main(String[] args) {
int x, y;
}
static void ShowNum(double num1, double num2){
int sum;
}
static void FindProduct(double number){
int product, square;
}
main
local variables - x, y
method parameter - args
ShowNum
local variables - sum
method parameter - num1, num2
FindProduct
local variables - number
method parameter - Product, square
are not automatically initialized with a default value. They must be given a value before they can be used.
Local variables
What happens if you use a local variable before it has been given a value?
A compilor error will result
What would the output be?
public static void main(String[] args) {
int x=5;
System.out.println(“Before Calling ChangeValue Method: “ + x);
ChangeValue(x);
System.out.println(“Before Calling ChangeValue Method: “ + x);
}
static void ChangeValue(int x){
x=50;
System.out.println(“Inside ChangeValue Method: “ + x);
}
The value of the local value is not affected when the value of variable is changed in other method.
Before Calling ChangeValue Method: 5
Inside ChangeValue Method: 50
Before Calling ChangeValue Method: 5
A method may send a value back to the statement that called the method. Data may be passed into a method by way of parameter variables. Data may also be returned from a method, back to the statement that called it.
Returning a value from a method
These are methods that return a value
Value-returning methods
Explain what happens to the variables and the output.
public static void main(String[] args) {
int total = GetSum(10,20);
System.out.println(“Total: “ + total);
}
static int GetSum(int num1, int num2){
int sum = num1+num2;
return sum ;
}
VARIABLE TRACING
num1=10
num2=20
sum=num1+num2 | sum=30
total=sum | total=30
OUTPUT
Total:30
TRUE OR FALSE: The non void value must return any value otherwise it would be a compilation error
TRUE
Why does an error occur if you put a statement after the return statement?
Because the statement after the return statement is a dead code or unreachable statement