Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Organizing Data Comparable

Not seeing my error even though I understand what the error is, need help.

I have tried everything I still don't know why I am still getting an error. I tried adding the @Override property and running the code without it but i dont seem to understand what's going wrong

6 Answers

Kevin Faust
Kevin Faust
15,353 Points

Whoops sorry Chevano,

I deleted the part i wrote above and ill rewrite it here. I misread it for some reason. We can use the getter or directly referencing to the instance as you did. since we are within the class we can reference to the private instance variable. however it is good practice to use the getter as we can use that method inside or outside the code with no worry.

All good?

Kevin

Kevin Faust
Kevin Faust
15,353 Points

Hey Chevano,

No problem. I looked over what you wrote and I see what you did wrong.

Object myObject = (Object) obj

This line doesn't make sense. Do you know why? When we pass in an object in our compareTo method, it will be upcasted to an Object. However what our method is doing is comparing two BlogPosts. Therefore, we need to downcast the object we passed in (the obj) to a BlogPost object.

BlogPost myObject = (BlogPost) obj

What I did there was downcast the obj to a BlogPost object and then stored it in a myObject variable. It is now a BlogPost object and we can now use our method.

The rest of your code works perfectly fine as I tested it out just now.

I hoped that fixed your problem and dont forget to mark as best answer so others with the same question can see

Happy coding and best of luck,

Kevin

Kevin Faust
Kevin Faust
15,353 Points

Hey Chevano,

Since I dont know what you are having trouble with specifically, let's just start from the beginning.

So first we just add the implements comparable interface to our class. Simple enough. Then we add the compareTo() method:

public int compareTo(Object obj) {
   return 1; 
  }

What part 2 wants is that when an object is passed in, you have to downcast it to a BlogPost and then check if the current BlogPost is equal to the one we passed in. We do this by doing the following:

    BlogPost bp = (BlogPost) obj;
    if (equals(bp)) {
     return 0; 
    }

As we see, it downcasts the obj to a BlogPost and stores it as bp. Then we check if the current BlogPost that is calling the method is equal to the BlogPost we passed in. If they are the same, 0 is returned and they remain in the same order.

For part 3. we just compare their dates by doing the following:

return getCreationDate().compareTo(bp.getCreationDate());

this compares both their creation dates and java will automatically sort them out in order. I wouldnt worry too much about it. Java has its own algorithm which is beyond our scope. Also if type casting is still sort of confusing as it was for me at first, once you learn generics you wont even use them anymore to be honest.

Anyways, if you have any further questions let me know.

Happy coding and best of luck,

Kevin

Hi Kevin,

I apologize I never knew that my code wasn't posted I had problem with the last task. I had the following;

public int compareTo(Object obj) { Object myObject = (Object) obj; if(equals(myObject)) { return 0; } return mCreationDate.compareTo(myObject.mCreationDate); }

The error was coming from the last line. I haven't read your comments in fully as yet cause i read that you never understood where the confusion came from so I just wanted to make that clear before reading your solution.

Hi Kevin,

I got the code to ran, thanks a lot.

Hi Kevin,

Not to disturb you are anything but mCreationDate was declared as a private member variable so I am not following you when you said its not private please show me what I am not seeing. And I just wanted to note that I understood everything you just mentioned except for that concept.