Recursion

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

Answer:

Use the drawStar() method again.

Recursion

Of course. We already have a method to draw a star, so just use it. The new stars need to be smaller than the central star. So the size of the new stars is size/3. (Other sizes will also work. You may wish to play with this.)

 
  // Seriously Wrong Method
  //
  private void drawStar( int x, int y, int size )
  {
    int endX, endY ;
     
    // Six lines radiating from (x,y)
    for ( int i = 0; i<6; i++ )
    {
      endX = x + (int)(size*Math.cos( (2*Math.PI/6)*i ));
      endY = y - (int)(size*Math.sin( (2*Math.PI/6)*i ));
      graph.drawLine( x, y, endX, endY );
      drawStar( endX, endY, size/3 ) ;
    }
  }

The basic star method is easily modified by inserting a statement that puts a star on the end of each line.

QUESTION 11:

But something is very seriously wrong with the new method. What is wrong?