alice-teacher Setters for a List

Don Slater dslater at andrew.cmu.edu
Wed Apr 13 11:55:39 EDT 2011


Paul,
I think the first thing you want to do is make sure that you are creating an array and not a list of items. See attached "ScreenShot1". The list structure in Alice 2 does not easily support setting the position in the list to a different value. Rather you end up inserting at the position, and having to remove the element you want replaced. Let me know if you would like more explanation.

Then using your example code as a guide (I am using an array of bugs, rather than an array of ints):

	int n = list.get(position2);

	See "ScreenShot2"
		create the variable
		drag the variable into the code editor
		Drag the tile for the array for the value to be assigned to the variable 
		you will be asked which index value you wish to use (in the case of your example, 2)

 	list.set(position2, list.get(position1));

	See "ScreenShot3"
		Drag the tile for the array into the code editor
		Select the menu option "set item <index> to <item> in <array name>"
			use the appropriate index value for <index>, and select a placeholder object for <item>
		Drag the tile for the array to replace the placeholder
		you will be asked which index value you wish to use (in the case of your example, 1)

	list.set(position1, n);

	See "ScreenShot4"
		Drag the tile for the array into the code editor
		Select the menu option "set item <index> to <item> in <array name>"
			use the appropriate index value for <index>, and select the variable for <item>

I have also attached the .a2w file for you to play with. The worlds includes a loop before and after the swap to show how the order of the bugs in the array has changed.
		
Let me know if you have any questions.

All the best,
Don Slater



On Apr 12, 2011, at 1:33 PM, Paul Flaherty wrote:

> I'm not sure what happened, but at some point between April 2010 and April 2011, I lost track of how to set the value of individual items in a list in Alice.
>  
> Last year, I did a lesson to teach my HS Intro students the basics of how to sort. We made a list in Alice, populated it with integers, wrote a swap method to swap to values in an array, and Bubble Sorted it in place.
>  
> This year, I'm trying to do it again, but I can't figure out how to get Alice to let me change the value of an individual item in a list The equivalent, with a Java ArrayList, would be:
>  
> int n = list.get(position2);
> list.set(position2, list.get(position1));
> list.set(position1, n);
>  
> I can get the getters to work, but I can't figure out where the setters went. Was this functionality removed?
>  
> In the meantime, I'll teach my kids Selection Sort instead, and I guess we'll sort in-place in Java. The "watch this variable" function of Alice is awesome for showing students how the sort works, though.
>  
> Thanks for the help,
>  
> Paul Flaherty
> Mathematics | Computer Science
> Westwood Regional Jr./Sr. High School _______________________________________________
> 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: https://lists.andrew.cmu.edu/mailman/private/alice-teachers/attachments/20110413/995ae29b/attachment-0006.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bugArray.a2w
Type: application/octet-stream
Size: 692968 bytes
Desc: not available
Url : https://lists.andrew.cmu.edu/mailman/private/alice-teachers/attachments/20110413/995ae29b/attachment-0001.obj 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://lists.andrew.cmu.edu/mailman/private/alice-teachers/attachments/20110413/995ae29b/attachment-0007.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ScreenShot1.png
Type: image/png
Size: 70526 bytes
Desc: not available
Url : https://lists.andrew.cmu.edu/mailman/private/alice-teachers/attachments/20110413/995ae29b/attachment-0004.png 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://lists.andrew.cmu.edu/mailman/private/alice-teachers/attachments/20110413/995ae29b/attachment-0008.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ScreenShot2.png
Type: image/png
Size: 54894 bytes
Desc: not available
Url : https://lists.andrew.cmu.edu/mailman/private/alice-teachers/attachments/20110413/995ae29b/attachment-0005.png 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://lists.andrew.cmu.edu/mailman/private/alice-teachers/attachments/20110413/995ae29b/attachment-0009.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ScreenShot3.png
Type: image/png
Size: 73809 bytes
Desc: not available
Url : https://lists.andrew.cmu.edu/mailman/private/alice-teachers/attachments/20110413/995ae29b/attachment-0006.png 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://lists.andrew.cmu.edu/mailman/private/alice-teachers/attachments/20110413/995ae29b/attachment-0010.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ScreenShot4.png
Type: image/png
Size: 104646 bytes
Desc: not available
Url : https://lists.andrew.cmu.edu/mailman/private/alice-teachers/attachments/20110413/995ae29b/attachment-0007.png 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://lists.andrew.cmu.edu/mailman/private/alice-teachers/attachments/20110413/995ae29b/attachment-0011.html 


More information about the alice-teachers mailing list