Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
This Setter methods that check new values worksheet is part of the VCE Applied Computing U1O2 Programming: Object-oriented programming journey. Print it as a booklet?
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1def set_balance(self, amount): | ADefines a setter method that receives a new value for the private balance attribute so it can be checked before it is stored | |
| 2if amount >= 0: self._balance = amount | Bdef get_balance(self): return self._balance | |
| 3A setter can reject a bad value by refusing to change the attribute and giving feedback instead | CChecks the new value is valid and only then updates the private attribute | |
| 4A getter and setter pair lets code read a private attribute and change it safely | Delse: print("Invalid amount") | |
| 5def set_balance(self, amount): self._balance = amount | EThe value is saved to a local variable called balance, not to self._balance, so the object's attribute never changes | |
| 6def set_balance(self, amount): if amount >= 0: balance = amount | FThe setter stores the value without checking it, so a negative amount could be saved and the object would hold bad data |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
def set_balance(self, amount): — A setter method that changes a private attribute only after checking the new value is valid, such as rejecting a negative amount, so the object never holds bad data.def set_balance(self, amount):
if amount >= 0:
self._balance = amountif amount >= 0:
self._balance = amountif amount < 0:
returndef get_balance(self):
return self._balanceself._balance = amount