Comments on Week of 21 Feb 2005
What do you think?
- Of lists and trees?
- Vs. arrays?
- REMOVEDsts and trees of pictures, vs. lists and trees of sounds?
- About repeating and weaving in different media?
REMOVEDke it? Hate it? Wish it were faster? Wish it were easier?
Let us have it!
| As promised, reverse() for LayeredSceneElement repaired – the fix we did in class worked. |
/**
* Reverse the list starting at this,
* and return the last element of the list.
* The last element becomes the FIRST element
* of the list, and THIS goes to null.
**/
public LayeredSceneElement reverse() {
LayeredSceneElement reversed, temp;
// Handle the first node outside the loop
reversed = this.last();
this.remove(reversed);
while (this.getNext() != null)
{
temp = this.last();
this.remove(temp);
reversed.add(temp);
};
// Now put the head of the old list on the end of
// the reversed list.
reversed.add(this);
// At this point, reversed
// is the head of the list
return reversed;
}
public class TestLayeredSceneElement{
public static void main(String [] args){
FileChooser.setMediaPath("D:/cs1316/mediasources/");
Picture bg = new Picture(400,400);
Picture bg2 = new Picture(400,400);
LayeredSceneElement dog1 = new LayeredSceneElement(
new Picture(FileChooser.getMediaPath("dog-blue.jpg")),10,5);
LayeredSceneElement tree2 = new LayeredSceneElement(
new Picture(FileChooser.getMediaPath("tree-blue.jpg")),10,10);
LayeredSceneElement house = new LayeredSceneElement(
new Picture(FileChooser.getMediaPath("house-blue.jpg")),10,10);
dog1.setNext(tree2); tree2.setNext(house);
dog1.drawFromMeOn(bg);
bg.show(); // So, old one should be on bottom -- dog on bottom
LayeredSceneElement rev = dog1.reverse();
rev.drawFromMeOn(bg2);
bg2.show(); // new one should have dog on top
}
}
HW5 turned out to be awesome. it took a long time to figure out but i understand linked lists so much more. we should probably have better pragmatic experience with the declarations the define a class and how to use them in methods the right way. it took me the most time to figure out that mess.
| That's great to hear, and very useful advice to me. THANKS! We'll work more on declarations that define a class and that define a method. Mark Guzdial |
Link to this Page