1 / 10

Warm Up

Warm Up. Follow the directions on Handout 1 to create Sierpinski’s Triangle. Handout 2. Underline the recursive call(s) Specify when the recursion stops and the value returned for the base case(s). Handout 3. Show the call stack and what is printed or returned by each call. Handout 6.

agatha
Download Presentation

Warm Up

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Warm Up • Follow the directions on Handout 1 to create Sierpinski’s Triangle

  2. Handout 2 • Underline the recursive call(s) • Specify when the recursion stops and the value returned for the base case(s)

  3. Handout 3 • Show the call stack and what is printed or returned by each call

  4. Handout 6 • Complete Handout 6 for homework • Will go over in class tomorrow

  5. Simple to Recur

  6. Questions • How might an object repeat itself? • What are the three simple steps for repeating with recursion? • When is it okay to stop? • Without a loop, how can you know where you are?

  7. Three Simple Rules To write repetitive code: • Begin by bailing out. That is, begin at the ending (the termination). Base case. • Process the first. That is, process the first piece of data. • Process the rest. That is, let this (recursive) method process the rest of the data.

  8. Structural Recursion • Refers to a self-referential data structure with natural termination. • Matryoshka Nesting Dolls

  9. howManyDolls public inthowManyDolls() { if(this.innerDoll == null){return 1; } return 1 + this.innerDoll.howManyDolls(); } • Begin by bailing out. • Process the first. • Process the rest.

  10. Expand the Matryoshka class • Add three instance fields to the Matryoshka class; a String called name, a java.awt.Color called hair, and a boolean called babushka. Expand the constructors to assign these fields at instantiation. • Write a new recursive method, howManyWearingBabushkas. If the babushka field is true, that Matryoshka is wearing a babushka. • Write a new recursive method, redHeadCount. If the hair is equal to java.awt.Color.RED, that Matryoshka has red hair, sometimes known as being a redhead. • Write a new recursive method, lastName. If a Matryoshka has no inner doll, then its name is alphabetically last of the collection. If a Matryoshka has an inner doll,, and its name is later in the alphabet than the last of the inner doll's collection, then its name is the last name. Otherwise, the last name of the inner doll's collection is the last name. Use the String compareTo method to see which name is later in the alphabet.

More Related