Archive for February 14, 2008

Chapter 9, 9.3

1. Write a loop that prints all of the items in an array a to the terminal screen. – int i = 0      for (i==0, i<a.length, i++)          System.out.print(a[i]) 2. Repeat Question 1 put but print the items in reverse order. – int i = a.length – 1     for (i == a.length – 1; i>=0; i –)     System.out.print(a[i])3. —  int index;          for (int i =0; i < a. length; i ++){           if (a[i] <0 ){              index = i;              break;         } else {              index = a.length;        }     System.out.print (index) ;4. Describe what the following code segments do:    a. It will change all the negative values of a to positive value by taking the absolute values of them.    b. the loop will just get the sum of all elements of the a array. 5. What is the advantage of using the instance variable length variable length in a loop in a loop with an array.  – It tells the operator precisely how many elements inside the array.

Chapter 9, 9.2

1. Assume that the array a contains the five integers 34, 23, 67, 89, and 12. Write the values of the following expressions:- a. 34  b. a[a.length -1] =4  c. a[2]+ a[3] = 156What happens when a program attempts to access an item at an index that is less than 0 or greater than or equal to the array’s length?– The JVM checks the values of subscripts before using them and throws an ArrayIndexOutOfBoundsException if they are out of bound. 

Chapter 9, 9.1

1. A Program needs many variables to store and process data, How does an array solve this problem?– An array can group similar data with one array that contains many elements2. How does the programmer access an item in an array?– They use the name and the index to access it.3. Mary is using an array of doubles to store an employee’s wage amounts for each day of the week(Monday through Friday). Draw a picture of this array with sample items and references to each one.–