11.8. Creating Another ClassWhat if we want to represent a class period? We might want to keep track of the teacher's name, the period number, and the students in that period. Should we just add this to the class Student? A student is in a class period, but a student isn't a class period. It sounds like we need another class which we could name ClassPeriod. The teacher's name can be represented by a String object. We can use the type int to represent the period number. What type would we use for the students in the period? This can be an array of Student objects. If we know the maximum number of students that we can have in a class, we can go ahead and create an array of that size. Say we can only have 35 students at most.
Figure 11.16. UML class diagram.We have enough information to begin the new class declaration: /** * ClassPeriod represents a class period which has a teacher, * a period number, and an array of students */ public class ClassPeriod { //////////////// fields ///////////////////////// private String teacherName; private int periodNumber; private Student[] studentArray = new Student[35]; } 11.8.1. Adding ConstructorsNext we need to add constructors. We might not know anything about this new class period, or we might know only the teacher name, we might know both the teacher name and period number. We will create constructors for each of these cases. The constructors are declared inside of the open and close curly braces that define the block for the class definition. Be sure to add the constructors before the closing curly brace '}' that ends the class definition. ///////////////// constructors ////////////////// /** * No-argument constructor. Leaves fields with * default values. */ public ClassPeriod() {} /** * Constructor that takes just the teacher's name * @param name the name for the teacher */ public ClassPeriod(String name) { this.teacherName = name; } /** * Constructor that takes the teacher's name and period number * @param name the namefor the teacher * @param num the numberfor the class period */ public ClassPeriod(String name, int num) { this.teacherName = name; this.periodNumber = num; } 11.8.2. Adding Accessors and ModifiersSince the fields are private they can only be directly accessed by code in the current class definition. In order to let code from other classes (declared in other files) be able to ask a ClassPeriod for its information we need to provide public methods that return the information. We do this by creating methods called accessors. Methods should be added before the closing curly brace '}' in the block that follows the class declaration. /////////////////////////// methods //////////////////////// /** * Method to get the teacher's name * @return the name of the teacher, or null if none yet */ public String getTeacherName() { return teacherName; } What if we want to set the value of a field? We need to create public methods that allow these values to be set. These are called modifier or mutator methods. We have to decide when this is allowed. Should another class be able to set the teacher name, or should we only allow it to be set if it is currently null? Does it ever happen that a teacher's name will change during the school year? Sure, a teacher may get married and change her name. Or what if a teacher leaves during the school year due to illness or pregnancy? We should allow the name to be changed. What about the period number? Does this ever change? Probably not, so let's not allow it to change if it has been set. We will be able to tell if it has been set because the default value for a number field is 0 and class periods start with 1. Should we allow another class to change the array that studentArray refers to? Probably not, but we can change the student in the array at a specified index. /** * Method to set the teacher's name * @param name the name to use */ public void setTeacherName(String name) { this.teacherName = name; } /** * Method to set the period number (if it hasn't been set) * @param num the number to use * @return flag to say if set worked */ public boolean setPeriodNumber(int num) { if (this.periodNumber == 0) { this.periodNumber = num; return true; } else return false; } |