View Full Version : Object equality testing
Silver Fox
04-01-2008, 08:20 PM
I've been using Alice for two years now and this is the first time this issue has arisen: Is there a built-in way to test whether two objects are the same? What I'd like to do is something like
if (thisObject == thatObject)
//...
But obviously that won't work. I came up with about half a dozen workarounds, but they're all inelegant. Am I just missing something, or is there really no simple way in Alice to test objects for equality?
Tell me I'm missing something simple. Please.
lanceA
04-01-2008, 09:46 PM
Hopefully, I'm the person that is missing something but, I do not believe you can compare two objects in Alice in the strictest terms of Java. I've never come across a class in Alice that implements the comparable interface. Neither, to the best of my knowledge, does Alice implement the instanceOf class. (I haven't tried using the double equals to determine if two objects are pointing to the same reference.)
Instead, I believe Alice was made available to help introduce students to the basics of OOP programming, as stated in the opening sentence of the Forward of the book by Dann, et. al. The operative word there was, basics. From my limited experience, Alice does not strictly conform to OOP programming concepts.
I use Alice as an introduction to basic OOP programming for a group of 9th grade students. In later years, i.e., 10th grade when the students are actually programming in Java, they often blurt out in class, "Oh. this is like the Array-thing we did in Alice. Now I get it!"
That's just my 2¢, but I do not use Alice to teach a complete course on OOP programming; just an introduction to OOP programming concepts. (However, version 3.0 may change some of my thinking!)
I hope this helps and I'm sure others can provide more detailed information.
DickBaldwin
04-01-2008, 11:37 PM
NOTE: I originally uploaded the wrong file. I couldn't figure out any way to replace it with the correct file without repeating the entire posting.
Object equality or the lack thereof in Java
The == operator works the same way in Alice 2.0 as it does in Java when it is applied to objects. If you apply the == operator to two object references in Java, it will return true only if both references point to the same physical object. It will return false if the two references point to two different objects instantiated from the same class even if there is no apparent difference between the two objects.
This can be verified with the following Java code:
class test{
public static void main(String[] args){
test var1 = new test();
test var2 = var1;
test var3 = new test();
System.out.println(var1 == var1);
System.out.println(var1 == var2);
System.out.println(var1 == var3);
}//end main
}//end class
This Java program produces the following output:
true
true
false
If you want to determine if two Java objects "are the same," you must override the equals method that is inherited from the Object class when you define the class from which the two objects are instantiated. The author of the class that overrides the equals method can decides what constitutes "the same" and what does not constitute "the same." Thus, that author could conceivably cause the equals method to return true when there are major differences between the objects, could require that the two objects be absolutely equal down to the last digit in all float and double values, or could strike a balance somewhere in between these two extremes.
Overriding the equals method in Java can be a significant challenge if the class contains references to objects of other classes and those classes also contain references to objects of still other classes. Often when overriding the equals method, you aren't simply testing to determine if two objects are equal, you are testing two hierarchical trees of objects to determine if every object in one tree is equal to every object in the other tree. (This can be almost as difficult as overriding the clone method.) Once again, the author of the class that overrides the equals method has a lot of latitude in deciding what constitutes equal.
Therefore, there is no "simple way" to test two objects for equality in Java either, unless you are using a class, such as the String class, for which someone else has already done all the hard work of overriding the equals method. Although I wouldn't bet my life on it, I'm reasonably confident that the same is also true in C++ and C#.
What about object equality in Alice?
The attached world shows that if you apply the == operator to two objects in Alice, it will return true if the two objects are in fact the same object, and will return false if they are different objects of the same type with no apparent differences between them. Again, this mimics the behavior of the application of the == operator to two object references in Java. Unfortunately, as far as I know, there is no way to override an equals method in Alice 2.0, so there is apparently no way to test two Alice objects for equality, simple or otherwise.
Alice 3.0 should rectify this shortcoming, since the CMU position is that we will be able to do anything with Alice 3.0 that we can do with Java, and more.
Although I approach the topic from a different viewpoint, I have more to say about the application of the == operator to objects and the lack of an instanceof operator in Alice 2.0 at http://www.dickbaldwin.com/alice/Alice0930.htm
Dick Baldwin
Free Alice tutorials: http://www.dickbaldwin.com/tocalice.htm
Free programming tutorials: http://www.dickbaldwin.com/toc.htm
Silver Fox
04-02-2008, 07:10 AM
Hah! So my first, Java-inspired, idea was right after all. It's funny how the mind works. When I tried to construct
if (object1 == object2)
...
I began the way I usually do, namely dragging the "==" tile into the condition box of the if statement. That didn't work, though, since I couldn't drag any object tiles into the left or right arguments of the "==". After seeing that it was possible (thanks, Dick), I found that the way to do it was to first drag the object tile into the condition box and then selecting the drop-down menu item "object1 ==".
Meta-moral: a previously impossible problem suddenly becomes possible when you are shown that there indeed is a solution.
Thanks, all.
DickBaldwin
04-02-2008, 09:34 AM
Hah! So my first, Java-inspired, idea was right after all. It's funny how the mind works. When I tried to construct
if (object1 == object2)
...
I began the way I usually do, namely dragging the "==" tile into the condition box of the if statement. That didn't work, though, since I couldn't drag any object tiles into the left or right arguments of the "==". After seeing that it was possible (thanks, Dick), I found that the way to do it was to first drag the object tile into the condition box and then selecting the drop-down menu item "object1 ==".
Meta-moral: a previously impossible problem suddenly becomes possible when you are shown that there indeed is a solution.
Thanks, all.
I agree that it is sometimes difficult to determine the correct sequence of drag-and-drop steps to cause Alice 2.0 to produce the statement that you need.
Since you are happy:D, I assume that your need was to test to determine if two objects were in fact the same object and was not to determine if two different objects were "equal" to one another.
Dick Baldwin
Free Alice tutorials: http://www.dickbaldwin.com/tocalice.htm
Free programming tutorials: http://www.dickbaldwin.com/toc.htm