Title: Lecture 10: Methods
1Lecture 10 Methods
2Outline
- Overview
- Method format
- Arguments
- The returned value
- Variable scope
- Using methods
- Commenting methods
- The method main()
3Overview
- Methods provide a way to break a program into
smaller chunks. - Youve already seen many examples
- System.out.println(Hello)
- Math.sqrt(25.0)
- playerWantsToHit()
- getNewCard()
- A method is a chunk of code that can be executed
by calling its name. - But where is the code thats executed when a
method is called?
4Method Format
public static int triple(int n) n 3
return n
5Arguments
- A method can have any number of arguments.
- getNewCard() takes no arguments.
- System.out.println(Hello) takes 1 argument.
- Math.pow(3, 2) takes 2 arguments.
- Etc.
- When an argument of type int, double,
or boolean is passed to a method, the
method makes a copy. - Strings and arrays work differently. More
later.
public static int triple(int n) n 3
return n
int n 5 int nTripled triple(n) System.out.pr
intln(nTripled) System.out.println(n)
6The Returned Value
- As weve seen, a method can return a value that
it computed. - getNewCard() returns a random value between 1 and
10. - Math.pow(x, y) returns xy.
- The type of the returned value must be stated.
- The return statement must be included
at the end of the method body this
indicates the value being returned.
public static int triple(int n) n 3
return n
7The Returned Value
- A method can do stuff, but then return nothing.
- Example System.out.println(Hello)
- In this case, the return type is void, and
there doesnt have to be a return statement.
public static void showTriple(int n) n
3 System.out.println(n)
8Variable Scope
- Variables declared inside a method have local
scope. That is, they only exist inside the
method. - What will happen if you try to use
nTimes3 outside of the method
triple()? - Error!
- Also, as soon as the method ends, the
local variable is lost.
public static int triple(int n) int nTimes3
n 3 return nTimes3
int n 5 int nTripled triple(n) System.out.pr
intln(nTimes3)
9Using Methods
- Why use methods?
- So the same code doesnt have to be repeated.
- Example getNewCard()
- To make the program easier to read.
initializePlayerHands() while (playerHits)
drawNewCard() checkIfBusted() while
(dealerTotal lt 17) drawNewCard() determineWin
ner()
10Commenting Methods
- Write a comment block right before each method,
which includes three parts - Method description What the method does.
- Input A description of the arguments passed to
the method. - Output A description of the value being
returned from the method.
// This method triples the value of an int. //
Input n any int. // Output An int that
equals three times the input. public static int
triple(int n) n 3 return n
11The Method main()
- Now we can understand (almost) this line of code
- public static void main(String args)
- Its a method called main that takes in an
array of Strings and doesnt return anything. - When you execute a program (by hitting the
button), that tells Java to execute the main()
method.
12Homework
- Read 3.1 3.5.
- HW5 out today, due Monday.
- Poker.