// Helene Martin, CSE 143 // A simple star-printing method written iteratively and recursively. public class Stars { public static void main(String[] args) { writeStars(3); writeStars(15); writeStars(0); } // Prints a line containing the given number of stars. // Precondition: n >= 0 public static void writeStars(int n) { if (n == 0) { // base case (simplest instance of the problem) System.out.println(); } else { // recursive case System.out.print("*"); writeStars(n - 1); } /* It can be useful to write out specific cases to see the pattern for the recursive case else if (n == 2) { System.out.print("*"); writeStars(1); } else if (n == 3) { System.out.print("*"); writeStar(2); } ...*/ } // Prints a line containing the given number of stars. // Precondition: n >= 0 public static void writeStarsIterative(int n) { for (int i = 0; i < n; i++) { System.out.print("*"); } System.out.println(); } }