discussion solution last years exam

Gipzo
Erstie
Erstie
Beiträge: 13
Registriert: 26. Mai 2015 15:48

discussion solution last years exam

Beitrag von Gipzo »

Hi,

because there is no official solution of the last years exam, I think it's quite useful to discuss the different solutions of each other.
I opened this thread to do so.

I will upload my solution soon, when it's finished.
Meanwhile feel free to upload and discuss your solutions.

Gipzo
Erstie
Erstie
Beiträge: 13
Registriert: 26. Mai 2015 15:48

Re: discussion solution last years exam

Beitrag von Gipzo »

These are my answers to most of the tasks in Topic 2 - 5. Im not sure enough about the other solutions to upload them. So please share your answers here.

Topic 2
a)
Visitor – Pattern:
- Trait Visitor is the visitor interface with all visit methods declared (no concrete Visitors are implemented)
- The all trait Statement provides a method accept so every Subclass of it has to implement it and can be visited

Decorator-Pattern:
- InterpretedStatement is Decorator
- Decorates method interpret

Observer-Pattern:
- Trait Statement is Subject which hold all Observers in errorListener
- It can register listeners and fire events
- The interpret methods in the classes that inherit from Statement have a case fireInterpretationError which is equal to notify() in the ObserverPattern

These are the ones that can be seen at first sight, please add patterns I forgot

b)
1)
Open for other types of conditions. Closed for new Statements as a result of the visitor pattern.

2)
New condition can be added like an equal condition.

3)
As written before the visitor pattern is in conflict with the OCP, because when you want to add new statements they need to implement an accept method and it’s also necessary to add a visit method to the Visitor trait and its subclasses

4)
Every case of interpret in the subclasses of Statement where no error is fired statement.interpret(…) is called except in class Return. So not every subclasses has the same behavior in this case.

5)
It would be possible to add Expr trait, with concrete expression classes, but as mentioned before there is much work to do that the visitor pattern also apllies to the new extensions

Topic 3
a)
I’m not sure if I understand the task correctly, but I think you should mark every line that is accepted by the compiler with √ and the ones not accepted with x. So my solution is: (√,√,x, √,x,x,x)

Topic 4 is missing in the exam

Topic 5
a)
slides RP2 page 19

b)1)
slides RP2 page 45

2)
You don’t want update Signal multiple times, if you access a signal value before it was updated “enough” times but after it was updated the first time, you might get wrong values

3)
Graph Configuration:
A -> B; A -> C; B -> D; C -> D
In propagation order abdc a glitch occurs, because d gets updated after b is updated and again again after c is updated.
In propagation order abcd does not lead to glitch

c)
val e1 = Evt[Int]()
val e2 = Evt[Unit]()
val s1 = Signal[List[Int]] = e1.last(10)
val s3 = Signal{ s1.now }
val s = e2.snapshot(s3)
val s4 = Signal[Int] = e2.count

ms86coku
Neuling
Neuling
Beiträge: 1
Registriert: 11. Feb 2017 18:26

Re: discussion solution last years exam

Beitrag von ms86coku »

Hi,
Topic 3
a) (√,√,x, x,x,√,x)
Topic 5
c)
val e1 = Evt[Int]() // same
val e2 = Evt[Unit]() // same
val s1 = Signal[List[Int]] = e1.last(10) // same
val s2 = Signal{ s1.sum } // ?
val s3 = e2.snapshot(s1) // ?
val s4 = Signal[Int] = e2.count // same


This is my solution only for these 2 problems, but im not 100% sure about it. Maybe someone else can judge it,if possible.

Best, Hu

Gipzo
Erstie
Erstie
Beiträge: 13
Registriert: 26. Mai 2015 15:48

Re: discussion solution last years exam

Beitrag von Gipzo »

Hi,

could you explain your solution in Topic 3 to me please?

tz33quvu
Windoof-User
Windoof-User
Beiträge: 40
Registriert: 29. Jan 2017 09:26

Re: discussion solution last years exam

Beitrag von tz33quvu »

Hi
I think in topic 5 s2 should be like val s2:Signal[Int]=s1.fold(0)((x:Int,y:Int)=>(x+y)) regarding to the ppt

in topic 3
type mismatch; found : etwas.b3.coordinate required: etwas.b1.coordinate
b4 is actually a reference to b1

Do you have solution to topic 1? I am quiet confused with topic 1 b)

Terry

Erdi
Erstie
Erstie
Beiträge: 22
Registriert: 1. Nov 2011 18:01

Re: discussion solution last years exam

Beitrag von Erdi »

Regarding topic 2a)
I think the Decorator you identified is a Proxy as is either calls the inprepret method of the object it holds OR something other. A Decorator always would at some point call the interpret method of the object it holds.

Correct me if I'm wrong ;-)

I am also really interested in solutions about topic 1, I will post my solutions soon, but there are a lot of things I am not sure about.

Benutzeravatar
Mark_G
Mausschubser
Mausschubser
Beiträge: 67
Registriert: 8. Okt 2008 23:07

