import javabook.*; class Recursion { public static void main(String[] args) { MainWindow mw = new MainWindow(); mw.setVisible( true ); InputBox in = new InputBox( mw ); int number = in.getInteger( "Number, please:" ); int fact = recursive_factorial( number ); System.out.println( "-------------------" ); System.out.println( number + "! = " + fact ); /* int[] a = new int[10]; for( int i=0; i < a.length; i++ ) a[i] = (int)(Math.floor(Math.random() * 100) + 1); print( a, 0 ); System.out.println(""); number = sum( a, a.length ); System.out.println( "Sum = " + number ); String s = in.getString( "String, please:" ); if( isPalindrome( s ) ) System.out.println( s + " is a palindrome." ); else System.out.println( s + " is not a palindrome." );*/ } public static int recursive_factorial( int n ) { if( n == 1 ) return 1; int total = n * recursive_factorial( n - 1 ); return total; } public static int factorial( int n ) { int total = 1; for( int i = 2; i <= n; i++ ) { total = total * i; } return total; } public static int sum( int[] a, int length ) { if( length <= 0 ) return 0; int partialSum = sum( a, length - 1 ); return a[length-1] + partialSum; } public static void print( int[] a, int startingAt ) { if( startingAt >= 0 && startingAt < a.length ) { System.out.print( a[startingAt] + " " ); print( a, startingAt + 1 ); } } public static boolean isPalindrome( String s ) { if( s.length() <= 1 ) return true; if( s.charAt(0) != s.charAt(s.length()-1) ) return false; return isPalindrome( s.substring( 1, s.length() - 1 ) ); } }