Advertisement
Guest User

Untitled

a guest
May 28th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. Writing recursive methods:
  2. ==========================
  3. -- The key is to break the problem into smaller subproblems; does the
  4. problem require you to perform some action over and over again?
  5. Look up ``Divide and conquer''
  6.  
  7. -- Remember that your method must have a **base case**; otherwise it will
  8. recurse forever and never return! The base case is the smallest, trivial case.
  9. For instance, if you were trying to find the minimum element in a list of
  10. integers, the base case would be if the list just contains one integer (then you
  11. just return that integer, there is no problem to solve).
  12.  
  13. -- Remember that your problem must **get smaller** at each recursive call
  14. (otherwise it will not get to the base case, and you will loop forever);
  15. e.g., if you are dealing with lists, then each recursive call should be on
  16. a smaller list.
  17.  
  18. -- If you are stuck, do it with pencil and paper first. If someone gave you a
  19. word and asked you whether or not it was a palindrome, how would you figure out
  20. the answer? If you had to teach a toad how to reverse a string, what would you
  21. tell it?
  22.  
  23. Q2:
  24. ===
  25. -- If you just put ``public void reverse(List<E> list)'' then the compiler will be annoyed
  26. because it will think that `E' is a concrete type (like String, HashSet etc), but of course
  27. you mean a **type parameter** not a type. To fix this, you need to put
  28. public <E> void reverse(List<E> list).
  29.  
  30. Java tips:
  31. ==========
  32. -- None of these methods require you to keep a reference to instance variables, so
  33. you can make them static.
  34.  
  35. Extras:
  36. =======
  37. -- What blackbox tests would you write for the methods in this prac?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement