CourseLoad The courseLoad attribute is meant to represent a list of all Course objects that the Student is presently enrolled in. So, it makes perfect sense that this attribute be declared to be simply a standard collection of Course objects !public class Student{private string name;private string studentId;private CollectionType courseLoad; // of Course objects// etc.
TranscriptThe transcript attribute is a bit more challenging. What is a transcript, in real-world terms? It's a report of all of the courses that a student has taken since he or she was first admitted to this school, along with the semester in which each course was taken, the number of credit hours that each course was worth, and the letter grade that the student received for the course. If we think of each entry in this list as an object, we can defin them via a TranscriptEntry class, representing an abstraction of a single line item on the transcript report, as follows:public class TranscriptEntry{ // One TranscriptEntry represents a single line item on a transcript report. private Course courseTaken; private string semesterTaken; // e.g., "Spring 2000" private string gradeReceived; // e.g., "B+" // Other details omitted ... // Note how we "talk to" the courseTaken object via its methods // to retrieve some of this information (delegation once again!). public void PrintTranscriptEntry() { // Reminder: \t is a tab character. Console.WriteLine(courseTaken.CourseNo + "\t" + courseTaken.Title + "\t" + courseTaken.CreditHours + "\t" + gradeReceived); } // etc.}public class Student{private string name;private string studentId;private CollectionType courseLoad; // of Course objects// etc.Note that we're declaring one of the attributes of TranscriptEntry to be of type Course, which means that each TranscriptEntry object will maintain a handle on its corresponding Course object. By doing this, the TranscriptEntry object can avail itself of the Course object's title, course number, or credit hour value (needed for computing the GPA)-all privately encapsulated in the Course object as attributes -by accessing the appropriate properties on that Course object as needed. Back in the Student class, we can now define the Student's transcript attribute to be a collection of TranscriptEntry objects. We can then add a PrintTranscript method to the Student class, the code for which is highlighted in the followingsnippet:public class Student{ private string name; private string studentId; // Pseudocode. private CollectionType transcript; // of TranscriptEntry objects // etc. // Details omitted ... public void PrintTranscript() {// Pseudocode.for (each TranscriptEntry t in transcript) {t.PrintTranscriptEntry();}}}
Transcript, Take 2Alternatively, we could use the technique of creating a wrapper class called Transcript to encapsulate some standard collection type, as we did withEnrollmentCollection in an earlier example:public class Transcript{private ArrayList transcriptEntries; // of TranscriptEntry objects// other attributes omitted from this example// Pseudocode.public void AddTranscriptEntry(arglist) {insert new entry into the transcriptEntries ArrayList -- details omitted}// We've transferred the logic of the Student class's PrintTranscript// method into THIS class instead.public void PrintTranscript() {// Pseudocode.for (each TranscriptEntry t in transcriptEntries) {t.PrintTranscriptEntry();}}// etc.}
We then can go back to the Student class and change our declaration of the transcript attribute from being a standard collection type to being of typeTranscript:public class Student{private string name;private string studentId;// etc.private Transcript transcript; // an ENCAPSULATED collection of// TranscriptEntry objects// etc.We can then turn around and simplify the PrintTranscript method of the
Student class accordingly:public class Student{// Details omitted.public void PrintTranscript(string filename) {// We now delegate the work to the Transcript attribute!transcript.Print();}// etc.This "take 2" approach of introducing two new classes/abstractions- TranscriptEntry and Transcript—is a bit more sophisticated than the first approach, where we only introduced TranscriptEntry as an abstraction. Also, this second approach is "truer" to the object paradigm, because the Student class needn't be complicated by the details of how Transcripts are represented or managed internally -those details are hidden inside of the Transcript class, as they should be. Our Completed Student Data Structure Table 6-2 illustrates how we've taken full advantage of collections to round out our Student class definition.Table 6-2. Rounding Out the Student Class's Data Structure with CollectionsSummaryIn this chapter, you've learned
ExercisesGiven the following abstraction:
A book is a collection of chapters, which are each collections of pages. sketch out the code for the Book, Chapter, and Page classes.
What generic type (s) of collection (s)-ordered list, sorted ordered list, set, dictionary - might you use to represent each of the following abstractions ? Explain your choices.
What collections do you think it would be important to maintain for the SRS, based on the requirements presented in the Introduction to this book ? What collections do you think it would be important to maintain for the Prescription Tracking System (PTS) described in Appendix B ?What collections do you think it would be important to maintain for the problem area that you described for exercise 3 in Chapter 2 ?