Re: discussion solution last years exam

Beitrag von Mark_G »

I hope our exam will be as simple as the last one :)

pschulz
Neuling
Neuling
Beiträge: 9
Registriert: 7. Nov 2016 18:15

Re: discussion solution last years exam

Beitrag von pschulz »

Topic 2
a)
Visitor – Pattern:
- Trait Visitor is the visitor interface with all visit methods declared (no concrete Visitors are implemented)
- The all trait Statement provides a method accept so every Subclass of it has to implement it and can be visited
agree
Decorator-Pattern:
- InterpretedStatement is Decorator
- Decorates method interpret
dissagree like allready said
Observer-Pattern:
- Trait Statement is Subject which hold all Observers in errorListener
- It can register listeners and fire events
- The interpret methods in the classes that inherit from Statement have a case fireInterpretationError which is equal to notify() in the ObserverPattern

These are the ones that can be seen at first sight, please add patterns I forgot
agree
I also found(?)

Template Pattern:
case class Condition:
Has a concrete use of method check() <- Template Method
which is not defined.

Concrete instance have to define check()

FactoryPattern:
object Condition creates Condition objects

David Artmann
Neuling
Neuling
Beiträge: 9
Registriert: 26. Okt 2016 20:13

Re: discussion solution last years exam

Beitrag von David Artmann »

Topic 1:
a)
1) False, because most modern programming languages of both worlds, FP and OOP, are multi-paradigm by offering mechanisms for either write FP or OOP code. (This could also be answered the other way around, by stating that the with FP languages are meant which only offer FP functionality)
2) True, because FP is part of DP.
3) False, trait can implement methods.
4) True, most design patterns, per default, involve more than one class.
5) False, if so, all would be formally defined and they are just guidelines.
6) True, pattern matching is an elegant way to realize the VP in scala.

b)
1)
- Fields, declared outside and referenced inside of Java Lambdas need to be set final.
- Return statement must be explicitely defined in Java. (This does not directly relate to closures in Java, but I had no other second answer...)
2) Declare method final.
3) Bridge pattern
4)
- New method "addAll()" gets added to base class but not overriden in EncryptedDirectory. This can lead to a security risk, when clients use addAll assuming is also offers encryption functionality.
- new method "addAll()" gets added with same signature as in EncryptedDirectory. Then EncryptedDirectory needs to be at least changed by adding override annotation.

c)
Lin(F) = {F, Lin(E) >> Lin(D)}
= {F, Lin(E) >> {D, B}}
= {F, {E, Lin(C) >> Lin(A) >> Lin(D)} >> {D, B}}
= {F, {E, Lin(C) >> Lin(A) >> {D, B}} >> {D, B}}
= {F, {E, {C, A, B} >> {A} >> {D, B}} >> {D, B}}
= {F, {E, C, A, B, D} >> {D, B}}
= {F, E, C, A, D, B}

d)
- Forwarding: callee.m()
- Delegation: callee.m(this)

Topic 2:
a)
Visitor Pattern: trait Visitor is visitor interface, every extending class a ConcreteVisitor.
Template Method Pattern: case class Condition takes function "check" which is used inside of "interpretCondition".
Factory Pattern: "createGreaterThan" creates Condition object by singleton of Condition object. (unsure about that)
Proxy Pattern: InterpretedStatement is proxy for field "original" and delegates in case of None.

b)
1)
- Open for extension by adding further methods to the Condition object like "createLowerThan". (Originally I thought of adding more cases to the case classes, but I did not found a valid example)
- Closed for modification by subclassing Statement.
2)
- Extend trait Visitor to print out each visited Element.
- Add further operations to object Condition.
3)
?
4) No violations to LSP detected.
5) Introduce new trait for ErrorHandling which extends Statement and gets responsibility for errorListener.

c)
Trait mixin compositions. Delegation established at runtime -> preserves object identity.

d)
(just sketched, certainly syntax is not correct)
Template Method Pattern implementation:

Code: Alles auswählen

class TemplateFilteredSet[+A] extends Set[+A] {
	def filter(): Set[+A] = {
		(...)
		if(pred(elem)) { (...) }
		(...)
	}
	def pred(a: +A): Boolean
}
Advantage: increases flexibility, because subclasses implement low-level mechanisms.
Disadvantage: not reusable low-level mechanisms.

Strategy Pattern implementation:

Code: Alles auswählen

class StrategyFilteredSet[+A] extends Set[+A] with Pred[+A] {
	(...)
	def filter(): Seq[+A] = {
		(...)
		if (pred(elem)) { (...) }
		(...)
	}
}
trait Pred[+A] {
	def pred(a: +A): Boolean
}
trait ConcretePred[Int] {
	override def pred(i: Int): Boolean = { (...) }
}
Advantage: Strategy interchangeable during runtime.
Disadvantage: Strategy Pattern causes increased amount of objects.

Topic 3:
a)
√, √, x, x, x, √, x

b)

Code: Alles auswählen

def eval(env: Map[String, Int], e: Expr): Int = e match {
	case Constant(v) => v
	case Add(l, r) => eval(env, l) + eval(env, r)
	case Mul(l, r) => eval(env, l) * eval(env, r)
	case Var(n) => env(n)
}
def format(e: Expr): String = e match {
	case Constant(v) => v.toString
	case Add(l, r) => format(l) + "+" + format(r)
	case Mul(l, r) => format(l) + "*" + format(r)
	case Var(n) => n
}
println(format(testExpr))
println(eval(Map("x" -> 7), testExpr))
Topic 5:
a)
- less biolerplate code
- more concise
- clear programming intention

b)
1) Temporary spurios values due to propagation order.
2) Because fields can be updated with false values and events can be fired erroneously.
3)

Code: Alles auswählen

       (e)
        |
       (d)
      /  \
   (b)  (c)
      \  /
      (a)
With Glitch: abdc
Without Glitch: abcd

c)

Code: Alles auswählen

val e1 = Evt[Int]()
val e2 = Evt[Unit]()
val s1 = e1.last(10)
val s2 = Signal(s1().sum)
val s3 = e2.snapshot(s1)
val s4 = e2.count

Sakaze
Neuling
Neuling
Beiträge: 9
Registriert: 26. Apr 2011 13:02

Re: discussion solution last years exam

Beitrag von Sakaze »

tz33quvu hat geschrieben:Hi
I think in topic 5 s2 should be like val s2:Signal[Int]=s1.fold(0)((x:Int,y:Int)=>(x+y)) regarding to the ppt
Is it possible to execute fold on a signal instead of an event?
Do can someone proof ift this ist working?

This is my approach.
s2:Signal[Int]=s1.changed.fold(0)((x:Int,y:Int)=>(x+y))

Are the old values which are removed from the seq still in the sum?

pschulz
Neuling
Neuling
Beiträge: 9
Registriert: 7. Nov 2016 18:15

Re: discussion solution last years exam

Beitrag von pschulz »

Code: Alles auswählen

val e1 = Evt[Int]()
val s1:Signal[Seq[Int]] = e1.last(10)
val s2 = Signal { s1().sum }
val s2 = Signal {s1().fold(0)((x,y) => x + y)}

e1(1)
e1(2)
e1(3)
println(s2.now)
e1(2)
println(s2.now)
e1(77)
println(s2.now)

both versions of s2 compile just fine and both print :
6
8
85

nc18cyje
Neuling
Neuling
Beiträge: 4
Registriert: 15. Apr 2016 10:07

Re: discussion solution last years exam

Beitrag von nc18cyje »

Can someone pls explain Topic 3a ?
How and when actually the type check is done?

pschulz
Neuling
Neuling
Beiträge: 9
Registriert: 7. Nov 2016 18:15

Re: discussion solution last years exam

Beitrag von pschulz »

Regarding Topic 3a)

All of them are type checked in the moment when they are assigned. The description maybe confused you a bit...
The question in general is: Which of those assignments are valid assignments and which of those not because the compiler will reject them.

So basically what u want to understand here is this line:

Code: Alles auswählen

val occupied = scala.collection.mutable .Set[this.Coordinate ]()
This means that occupied requieres Coordinate to be of type THIS -> Coordinate.
"this" means "this object", right?

So if you have:

Code: Alles auswählen

val b1:Board = Board(10,10)
val c:Coordinate = b1.Coordinate(10,10)

val b2:Board = Board(10,10)
b2.occupied += c 
<- will fail because c is of type b1.Coordinate but b2.occupied requieres Coordinates of type b2.Coordinate.

You could say: "the object b2 requieres Coordinates which are generated by itself" ( c is generated by b1 so b2 won't accept c, since occupied requieres a Set of this.Coordinate

You can also look at the Animal,Cow,Food example in the slides.

majidazimi
Erstie
Erstie
Beiträge: 17
Registriert: 16. Jul 2016 12:25

Re: discussion solution last years exam

Beitrag von majidazimi »

1)
- Fields, declared outside and referenced inside of Java Lambdas need to be set final.
- Return statement must be explicitely defined in Java. (This does not directly relate to closures in Java, but I had no other second answer...)
2) Declare method final.
3) Bridge pattern
Regarding number (1). Better answer for second reason would be:
in scala you can actually call the lambda as a normal function like f(x) but in java lambda is actually an object (Function<Integer, Integer> in this example) and you can't call it directly like f(x). You can only call it like: f.apply(x) which is less intuitive than f(x).

Regarding number (3), I think it really looks like adapter than bridge. because it says IntPair is part of a library and probably we don't have a chance to modify it.

nc18cyje
Neuling
Neuling
Beiträge: 4
Registriert: 15. Apr 2016 10:07

Re: discussion solution last years exam

Beitrag von nc18cyje »

How exactly the topic 2d , to design template and strategy pattern is done?
can someone pls throw some light on this
Zuletzt geändert von nc18cyje am 13. Feb 2017 21:51, insgesamt 1-mal geändert.

Antworten

Zurück zu „Archiv“