arrays not equal
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
Moreover the equals method just checks for the array references - whether they refer to the same object. So when you create two different objects for the WrapperDeep class it is going to return false
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
**** TESTING SHALLOW OBJECTS ****
inital shallow object contains
7 17 77
copy shallow object contains
7 17 77
inital shallow object changed to
13 14 15
copy shallow object not changed contains
13 14 15
WOOPS! ws.equals(ws2) is true
**** TESTING DEEP OBJECTS ****
inital deep object contains
2 3 4
copy deep object contains
2 3 4
inital deep object changed to
7 6 -5
copy deep object not changed contains
2 3 4
RIGHT! wd.equals(wd2) is false
Process completed.
or something like that. I don't understand how to implement the copy constructors in the assignment. Thank you for helping or hints.
Edit: I figured out my test for equality of the arrays. using Arrays.equals with java.util.Arrays import. We haven't learned it in class yet but a little independent research shouldn't be counted against I hope
Still don't understand how to implement the copy constructors in this assignment though 
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Look at the below constructor code. It gets another WrapperShallow reference as its input.
So first step is to create two Wrapper Shallow objects
After you have created two references, then the next step is to change the array value of one of them like below.
Now check the values using the equals method. Combining all it should look like below.
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
but for some reason even though object wd prints out the same as wd2, the equals method still says they are not equal
Because, as John pointed out, both your constructors create a new array.
Off-topic, and at the risk of starting a heated discussion, I have a definite preference for using instanceof in equals() methods, viz (pattern):A few advantages are:
1. You don't need to check for null.
2. It generally works as you want in a class hierarchy; that is, it will also work if obj is a subclass of ThisClass.
3. It saves a call to getClass(), which (at least on my machine) is significantly heavier than instanceof. Now before anyone starts quoting Donald Knuth to me, I state this only because in most cases the call is unnecessary.
Obviously if the method should not work with a subclass of ThisClass, you should use the style you did; but I've rarely found it to be an issue.
Winston
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
As far as using instanceof in equals methods, we haven't learned that in class yet so I don't want to get too far ahead if what I am doing is working the way we are taught. Once we have learned about instanceof though, I will likely use it if it is easier and uses less memory. Thank you
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
The only issue I have though is that if I comment out the wd.setArray() method, the equals method says they are not equal even though the values are the same. I guess it might be intended that way since this is a comparison of deep and shallow object copies, not the equality of values.
Exactly right. Comparing arrays for content is a different thing from checking to see whether they are the same array.
Array.equals is better for checking the equality of arrays, am I right?
No. Array is a non-instantiable class (at least for you), so you can't call its equals() method.
Arrays.equals(), on the other hand, is precisely for comparing two arrays based on their content.
As far as using instanceof in equals methods, we haven't learned that in class yet so I don't want to get too far ahead if what I am doing is working the way we are taught.
Oops, my apologies then. It may be that the style you use is taught because it's considered "safer", but that's a discussion for a few months down the road
. Winston
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Michael D Watson wrote:I would like to have this thread (arrays not equal) deleted please. My professor uses this question every semester...
I fear the thread will not be deleted. You might kindly ask your professor to change the questionnaire... Anyways its up to the moderator of the forum...
| This tiny ad will self destruct in five seconds. Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |











