Lecture 15, CS 302-6, October 7

  1. Review
    1. Arrays
    2. Binary Search

                                                               i.      http://www.youtube.com/watch?v=ube5EYFlFR0&feature=related

    1. Selection Sort

                                                               i.      http://en.wikipedia.org/wiki/File:Selection-Sort-Animation.gif

  1. Methods
    1. Think about our Java programs so far – we’ve put everything in this section called main

                                                               i.      Public static void main(String[] args)

    1. Is this a method?  Yes.  But it’s kind of a special case.  We’ll get to it later
    2. What is a method?  A method is a block of Java code that performs some task.

                                                               i.      We can think of it as a black box – something goes in, something happens, and something else comes out

    1. Why?

                                                               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

    1. Examples of methods that we’ve seen already:

                                                               i.      Math.pow(a,b)

                                                             ii.      scanner.next()

                                                            iii.      random.nextInt()

    1. Methods have inputs and outputs:

                                                               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

  1. So, how do you declare a method?

Public static [return type] [name]([type1] param1,[type2] [param2] , …)

{

//body

return x;
}

    1. As stated – can only have one return value, but any number of parameters
    2. You declare methods at the class level

                                                               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.

    1. Example - MyRandom.java

                                                               i.      Question – why do we get those results with the FOR loop?  (think of what date.getTime() returns each time we call it)

    1. Example - AnyRandom.java
  1. Notes on Methods
    1. Commenting methods

                                                               i.      Javadoc format.  Eclipse can help – generate Element Comment command

    1. Variable scope – within the block they are declared in.  More on this later.
    2. Watch out for:

                                                               i.      Missing return values – will not compile

                                                             ii.      Modifying parameters in the body of a method – works, but bad form

    1. Example – Bottles.java
  1. HW
    1. Read 5.3-5.5