Identify Responsibilities and Collaborators - Quiz Explanation
The correct answers are indicated below, along with text that explains the correct answers.
1.
The Checkbook Manager has a class called CheckingAccount. Which of the following is a good responsibility for the CheckingAccount class?
Please select the best answer.
A.
Print the bank's name and address on reports.
B.
Track the balance in the account.
C.
Report on interest rates available on savings accounts.
D.
Prepare a summary for tax calculations.
The correct answer is B.
A is incorrect because the details of information about the bank belong to another class (probably Bank). C is incorrect because a SavingsAccount class would report on interest rates. D is incorrect because tax rules and regulations, if they are even in the scope of this application, belong in another class.
2.
CheckingAccount has a responsibility to provide the account balance on request. Which statement about collaborations is most correct?
Please select the best answer.
A.
CheckingAccount will collaborate with Deposit and Withdrawal classes to get the amounts of all outstanding transactions and total them.
B.
CheckingAccount will collaborate with AccountHolder to find the individual's current checking balance.
C.
CheckingAccount will not collaborate with another class to perform this responsibility.
D.
CheckingAccount will collaborate with Statement because Statement requests the balance.
The correct answer is C.
A is incorrect because the current balance is sure to be stored in the account rather than calculated every time. B is incorrect because an account holder might have no checking account, or two, so storing the balance in AccountHolder isn't feasible. D is incorrect because classes that request your service do not collaborate with you: The relationship is one way.
3.
CheckingAccount has a responsibility to print certain reports for the user. These reports require the bank's name and address on them. CheckingAccount will collaborate with the Bank object to get this information. Which one of the following statements best describes a relationship needed by this collaboration?
Please select the best answer.
A.
The checking account should know its bank, so it can delegate the work.
B.
The CheckingAccount class should be part of the Bank class.
C.
There should be a global Bank variable all CheckingAccount objects can reach.
D.
The Bank class should be part of the CheckingAccount class.
The correct answer is A.
Whenever two classes will share work, one must know how to find the other. B and D are incorrect because there is no requirement for one class to be part of another--separating responsibilities is important. C is incorrect because many object-oriented languages do not allow global variables, and there is no need to rely on them.