Faculty of Engineering and Mathematical Sciences 
Not logged in (login)

help2200


This forum is provided to promote discussion amongst students enrolled in Data Structures and Algorithms (CITS2200).
 
Options:
RSS cloud
Jump to:

Lab 5 - equals(Object o)

9 of 251 articles shown, currently no other people reading this forum.
photo
From: Thomas C.
Date: Sat 4th Apr, 1:33pm
Actions: 
        Login-to-reply
With the equals method after checking if the object o is an instance of BinaryTree I tried 
to cast o down to a BinTree object so I can call methods like getRight() or isEmpty() on it 
to compare to the given BinTree.

This works with a test class I have written and I get the correct boolean value back when 
comparing similar trees, empty trees or a tree with an object that is not a binary tree.

However when I submit the file I get a ClassCastException as seen in the image attached.

Can anyone explain how we are meant to convert Object o to something we can compare against 
the BinTree?

Any help would be greatly appreciated!

Lab 5 - equals(Object o)

photo
From: Thomas C.  O.P.
Date: Sat 4th Apr, 1:41pm
Actions: 
        Login-to-reply
I wasn't checking what o was an instance of correctly before trying to cast. All good now. 

Lab 5 - equals(Object o)

photo
From: ANONYMOUS
Date: Tue 7th Apr, 10:40am
Summary: confused
Actions: 
        Login-to-reply
I'm confused as to why you need to check if "o" is an instance of BinaryTree. Isn't o the item 
that's stored in the root of BinTree? Why would you need to cast the object variable as 
BinaryTree?

Lab 5 - equals(Object o)

photo
From: Thomas C.  O.P.
Date: Tue 7th Apr, 10:54am
Actions: 
        Login-to-reply
The equals method we need to implement has 
argument o of type Object. If its anything 
other than a binary tree the method should 
return false. So to call Binary Tree methods 
on o it needs to be cast to a BinaryTree so we 
can check if it has the same structure as the 
tree you call equals() from.

Lab 5 - equals(Object o)

photo
From: ANONYMOUS
Date: Tue 7th Apr, 11:13am
Actions: 
        Login-to-reply
> The equals method we need to implement has 
> argument o of type Object. If its anything 
> other than a binary tree the method should 
> return false. So to call Binary Tree methods 
> on o it needs to be cast to a BinaryTree so we 
> can check if it has the same structure as the 
> tree you call equals() from.

Ah, that makes sense. I forgot o was a parameter. Well, I'm still a bit confused about 
BinTree. BinTree is supposed to be a subclass of BinaryTree? I'm not sure what that's 
supposed to mean. Is it supposed to only have the methods equals and iterator?

Lab 5 - equals(Object o)

photo
From: Thomas C.  O.P.
Date: Tue 7th Apr, 12:21pm
Actions: 
        Login-to-reply
BinaryTree is an abstract class from the CITS2200 package. Abstract classes are sort of like 
interfaces in that you have to implement their abstract methods. So BinTree extends BinaryTree by 
implementing equals() and iterator().

Lab 5 - equals(Object o)

photo
From: ANONYMOUS
Date: Thu 9th Apr, 12:52pm
Actions: 
        Login-to-reply
I have tested my equals method with two empty BinTrees, one empty and one populated BinTree, many different 
populated BinTrees against each other, and BinTrees against objects that aren't BinTrees, such as strings. I 
tested b1.equals(b2) and b2.equals(b1) for each case. My test program gives me the output I expect for each 
case, but cssubmit tells me my equals method produced some wrong output. May I please have some suggestions 
for how else could I test the method? Thanks

Lab 5 - equals(Object o)

photo
From: Thomas C.  O.P.
Date: Thu 9th Apr, 5:01pm
Actions: 
        Login-to-reply
Think about what kind of tree the testing program might be using to compare against your BinTree. Does your instanceof 
test handle that properly? Maybe you shouldn't only be testing for an instance of your specific implementation of 
BinaryTree<E>?

Lab 5 - equals(Object o)

photo
From: ANONYMOUS
Date: Thu 9th Apr, 8:07pm
Actions: 
        Login-to-reply
Yes sorry, I am testing for o instanceof BinaryTree, and casting o to the type BinaryTree<E>. I don't ever reference BinTree in the 
equals(Object) code, that was a typing error sorry. I still can't find out why the method is failing cssubmit, but giving me all the 
expected outputs for BinaryTree comparisons. Or have I misinterpreted you?
This Page


Program written by: [email protected]
Feedback welcome
Last modified:  8:27am May 24 2020