public class ArrayAsAPropertyDemo { public static void main(String[] args) { Student stud1 = new Student(); // displaying original schedule for (int i = 0; i < stud1.getSchedule().length; i++) { String[] schedule = stud1.getSchedule(); // more efficient to move // this statement above loop String course = schedule[i]; System.out.println(course); } // changing the students schedule. String[] newSched = {"Gym", "Bio", "Music", "French", "Latin", "Art"}; stud1.makeNewSchedule(newSched); // Note that the students new schedule is of length 6 while the old // schedule had a length of 5. This is obviously legal to do in this // situation. // dropping Gym and taking Physics. Note that the changeSchedule method // is user-friendly and that 1 is subtracted behind-the-scenes so // 8th graders who never took VB or Java aren't confused! stud1.changeSchedule("Physics", 1); System.out.println(); // displaying new schedule // same for loop as above only without using extra, temporary variables for (int i = 0; i < stud1.getSchedule().length; i++) { System.out.println(stud1.getSchedule()[i]); } } } // ************************ Student class *********************** // Note that you can combine a regular class (below) with // a client program (above) as long as you don't make the class public // This is horrible style and certainly not taught on the AP exam. But // it's convenient for an instructor to do in demo programs. class Student { // default constructor public Student() { mySchedule = new String[5]; mySchedule[0] = "Chem" ; mySchedule[1] = "History"; mySchedule[2] = "Calc"; mySchedule[3] = "English"; mySchedule[4] = "Java" ; // 0 1 2 3 4 // Chem History Calc English Java } // accessor methods public String getName() { return myName; } public String[] getSchedule() { return mySchedule; } // modifier methods public void setName(String newName) { myName = newName; } // interesting methods public void changeSchedule(String newCourse, int period) { mySchedule[period - 1] = newCourse; } public void makeNewSchedule(String[] newSchedule) { mySchedule = newSchedule; } // properties private String myName; private String[] mySchedule; }