// 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(stutter(-348)); } // 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 the integer obtained by replacing every digit of n with two of // that digit. For example, stutter(348) returns 334488. public static int stutter(int n) { if (n < 0) { return -stutter(-n); } else if (n < 10) { return n * 11; } else { return 100 * stutter(n / 10) + stutter(n % 10); } } // writes the binary representation of n to System.out public static void writeBinary(int n) { if (n < 0) { System.out.print("-"); writeBinary(-n); } else if (n < 2) { System.out.print(n); } else { writeBinary(n/2); System.out.print(n % 2); } } }