Data Modeling 2

Created By: Geoffrey Challen
/ Updated: 2022-05-23

In this lesson we stop and put all that we've learned this week into action. We'll complete another object modeling exercise together. But this time, we'll make use of inheritance, polymorphism, and other new ideas that we recently learned. Welcome back!

Warm Up Debugging Challenge

But, as we frequently do, let's warm up with another graded debugging challenge!

Modeling Office Hours

Last time we did a game, and I said: I'm sorry we're doing a game. This time we're going to do something that may seem a bit like navel-gazing. And I'll say: I'm sorry we're doing something course-related. This is what I think about!

So let's model office hours.

Classes and Relationship

One way to begin object modeling is to think about what kind of entities we need to model and what kind of relationships they should have with each other. Let's start there:

Define the different classes that we are going to use for this problem. Focus on their relationships here, not on their properties or methods. Goal is to be able to track who is in office hours, and maintain a count and list of staff and students who are present.

Class Properties and Methods

Next, let's consider the kind of actions and methods that our objects need to support. That will help guide us as we add required instance variables.

Discuss the enter and leave methods on the Room class. Talk about what kind of state the Room class will need to maintain.

Implementing Initial Methods

Next, we'll pick a pair of the methods on our Room class to implement.

Implement the enter and leave methods using a private array. Talk about de-null-ing the array and maintaining the count.

Finishing Up

Finally, we'll finish at least a preliminary implementation of our office hours model. And then discuss how you could extend and improve it!

Add the print method to room. Discuss dealing with duplicate students or staff, and show how to handle this using unique ID on the appropriate classes.

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Show how to complete the homework problem above. Feel free to cover multiple approaches!

More Practice

Need more practice? Head over to the practice page.