Open bryankwe opened 2 years ago
I think that statement means that TrafficLight would have that method but we don't need the method body. (abstract method) Your implementation is correct but you can leave toggle() for TrafficLight as abstract.
For a), one statement that is included is "Notice that the TrafficLight class has the desirable property that it can be toggled between red and green only." The desirable property, in this case, would be the toggle method in TrafficLight since red and green are fixed there. So just have to make sure the colours are not fixed there, eg "red"/"green".
For d), I think one way to do it is to use child classes so that they can have different behaviours without changing the original classes, and to demonstrate you just have to call toggling on the child class.
I agree with jamietan123, I think the question is suggesting that TrafficLight class should be set as abstract to facilitate more colours (not specific to red and green )
Hi do yall know how to approach Q2 parts c and d? considering that there should be no cyclic dependencies, and so we can't return other Red/Green/AmberLights in their toggle methods? thank you
Hi, do you know where i can get the answers? have been searching everywhre but to no avail i cant find it
Hi do yall know how to approach Q2 parts c and d? considering that there should be no cyclic dependencies, and so we can't return other Red/Green/AmberLights in their toggle methods? thank you
^^^ yeah im not very sure how to tackle this problem either
for 2c i think if red returns green , green returns amber and amber returns red there is no cyclic dependency as no 2 classes depend on each other
Qns 2c and 2d are very challenging for me, I kept ending up with cyclical dependencies
hi! you could check out #536 on one way to solve 2d
Source
AY2021/2022 Semester 1 (LumiNUS Mock Paper)
Description
Hi all, I'm not too sure what part (a) means by "TrafficLight need only have the desirable property that it can be toggled, but not the specific color of the lights" I guess intuitively I understand that
TrafficLight
main function is totoggle()
but I dont really understand that statement.Here is my implementation (it works, but am I doing anything wrong?):
Also, I don't really understand what part (d) is asking for. How do we get the requested output if we can't modify any of the classes or methods? Or are we just supposed to explain (rather than code)?
Would really appreciate any comments / discussion. Thanks in advance :)