// School2.java // contains some people // This class breaks encapsulation by getting an array, which is really a pointer, from a Person object import javax.swing.JOptionPane; public class school { public static void main( String args[] ) { Person2 joe = new Person2(); // calls Person constructor Person2 jane = new Person2(); // calls Person constructor // append String version of time to String output String output = "Before initializing Joe. \nJoe's first name is: " + joe.getFirst() + "\nJoe's last name is: " + joe.getLast() + "\nJoe's age is: " + joe.getAge() ; int joeClasses[]; joeClasses = joe.getClasses(); output += "\nJoe's classes are:"; for (int i = 0; i < joe.getNumClasses(); i++) { output += "\t " + joeClasses[i]; } // change Joe's name and add classes joe.setFirst("Joe"); joe.setLast("Brown"); joe.setAge(25); joe.addClass(312101); joe.addClass(309130); joe.addClass(340112); output += "\n\nJoe's first name after changing is: " + joe.getFirst() + "\nJoe's last name is: " + joe.getLast() + "\nJoe's age is: " + joe.getAge(); output += "\nJoe's classes are:"; // this works because joeClasses is a pointer to what joe.getClasses() returns! // Note how encapsulation is broken! for (int i = 0; i < joe.getNumClasses(); i++) { output += "\t " + joeClasses[i]; } // see what happens! Encapsulation is broken! joeClasses[5] = 9992222; output += "\n5th index is: " + joe.getClass(5); // change Jane's name and add classes jane.setFirst("Jane"); jane.setLast("Jones"); jane.setAge(21); jane.addClass(212101); jane.addClass(209130); jane.addClass(240112); output += "\n\nJane's first name after changing is: " + jane.getFirst() + "\nJane's last name is: " + jane.getLast() + "\nJane's age is: " + jane.getAge(); output += "\nJane's classes are:"; int janeClasses[]; janeClasses = jane.getClasses(); for (int i = 0; i < jane.getNumClasses(); i++) { output += "\t " + janeClasses[i]; } JOptionPane.showMessageDialog( null, output, "Testing Class Time1", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } // end main } // end class TimeTest1 /************************************************************************** * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and * * Prentice Hall. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/