Small Stars

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.
Historical topic: Java appletsModern web browsers do not run Java applets. The Applet API was deprecated for removal before Java 25 and was removed in Java 26. Treat applet examples on this page as historical object-oriented/GUI examples; for new desktop interfaces use Swing where appropriate or JavaFX/OpenJFX.

Answer:

When a star is too small, don't draw it.

Small Stars

This is a somewhat hard thought, and I suggest that you get out pencil and paper and draw some stars attached to stars attached to stars. After a while it is clear that you want to stop drawing when the stars are too small to draw. Our applet is no different:

  private void drawStar( int x, int y, int size )
  {
    int endX, endY ;
    
    if ( size <= 2 ) return;  // base case
    
    // 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 );  
    }
  }

I picked the value "2" for the base case. Smaller values result in cluttered snowflakes.

QUESTION 13:

In this code, if the if statement followed the for statement, would everything be OK?