// COMP 1631 (Fall 2006) // Author(s): 1631 instructors // // Lab Test code - Tuesday solution // // This class is just a holder for the main method which contains two loops // and a blank space where students are required to insert code (which will // involve another loop). public class LoopyTuesday { public static void main(String[] args) { final int defaultSize = 30; // default size of array int n; // user-specified size of array int[] data; // uninitialized array int index; // index into array // get size of array and "build" it n = Console.readInt("Please enter a positive integer: "); if (n <= 0) { n = defaultSize; } // if data = new int[n]; // fill array with values 0, 1, 2, 3, ... index = 0; while (index < n) { data[index] = index; index++; } // while //------------------------------------------------------------------------ // fill the array with the sequence 1, 2, 4, 8, 16, 32, ... int powerOf2 = 1; index = 0; while (index < n) { data[index] = powerOf2; powerOf2 = powerOf2 * 2; index++; } // while //------------------------------------------------------------------------ // print out the array System.out.println(); // blank line System.out.println("Here are the values in the array:"); index = 0; while (index < n) { System.out.println(" data[" + index + "] = " + data[index]); index++; } // while } // main method } // end class LoopyTuesday