Lecture 21, CS 302-6, October 21

  1. Reminders
    1. Exam 1 – we’ll discuss next week, probably Wednesday

                                                               i.      Why?  Some people still haven’t taken it

    1. Program 2 – October 28

                                                               i.      10pm Monday-pairs must be registered

    1. Program 1 – coming back soon
  1. Program 2 stuff
    1. Suggestions for testing your code

                                                               i.      Try to think of a small test case that you know the answer to

                                                             ii.      “If we have A ants, a map B, tunnelAttractivenesses C, pheromone evaporation coefficient D, pheromone strength coefficient E, and run for F iterations, what should be the shortest route”

                                                            iii.      Suggestion: 1 ant, 1 iteration, set constants to 1, use a simple map

    1. One note on maps – I suggest testing with map2 in AntWorld.java.  Instructions on course website for accessing this.
    2. If you don’t know what value you want an object to have, but you want to initialize it, you can set it equal to null

                                                               i.      This is a little like setting String to “”, integer to 0, etc.

                                                             ii.      Example:

1.       int[] test=null ;

                                                            iii.      Null is a key word – basically says don’t point anywhere in memory

                                                           iv.      Note – we’ll get index out of bounds errors if we try to use test (for instance, call print(test[1]).  But we can pass it as a parameter, return it, and check if it is null (test==null or test!=null)

  1. Recursion
    1. Note – you will not be tested on this

                                                               i.      But it is good to know

    1. Concept – methods that call themselves
    2. One of the most powerful tools in programming

                                                               i.      Elegant/simple/concise solutions to complicated problems – sometimes (almost) the only solution

                                                             ii.      Also one of the most confusing topics in CS

    1. Vocab:

                                                               i.      Recursive case

1.      Case in which the method calls itself

                                                             ii.      Base case

1.      Case that terminates – does not call itself

2.      Often (but not always) something like:

a.       When length is 0

b.      When value is 0

    1. Lazy evaluation
    2. Example 1 – Factorials

                                                               i.      Factorials.java

    1. Example 2 – Anagrams

                                                               i.      Definition – all possible orders of the letters in the word

                                                             ii.      Idea – pick a letter, stick it in front.  You have n-1 letters left.  Pick a letter from that, stick it in front.  Etc.  Do this for each possible selection.

    1. Anagrams.java
  1. HW – read 6.6 (for Monday)