Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Writing recursive methods:
- ==========================
- -- The key is to break the problem into smaller subproblems; does the
- problem require you to perform some action over and over again?
- Look up ``Divide and conquer''
- -- Remember that your method must have a **base case**; otherwise it will
- recurse forever and never return! The base case is the smallest, trivial case.
- For instance, if you were trying to find the minimum element in a list of
- integers, the base case would be if the list just contains one integer (then you
- just return that integer, there is no problem to solve).
- -- Remember that your problem must **get smaller** at each recursive call
- (otherwise it will not get to the base case, and you will loop forever);
- e.g., if you are dealing with lists, then each recursive call should be on
- a smaller list.
- -- If you are stuck, do it with pencil and paper first. If someone gave you a
- word and asked you whether or not it was a palindrome, how would you figure out
- the answer? If you had to teach a toad how to reverse a string, what would you
- tell it?
- Q2:
- ===
- -- If you just put ``public void reverse(List<E> list)'' then the compiler will be annoyed
- because it will think that `E' is a concrete type (like String, HashSet etc), but of course
- you mean a **type parameter** not a type. To fix this, you need to put
- public <E> void reverse(List<E> list).
- Java tips:
- ==========
- -- None of these methods require you to keep a reference to instance variables, so
- you can make them static.
- Extras:
- =======
- -- What blackbox tests would you write for the methods in this prac?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement