Section 11.8. Creating Another Class

[Page 369 (continued)]

11.8. Creating Another Class

What 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.

[Page 370]

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.

Computer Science Idea: UML Class Diagrams

The standard way to show object-oriented designs is to use a UML class diagram. UML means the Unified Modeling Language. A class diagram shows each class in a rectangular box and relationships between classes with lines. A straight line (perhaps with an arrow at one end) shows an association between two classes. A straight line ending with an open triangle indicates inheritance. The open triangle is at the parent class end.

In Figure 11.16 we see that we have two classes in our design so far: Student and ClassPeriod and that both of these inherit from the class Object. There is a line connecting Student and ClassPeriod with an arrow pointing to the Student class. This means that a ClassPeriod object will keep track of the Student objects in it. The '*' at the end of the line near the Student class shows us that a ClassPeriod object can have 0 to many Student objects associated with it. The '5...8' at the ClassPeriod end means that a Student object can have five to eight ClassPeriod objects associated with it.

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]; }

[Page 371]

11.8.1. Adding Constructors

Next 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 Modifiers

Since 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; } 
[Page 372]
/** * Method to get the period number * @return the number for this period */ public int getPeriodNumber() { return periodNumber; } /** * Method to get a student based on the index * @return the student at this index or null if none at this * index */ public Student getStudent(int index) { return studentArray[index]; }

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; } 
[Page 373]
/** * Method to set the student at an index * @param studentObj the student object to use * @param index the index to set the student at */ public void setStudent(Student studentObj, int index) { this.studentArray[index] = studentObj; }

Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Year: 2007
Pages: 191 © 2008-2017.
If you may any questions please contact us: