// School4.java // contains some people // This class tries to break encapsulation, but cannot because Person4 class // does not return a pointer import javax.swing.JOptionPane; public class school4 { public static void main( String args[] ) { Person4 pArray[] = new Person4[2]; pArray[0] = new Student4(); // calls Student4 constructor pArray[1] = new Faculty4(); // calls Faculty4 constructor Student4 joe; Faculty4 jane; int joeClasses[]; // append String version of time to String output String output = "Before initializing Joe. \nJoe's first name is: " + pArray[0].getFirst() + "\nJoe's last name is: " + pArray[0].getLast() + "\nJoe's age is: " + pArray[0].getAge() ; joe = (Student4) pArray[0]; joeClasses = joe.getClasses(); output += "\nJoe's classes are:"; for (int i = 0; i < joe.getNumClasses(); i++) { output += "\t " + joe.getClass(i); } // change Joe's name and add classes pArray[0].setFirst("Joe"); pArray[0].setLast("Brown"); pArray[0].setAge(25); joe.addClass(312101); joe.addClass(309130); joe.addClass(340112); output += "\n\nRecord type: " + pArray[0].getType(); output += "\n\nJoe's first name after changing is: " + pArray[0].getFirst() + "\nJoe's last name is: " + pArray[0].getLast() + "\nJoe's age is: " + pArray[0].getAge(); output += "\nJoe's classes before refetching are:"; // this works because pArray[0] is a pointer to what pArray[0].getClasses() returns! // Note how encapsulation is maintained! for (int i = 0; i < joe.getNumClasses(); i++) { output += "\t " + joe.getClass(i); } // see what happens! Encapsulation is not broken! joeClasses[5] = 9992222; output += "\n5th index is: " + joe.getClass(5); // now refetch the information joeClasses = joe.getClasses(); output += "\nJoe's classes after refetching are:"; for (int i = 0; i < joe.getNumClasses(); i++) { output += "\t " + joe.getClass(i); } // get jane faculty object jane = (Faculty4) pArray[1]; // change Jane's name and add classes pArray[1].setFirst("Jane"); pArray[1].setLast("Jones"); pArray[1].setAge(21); jane.addClass(212101); jane.addClass(209130); jane.addClass(240112); output += "\n\nRecord type: " + pArray[1].getType(); output += "\n\nJane's first name after changing is: " + pArray[1].getFirst() + "\nJane's last name is: " + pArray[1].getLast() + "\nJane's age is: " + pArray[1].getAge(); output += "\nJane's classes are:"; int janeClasses[]; janeClasses = jane.getClasses(); for (int i = 0; i < jane.getNumClasses(); i++) { output += "\t " + jane.getClass(i); } JOptionPane.showMessageDialog( null, output, "Testing Class Time1", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } // end main } // end class school4 /************************************************************************** * (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. * *************************************************************************/