User Defined Types  «Prev  Next»

When To Use Composition - Exercise

Use composition in the course project

Objective: Build a more robust Check class.


This exercise is not scored. It's an opportunity for you to check your understanding of the material covered in the preceding lesson. When you are finished, click the Submit button to view the suggested results.


The Check class has a number of separate attributes that should really be encapsulated into classes of their own, because they have business rules. (For example, the cents attribute cannot be over 99, and the month attribute cannot be over 12.) These business rules are likely to be used elsewhere, too (think about the balance attribute of the CheckingAccount class, for example).


Define a Money class that has integer attributes for dollars and cents.
Then define a Date class with integer attributes for days, years, and months.
Use these two new classes and composition to create a better definition for a Check class. Finally, draw a class diagram that shows how all three classes are related.