Question: java code level: beginner write a method Background The Fibonacci sequence, where you start with 0 and 1 and generate the next number by adding

java code

level: beginner

write a method

Background

The Fibonacci sequence, where you start with 0 and 1 and generate the next number by adding up the previous two numbers.

For example, the first few numbers are: 0, 1, 1, 2, 3, 5, 8, 13, and so on.

This is closely related to the Golden Ratio ( a / b = (a + b) / a). It is often visualized as a spiraled shell (see above), which we will be creating as our final shape!

This is an excellent exercise integrating recursion and shapes. It may take some time to wrap your head around, so please read the write up carefully. Do not be intimidated; the implementation is not complex as long as you understand the idea behind it.

public static void draw(Group group, int centerX, int centerY, int prevRadius,
int currRadius, int startAngle, int n)

Note: Make sure you understand what each parameter represents before continuing to read! group represents the group that we use to group all the shapes together for displays. (centerX, centerY) is the center of the Fibonacci sequence. prevRadius is the previous radius of arch while currRadius is the current radius. startAngle is one of the possible four possible angles an Arc could start at.

Terminating Case (Base Case)

draw() is a recursive method. The terminating case, or the base case, is given to you.

If n == 0, return without drawing any squares or shells.

Recursion

On each recursive call, we will add one JavaFX Arc (documentation) object to the group using the current parameters of draw(). Then, you make a recursive call to draw().

Notice the Arc constructor's parameters. arcLength represents the "angular extent of the arc in degrees." We set that value to be 90 because every arc is exactly a quarter circle, and 360 / 4 = 90 degrees. We already created the Arc for you and added it to the group. Your main task is to figure out what the next recursive call to draw() will take in as parameters.

Thus, you need to find new values of centerX, centerY, prevRadius, currRadius, startAngle, and n, which you will use to make your recursive call to draw().

  • Updating centerX, centerY
    • There are four possible angles an Arc could start at.
    • Clearly think about these four cases (Hint: 0, 90,180, 270). How would the next center get updated in each case? Draw it out! Think about what variable you need to update the value of the center.
  • Updating prevRadius, currRadius
    • The next radius will be generated using the Fibonacci Sequence.
    • Use the fact that we keep track of both the previous and current radius. How would this help us generate the next radius?
  • Updating startAngle
    • Hint: There are 4 possible start angles: 0, 90,180, 270. How would you update from one to the next? Draw it out!
  • Next value of n

After you have calculated the new variables you want to pass in to your recursive call, make your recursive call to draw().

Inside Fibonacci class

java code level: beginner write a method Background The Fibonacci sequence, where

Sample Output

you start with 0 and 1 and generate the next number by

import javafx.scene.*; import javafx.scene.paint.*; import javafx.scene.shape.*; public class Fibonacci f // every arc is a quarter circle static final int arcLength-90; public static void draw(Group group, int centerX, int centerY, int prevRadius, int currRadius, int startAngle, int n) if (n-0) return; Arc a new Arc(centerx, centery, currRadius, currRadius, startAngle, arcLength); a.setType (ArcType.ROUND) a.setFill (null); a.setstroke(Color.BLACK); group.getchildren().add (a); TODO //calculate any new values needed, then make a recursive

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!