More with Classes
Professor Moriarty runs a lab where he and his students work on various experiments with rats. Each experiment starts in the morning at 8am and ends by 5pm, and each experiment requires observation and monitoring for the entire duration. Given the students' schedules, Professor Moriarty has been finding it increasingly difficult to manage times in the lab for all his students, so that some student is always available to observe and monitor the experiments. He needs our help in automating the process of scheduling students in the lab.
Every Friday, Professor Moriarty gets the schedules for all his students for the following week. He would like us to develop software that can read in the students' schedules, and generate a schedule for the lab for the following week.
Each student's commitments are for an hour at a time, so each day is divided into 9 blocks (block 0: 8am - 9am; block 1: 9am - 10am, etc.). Each student has the same schedule on Mondays, Wednesdays and Fridays, and the same schedule for Tuesdays and Thursdays.
The data file that Professor Moriarty can provide for us has information for one student on each line of the data file. The line of information for a student starts with the first name of the student, followed by the students schedule, i.e., blocks when the student has a commitment. Each student has one schedule for MWF and one schedule for TTh. The schedule for MWF is indicated by the letter 'M', and the schedule for TTh is indicated by the letter 'T'. End of the line indicates the end of information for that student.
Professor Moriarty assures us that he never has more than 20 students working in his lab, and that he allows a student to work in the lab only if the student's first name has no more than 10 characters. He also makes sure that he never has students with the same first name working in the lab at any given time.
For example, the line
Caroline T 1 4 7 M 2 4
indicates that Caroline is busy on MWF during blocks 2 and 4, and on TTh during blocks 1, 4 and 7.
Professor Moriarty assures us that there will be exactly one blank between the different pieces of information on each line, and that the end of line character will be right after the last piece of information.
The end of file indicates the end of the schedules for the students in the lab.
To begin with, we will develop a program that prints out the weekly availability of students. For example, if the input file is
Caroline T 1 4 7 M 2 4 Michael M 1 2 3 6 T 1 3 5 7 Gwen M 4 7 8 0 T 1 2 7
we would like our program to print out
Day 0 1 2 3 4 5 6 7 8 ----------------------------------------- |---|---|---|---|---|---|---|---|---|---| | M | | | | | | | | | | |---|---|---|---|---|---|---|---|---|---| | T | | X | | | | | | X | | |---|---|---|---|---|---|---|---|---|---| | W | | | | | | | | | | |---|---|---|---|---|---|---|---|---|---| | H | | X | | | | | | X | | |---|---|---|---|---|---|---|---|---|---| | F | | | | | | | | | | |---|---|---|---|---|---|---|---|---|---|
A blank indicates that at least one student is available during that block on that day; the letter 'X' indicates that no student is available during that block.
Assuming that we have a new type (a class
)
called Lab
that is capable of storing schedule
information for all the students in a lab, our
function main
could be:
int main () { Lab Moriarty; // Create an object to store information about // Professor Moriarty's lab Moriarty.readSchedules (); // Populate the lab object with information for all the // students in the lab Moriarty.printAvailability (); // Print out the availability of students for the week // as shown above. return (0); }
The class Lab
needs to be able to store schedule
information for all the students in the lab. Assuming we have a new
type (Student
) to store the information for one
student, a good data structure for storing information for all the
students would be an array of Student
objects, along
with the number of students working in the lab.
Each Student
object will contain the student's first
name and the schedules for the student.
From the function main
above, we see that the
class Lab
, besides the default constructor, needs at
least two public member
functions: readSchedules()
and printAvailability()
.
The member function readSchedules()
needs to read in
information for each student. It will be best to relegate the
reading of student information to the Student
class,
with the member function readSchedules()
requesting the
next student to be read, and keeping track of the number of
students. Thus, the function could be:
void readSchedules () { numStudents = 0; // ASSERT: numStudents = n is the number of students // whose information has been read so far while (!cin.eof()) { // ASSERT: there is at least one more student whose // information needs to be read. students[numStudents].readStudentInfo(); numStudents++; } }
We have used the variables numStudents
and students
in the function above. Where are they
declared? What are their initial values?
The above function suggests that we need a public member
function readStudentInfo()
to read in the information for
one student. We note the following: