Lecture 14, CS 302-7 and 8, February 22

 

  1. Reminders
    1. Program 1

                                                               i.      Due Friday night

                                                             ii.      Input validation - .hasNextInt, .next

                                                            iii.      Reminder – documentation

                                                           iv.      Reminder – don’t try to do ‘too much’

  1. Review
    1. Arrays

                                                               i.      Declare/initialize

1.      int[] numbers=new int[3];

2.      int[] numbers; numbers=new int[3];

3.      int[] numbers={1,2,3};

                                                             ii.      Use

1.      numbers[1] (‘numbers sub 1’) -> 2

2.      numbers[0] -> 1

                                                            iii.      Characteristics

1.      Type – int, double, String, etc

2.      Length - numbers.length

  1. More about arrays as references
    1. If we set two array variables equal to the same array, then updating one updates the other
    2. Example – RefArray.java
  2. For each loop
    1. Structure

                                                               i.      int[] data = {…};

For( int value : data)

{

//   body

}

    1. Example – TestScores.java
  1. Things you can do with Arrays
    1. Many of the same things we did with loops – sums, averages, Max, min. etc – except, now we remember data

                                                               i.      Note –these require traversing the entire array

    1. Linear search

                                                               i.      Start at beginning, search until we find data

                                                             ii.      Note – search can abort early, once we find answer

    1. Binary Search

                                                               i.      Faster than linear

                                                             ii.      Condition – array is sorted

                                                            iii.      Real-world example – searching through a dictionary.

1.      Start in middle, then half each way, etc.

    1. Inserting and removing elements

                                                               i.      Remove – start at one above, copy down

                                                             ii.      Insert – start from end, copy up, then put item in

1.      Harder than remove

    1. Copying

                                                               i.      Int[] numbers2=Arrays.copyOf(numbers, numbers.length);

  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. What is a method?  A method is a block of code that performs some task.
    2. We’ll now write our own additional static methods
    3. Why?

                                                               i.      Modularity

                                                             ii.      Maintainability/readability

                                                            iii.      Reduce redundancy

    1. Think of methods as black boxes
    2. Examples that we’ve seen already:

                                                               i.      Math.pow, Arrays.copyOf

    1. Methods have inputs and outputs:

                                                               i.      Inputs – parameters/arguments or parameter variables/parameter values

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. Syntax

                                                               i.      Declaring :

public static <return type> <name>(<paramVars>) {

   //body

   //return <returnVal>;

}

                                                             ii.      Calling:

<ClassName>.<methodName>(<paramVals>); //Returns <returnVal>

  1. HW
    1. Read 5.1-5.2