public class ArrayOfObjectsDemo { public static void main(String[] args) { Student[] roster = new Student[10]; // instantiate the array for (int i = 0; i < roster.length; i++) { roster[i] = new Student(); // instantiate each Student } // within the array of Students // display all student schedules for (int j = 0; j < roster.length; j++) { System.out.print("\nStudent ID " + roster[j].getID() + ": "); for (int i = 0; i < roster[j].getSchedule().length; i++) { System.out.print(roster[j].getSchedule()[i] + " "); } } // ******************************************************************* // How many students are taking Calculus? int count = 0; // # of students taking Calculus for (int j = 0; j < roster.length; j++) { // Calculus is only offered in index position 2 of a schedule if (roster[j].getSchedule()[2].equals("Calculus")) { count++; } } System.out.println("\n" + count + " students are taking Calculus"); // ******************************************************************* // How many students are taking a class that begins with the letter H? count = 0; // # of students taking a course that begins with H // loop through the student roster for (int j = 0; j < roster.length; j++) { // loop through each student's schedule for (int i = 0; i < roster[j].getSchedule().length; i++) { if (roster[j].getSchedule()[i].substring(0, 1).equals("H")) { count++; break; // don't count student twice if he is taking both // History and Humanities } } } System.out.println(count + " students are taking a course that begins with H"); } } // ************************ Student class *********************** class Student { // default constructor public Student() { mySchedule = new String[5]; mySchedule[0] = Math.random() > .5 ? "Chem" : "Bio"; mySchedule[1] = Math.random() > .5 ? "History" : "Econ"; mySchedule[2] = Math.random() > .5 ? "Calculus" : "Statistics"; mySchedule[3] = Math.random() > .5 ? "English" : "Humanities"; mySchedule[4] = Math.random() > .5 ? "Java" : "Latin"; // For the AP exam, you don't need to know how to use the // ternary/selection operator ? : myID = currentID; currentID++; // increment currentID so next Student to be // instantiated has his own unique ID # } // accessor methods public int getID() { return myID; } public String[] getSchedule() { return mySchedule; } // interesting methods public void changeSchedule(String newCourse, int period) { mySchedule[period - 1] = newCourse; } public void makeNewSchedule(String[] newSchedule) { mySchedule = newSchedule; } // properties private static int currentID = 0; // static property for ID's to be unique private int myID; // each student has unique ID # private String[] mySchedule; }