Lecture 15, CS 302-6, October 7
i. http://www.youtube.com/watch?v=ube5EYFlFR0&feature=related
i. http://en.wikipedia.org/wiki/File:Selection-Sort-Animation.gif
i. Public static void main(String[] args)
i. We can think of it as a black box – something goes in, something happens, and something else comes out
i. Modularity
1. Separate code that does different things
ii. Maintainability/readability
1. Think of different methods as black boxes – all that is important is that they do what they are supposed to. We don’t need to worry about how they do it
iii. Reduce redundancy
1. Don’t need to repeat the same code: advantages -
a. Less typing
b. If something doesn’t work, you only need to fix it in one place, instead of many
i. Math.pow(a,b)
ii. scanner.next()
iii. random.nextInt()
i. Inputs – known as parameters or arguments
1. There can be any number: 0,1,2,…,etc
2. Declare in the method header, pass when you call the method
ii. Outputs – knows as return values
1. Can only be one (or zero…we’ll get to this later)
2. Declare in method header
3. ‘Send back’ with return statement
a. This return also indicates the end of the method
iii. Example method call - Math.pow(2,3) //2^3
Public static [return type] [name]([type1] param1,[type2] [param2] , …)
{
//body
return x;
}
i. Remember that Java programs are made up of blocks of code nested within each other. A method block is nested within the Class block.
i. Question – why do we get those results with the FOR loop? (think of what date.getTime() returns each time we call it)
i. Javadoc format. Eclipse can help – generate Element Comment command
i. Missing return values – will not compile
ii. Modifying parameters in the body of a method – works, but bad form