// This file contains several simple examples of recursive definitions. import java.io.*; import java.util.*; public class Recurse { public static void main(String[] args) { writeStars(8); writeStars2(6); Scanner input = new Scanner("this\nis\nfun\nno?\n"); reverse(input); System.out.println(dashes("hello")); } // iterative method that produces an output line of exactly n stars public static void writeStars(int n) { for (int i = 0; i < n; i++) { System.out.print("*"); } System.out.println(); } // recursive method that produces an output line of exactly n stars public static void writeStars2(int n) { if (n <= 0) { System.out.println(); } else { System.out.print("*"); writeStars2(n - 1); } } // post: reads a file, writing the lines to System.out in reverse order public static void reverse(Scanner input) { if (input.hasNextLine()) { String line = input.nextLine(); reverse(input); System.out.println(line); } } // returns a new string with dashes inserted between characters public static String dashes(String s) { if (s.length() <= 1) { return s; } else { return s.charAt(0) + "-" + dashes(s.substring(1)); } } }