// Student4.java // Student class extends the Person4 class and adds an array for classes // This class clones the class array when it returns it; maintains encapsulation public class Student4 extends Person4 { private int lastClass; // age private int classes[]; // classes taking // Student4 constructor initializes each instance variable including classes public Student4() { super( ); int temp[] = {0}; classes = new int[20]; setStudent(temp); } // set a new Student4 value // validity checks on the age; set invalid values to 20 public void setStudent( int classes[] ) { int i; for (i = 0; i < classes.length; i++) { this.classes[i] = classes[i]; } lastClass = i - 1; } public int[] getClasses() { return classes.clone(); } public void addClass(int newClass) { if (lastClass == classes.length - 1) return; else if (classes[lastClass] == 0) classes[lastClass] = newClass; else classes[++lastClass] = newClass; } public int getNumClasses() { return lastClass + 1; } public int getClass(int theElement) { return classes[theElement]; } public String getType( ){ return "Student"; } } // end class Student4 /************************************************************************** * (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. * *************************************************************************/