// Source code example for "A Practical Introduction // to Data Structures and Algorithm Analysis" // by Clifford A. Shaffer, Prentice Hall, 1998. // Copyright 1998 by Clifford A. Shaffer import java.io.*; // Driver class for Collatz sequence generator test class Collatzmain { static boolean ODD(int val) { return (val & 1) != 0; } // Main routine for Collatz sequence generator public static void main(String args[]) throws IOException { Assert.notFalse(args.length == 1, "Usage: Collatzmain "); int n = Integer.parseInt(args[0]); int temp = n; System.out.println("The start value is: " + n); // This is not actually the book's code. That is repeated below. // The book's code does not have a print statement. while (n > 1) { if (ODD(n)) n = 3 * n + 1; else n = n / 2; System.out.print(n + " "); } n = temp; System.out.println(); // Here is the book's code, to insure syntactic correctness while (n > 1) if (ODD(n)) n = 3 * n + 1; else n = n / 2; System.in.read(); } }