// COMP 1631 (Fall 2006) // Author(s): 1631 instructors // // Lab Test code - Thursday 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 LoopyThursday { public static void main(String[] args) { final int defaultSize = 40; // 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 <= 2) { 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 3, 5, 8, 13, 21, ... (the first two // elements are fixed, and every other element is the sum of the two elements // immediately preceding it) data[0] = 3; data[1] = 5; index = 2; while (index < n) { data[index] = data[index - 1] + data[index - 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 LoopyThursday