nus-cs2030 / 2122-s2

CS2030 repository and wiki for AY 2021/2022 Sem 2
MIT License
0 stars 0 forks source link

Mock Paper [2122 Sem 1] Question 2 #524

Open bryankwe opened 2 years ago

bryankwe commented 2 years ago

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 to toggle() but I dont really understand that statement.

Here is my implementation (it works, but am I doing anything wrong?): image

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 :)

jamietan123 commented 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.

jingyulim1 commented 2 years ago

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.

spicyseaweed commented 2 years ago

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 )

wk1267 commented 2 years ago

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

tristky commented 2 years ago

Hi, do you know where i can get the answers? have been searching everywhre but to no avail i cant find it

rachtan27 commented 2 years ago

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

loqir commented 2 years ago

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

kyanroh commented 2 years ago

Qns 2c and 2d are very challenging for me, I kept ending up with cyclical dependencies

txytxytxytxytxytxy commented 2 years ago

hi! you could check out #536 on one way to solve 2d