// Scott DeRuiter 7/5/02 // Hailstone.java // Program prompts the user for a positive integer, // then prints out the "hailstone" sequence. // 7/15/2011 - Greenstein: Changed TextReader to Scanner import java.util.Scanner; 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 ( ); hail.PrintCount ( ); } public void GetNumber ( ) { Scanner keyboard = new Scanner ( System.in ); System.out.print ( "\n\nEnter a positive integer ( 1 - 10000 ) : " ); number = keyboard.nextInt ( ); } public void PrintSequence ( ) { System.out.println ( "\n" ); if ( number > 0 && number <= 10000 ) { while ( number != 1 ) { System.out.print ( Format.left ( number, 7 ) ); counter++; if ( number % 2 == 0 ) number /= 2; else number = number * 3 + 1; if ( counter % 10 == 0 ) System.out.println ( "\n" ); } System.out.print ( Format.left ( number, 7 ) ); } } public void PrintCount ( ) { System.out.println ( "\n\nThe loop executed " + (counter+1) + " times\n\n\n" ); } }