ANSWERS FOR REVIEW QUESTIONS ON JAN. 31, 2008 Problem 1. +---+---------+------+------+------+------+------+------+------+------+ | i | j | a[0] | a[1] | a[2] | a[3] | a[4] | a[5] | a[6] | temp | +---+---------+------+------+------+------+------+------+------+------+ | 1 | 10 | 42 | 26 | 19 | 35 | 21 | 59 | 40 | 42 | +---+---------+------+------+------+------+------+------+------+------+ | 2 | 210 | 26 | 42 | 19 | 35 | 21 | 59 | 40 | 19 | +---+---------+------+------+------+------+------+------+------+------+ | 3 | 32 | 19 | 26 | 42 | 35 | 21 | 59 | 40 | 35 | +---+---------+------+------+------+------+------+------+------+------+ | 4 | 432 | 19 | 26 | 35 | 42 | 21 | 59 | 40 | 21 | +---+---------+------+------+------+------+------+------+------+------+ | 2 | 5 | 19 | 21 | 26 | 35 | 42 | 59 | 40 | 59 | +---+---------+------+------+------+------+------+------+------+------+ | 2 | 65 | 19 | 21 | 26 | 35 | 42 | 59 | 40 | 40 | +---+---------+------+------+------+------+------+------+------+------+ | 2 | | 19 | 21 | 26 | 35 | 42 | 40 | 59 | 40 | +---+---------+------+------+------+------+------+------+------+------+ Problem 2. // Find the Person object of minimum age and corresponding index public class FindMin { public static void main(String[ ] argv) { Person a[ ] = { new Person("Chloe", 'F', 27), new Person("Brandon", 'M', 18), new Person("Susan", 'F', 34), new Person("Larry", 'M', 35), new Person("Alice", 'F', 23), new Person("David", 'M', 29), new Person("Judy", 'F', 19) }; int min, minindex; // Set initial values of min and minindex. // These would be the values if the array // were of length 1. min = a[0].getAge( ); minindex = 0; // If new value is less than the min, // update values. for(int i = 1; i <= a.length - 1; i++) if (a[i].getAge( ) < min) { min = a[i].getAge( ); minindex = i; } // Print min value and corresponding index. System.out.println("The min age is " + min + "."); System.out.println("Index is " + minindex + "."); } } Problem 3. // This program inputs the gender and returns an // array of all objects having that gender. import java.util.Scanner; import java.io.FileReader; import java.io.FileNotFoundException; public class FindGender { public static void main(String[ ] argv) throws FileNotFoundException { final int SIZE = 20; Person[ ] p = new Person[SIZE]; String theName = ""; char theGender = '\0'; int theAge = -1; Scanner s = new Scanner(new FileReader("persons.txt")); // Create Person array from file. for(int i = 0; i < p.length && s.hasNextLine( ); i++) { theName = s.next( ); theGender = s.next( ).charAt(0); theAge = s.nextInt( ); p[i] = new Person(theName, theGender, theAge); } // Print original array p. for(int i = 0; i < p.length && p[i] != null; i++) System.out.println(p[i]); // Input gender. Scanner con = new Scanner(System.in); System.out.print("Enter gender to find: "); char genderToFind = con.next( ).charAt(0); // Create new array for output. Person[ ] q = new Person[p.length]; // Put all Person objects of given gender in array q. // int genderIndex = 0; for(int i = 0, int genderIndex = 0; i < p.length && p[i] != null; i++) if (p[i].getGender( ) == genderToFind) q[genderIndex++] = p[i]; // Print output array q. for(int i = 0; i < q.length && q[i] != null; i++) System.out.println(q[i]); } }