alice-teacher Passing an object to a method doesn't appear to work completely in Alice 2.3.2

Don Slater dslater at andrew.cmu.edu
Mon Apr 15 14:18:48 EDT 2013


I asked Marty to send me a copy of his code… This is my reply him, but it demonstrates a problem using DoTogether which happens frequently, and is very difficult to overlook and track down.

See the attached screen shot, from the method "world.KeepScore":

The code of concern is this doTogether, in which you are (1) setting the value of the variable 'animalThatWasHit',  (2) printing that value, and (3) sending that value as a parameter, AT THE SAME TIME.

A do together generates separate, independent threads for each statement in the doTogether body, and so what is happening in one statement may or may not, depending on the timing, have any impact on the other threads, particularly if they are trying to use the same variable.

So it is not so much that the value reverts to paintball, but that you print as the value is being set. If you had printed it again, after the doTogether, you would have seen the correct result.

I dissolved the doTogether and got the following output after running the program:

	tiger
	none
	tiger
	none
	tiger

I hope that this helps… 

This is a very difficult bug to track down, one that even experienced Alice programmers. will make. It is easy to overlook (you did not even think to include the doTogether's in your pseudocode, and yet they are critical) and not think about.

Look at how different your pseudocode looks if you had written it this way

> Method world.keepScore looks similar to:
		DoTogether
>                 // precondition: Local variable animalThatWasHit (type=OBJECT, initial value set to paintball)
>  
>                 20 animalThatWasHit set value to world.whoGotHit
>                 20 print animalThatWasHit
>                 20 world.whenHit target=animalThatWasHit


All the best,
Don Slater




On Apr 14, 2013, at 9:48 PM, Marty Schultz <schultz_marty at hotmail.com> wrote:

> Does anyone have a solution for this (I’ve included pseudo line numbers to make it clear):
>  
> Function world.whoGotHit (returns type OBJECT) ends with:
>                 10 print “world.whoGotHit”
>                 11 print tiger.name
>                 12 return tiger
>  
>  
> Method world.keepScore looks similar to:
>                 20 Local variable animalThatWasHit (type=OBJECT, initial value set to paintball)
>  
>                 22 animalThatWasHit set value to world.whoGotHit
>                 23 print animalThatWasHit
>                 24 world.whenHit target=animalThatWasHit
>  
> Method world.whenHit
>                 30 parameter target (type=OBJECT)
>                 32 print “world.whenHit”
>                 33 print target
>  
>  
> When I PLAY this program, the PRINT statements show:
>                 world.whoGotHit (from line number 10)
>                 tiger (from line number 11)
>                 the value of world.keepScore.animalThatWasHit is tiger (from line 23 – this is expected)
>                 world.whenHit (from line 32)
>                 the value of world.whenHit.target is paintball (from line 33- this seems WRONG – it should be “tiger”)
>  
> What appears to be happening is that even though the value of animalThatWasHit is “tiger” in the world.keepScore method, when the world.whenHit method is called, the value reverts back to the initial value, not the value shown in the parent (world.keepScore) method.
>  
>  
>  
>                
> _______________________________________________
> alice-teachers mailing list
> alice-teachers at lists.andrew.cmu.edu
> https://lists.andrew.cmu.edu/mailman/listinfo/alice-teachers

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.andrew.cmu.edu/pipermail/alice-teachers/attachments/20130415/4e33fdbb/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: codeOfConcern.png
Type: image/png
Size: 53234 bytes
Desc: not available
Url : http://lists.andrew.cmu.edu/pipermail/alice-teachers/attachments/20130415/4e33fdbb/attachment.png 


More information about the alice-teachers mailing list