Lecture 15, CS 302-7 and 8, February 24

 

  1. Reminders
    1. Program 1

                                                               i.      Due tonight

  1. Review
    1. Arrays

                                                               i.      Array variables as pointers

    1. For each loop
    2. Methods
  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.      Underlined in this section J

    1. Up until now – everything has been in the main method

                                                               i.      Is this a method?  Yes.  But it’s kind of a special case.  We’ll get to it later

    1. What is a method?  A method is a block of Java code that performs some task.
    2. Why?

                                                               i.      Modularity

1.      Separate code that does different things

                                                             ii.      Maintainability/readability

1.      Think of different methods as black boxes – once they do what they are supposed to, don’t worry about them.  Like using pseudocode, kind of

                                                            iii.      Reduce redundancy

1.      Don’t repeate code – advantages:

a.       Less typing

b.      If something doesn’t work, only need to fix it one place, instead of many

    1. Examples that we’ve seen already:

                                                               i.      Math.pow

                                                             ii.      scanner.next

                                                            iii.      random.nextInt

    1. Methods have inputs and outputs:

                                                               i.      Inputs – parameters

1.      can be multiple

2.      declare in method header

                                                             ii.      Outputs – return values

1.      only one

2.      specify with return statement

3.      declare in method header

                                                            iii.      example - Math.pow(2,3) //2^3

  1. So, how do you declare a method?

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

{

//body
}

    1. Zero or one returns
    2. We can have as many params as we want, including 0
    3. Declare methods at Class level
    4. MyRandom.java
  1. Notes on Methods
    1. Commenting: comment each method

                                                               i.      Had some questions on this – when only one method, not a big deal

                                                             ii.      But, when you have multiple methods, it is important

    1. Parameters

                                                               i.      Other names – argument

    1. Variable scope – within block it is declared in
    2. Watch outs –

                                                               i.      Missing return values

    1. Modifying parameters in body
  1. HW
    1. Read 5.3-5.5