// Scott DeRuiter 7/5/02 // Hailstone.java // Program prompts the user for a positive integer, // then prints out the "hailstone" sequence. public class Hailstone { private int number, counter; public Hailstone ( ) { number = counter = 0; } public static void main ( String [] args ) { Hailstone hail = new Hailstone ( ); hail.GetNumber ( ); hail.PrintSequence ( ); } public void GetNumber ( ) { TextReader keyboard = new TextReader ( ); System.out.print ( "\n\nEnter a positive integer ( 1 - 10000 ) : " ); number = keyboard.readlnInt ( ); } public void PrintSequence ( ) { TextWriter outFile; String name = "hail.txt"; outFile = new TextWriter ( name ); outFile.println ( "" ); outFile.println ( "" ); System.out.println ( "\n" ); if ( number > 0 && number <= 10000 ) { while ( number != 1 ) { System.out.print ( Format.left ( number, 7 ) ); outFile.print ( Format.left ( number, 7 ) ); counter++; if ( number % 2 == 0 )number /= 2; elsenumber = number * 3 + 1; if ( counter % 10 == 0 ) { System.out.println ( "\n" ); outFile.println ( "\n" ); } } System.out.print ( Format.left ( number, 7 ) ); outFile.print ( Format.left ( number, 7 ) ); } PrintCount ( outFile ); outFile.close ( ); } public void PrintCount ( TextWriter t ) { System.out.println ( "\n\nThe loop executed " + (counter+1) + " times\n\n\n" ); t.println ( ); t.println ( ); t.println ( "The loop executed " + (counter+1) + " times" ); t.println ( ); t.println ( ); } }