Exercise 1.1 and 1.2

burgi
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 115
Registriert: 15. Apr 2009 18:08
Wohnort: Ludwigshafen

Exercise 1.1 and 1.2

Beitrag von burgi »

Hi everybody,

there are two questions that arose for me during implementation of subtasks 1.1 and 1.2.

First, for the implementation of method incl(x: Int):IntSet, which IntSet should be returned ? In detail, I implemented the binary tree structure as extend to IntSet, thus every Node is an Intset. Now which IntSet (Node) should be returned for the incl method, the root node or the newly generated Node ?

Second, concerning the implementation of the foreach and toString method respectively. Is it sufficient to print all the integer values contained in the set, or do they have to be in a ascending / decending order ?

Greets Dominic

Benutzeravatar
mmi1991
Computerversteher
Computerversteher
Beiträge: 349
Registriert: 20. Okt 2011 18:46
Wohnort: Hattersheim

ToString with sideeffects?

Beitrag von mmi1991 »

Two additional questions:
Override method toString … The method should print the elements of the set…
1. It should print the elements and therefore have sideffects?
2. There is a nice approach to implement filter without the need to implement it in the subclasses of IntSet. Should we really implement filter in the subclasses, even if a nice and short approach (5 Lines of Code) exists?
Zuletzt geändert von mmi1991 am 31. Okt 2013 15:19, insgesamt 2-mal geändert.
Ophasentutor SoSe 2014, WiSe 2015/16
Alle Angaben wie immer ohne Gewähr

burgi
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 115
Registriert: 15. Apr 2009 18:08
Wohnort: Ludwigshafen

Re: Exercise 1.1 and 1.2

Beitrag von burgi »

Thats a good additional question, to precises it from my point of view. Should toString print to the console or should it return a String , as toString implicates.

Dennis Albrecht
Sonntagsinformatiker
Sonntagsinformatiker
Beiträge: 222
Registriert: 4. Okt 2010 18:15

Re: Exercise 1.1 and 1.2

Beitrag von Dennis Albrecht »

Hey guys!
burgi hat geschrieben:First, for the implementation of method incl(x: Int):IntSet, which IntSet should be returned ? In detail, I implemented the binary tree structure as extend to IntSet, thus every Node is an Intset. Now which IntSet (Node) should be returned for the incl method, the root node or the newly generated Node ?
It should return the root node of the newly created Tree.
burgi hat geschrieben:Second, concerning the implementation of the foreach and toString method respectively. Is it sufficient to print all the integer values contained in the set, or do they have to be in a ascending / decending order ?
In general Sets are unordered collections, with order (normally) meaning the order of insertion. But to produce unique Strings for different Sets holding the same elements, I would recommend to iterate over the Nodes in some kind of (in-)order (which would produce an ascending order of the elements).
mmi1991 hat geschrieben:1. It should print the elements and therefore have sideffects?
No, it should return the string.
mmi1991 hat geschrieben:2. There is a nice approach to implement filter without the need to implement it in the subclasses of IntSet. Should we really implement filter in the subclasses, even if a nice and short approach (5 Lines of Code) exists?
I would guess, you can find some even shorter solutions if you implement filter in the subclasses. But nevertheless the task asks for implementations in the subclasses.
burgi hat geschrieben:Thats a good additional question, to precises it from my point of view. Should toString print to the console or should it return a String , as toString implicates.
Should be answered, too.

Greets Dennis

imaier
Mausschubser
Mausschubser
Beiträge: 61
Registriert: 21. Okt 2013 21:27

Re: ToString with sideeffects?

Beitrag von imaier »

mmi1991 hat geschrieben: 2. There is a nice approach to implement filter without the need to implement it in the subclasses of IntSet. Should we really implement filter in the subclasses, even if a nice and short approach (5 Lines of Code) exists?
If you can find a concise solution that
  • does not take more than O(n) time to build (where n is the number of elements in the set), and
  • does not pattern match, and
  • is the same for all node classes
then you can of course implement it in the base class. The reason why we asked that you should implement the given methods in the subclasses is to make sure you rely on dynamic dispatch and not pattern matching for case distinction of your different node classes. The same is true for any helper methods you need.

There is an inherent tradeoff between dynamic dispatch and pattern matching. Putting operations in methods (instead of external functions as in Ex 1.5) works better with dispatch than pattern matching. That might be a nice thing to discuss in the next exercise session.

Please also note that you should *not* use a technique as in the second exercise to implement filter (or the other methods). Specifically, the filter implementation in 1.2 should apply the given predicate n times while building a new set and then forget the predicate, i.e., never apply it again.

Cheers,
Ingo

imaier
Mausschubser
Mausschubser
Beiträge: 61
Registriert: 21. Okt 2013 21:27

Re: Exercise 1.1 and 1.2

Beitrag von imaier »

Concerning the implementation of toString, Dennis is right when he says that toString should print the elements *to the string* (aka build a string that contains the elements) and not to the console. Overriding toString and then print the elements to System.out wouldn't be a reasonable thing to do except for temporary debugging purposes. Sorry for the confusion, I'll change the exercise sheet to clarify that point. The order of elements is not important (it is a set after all).

Cheers,
Ingo

Antworten

Zurück zu „Archiv“