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 Interfaces

edumorlom
edumorlom
4,073 Points

It says Task 1 is no longer passing for this exercise.

I don't see why Task 1 is no longer passing. Task one simply says to return 1 as default. Task 2 says to compare obj to obj2, if they are equal, return 0.

That's what I did. Someone please help.

  @Override
  public int compareTo ( Object obj ) {
    BlogPost obj2 = (BlogPost) obj;
    if (obj.equals(obj2)){
      return 0;}
    return 1;
  }

Thank you!

edumorlom
edumorlom
4,073 Points

my Task 1 code is:

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

2 Answers

Watch the video again at 9:30 and copy it. The answers are almost always in the video :)

  @Override
  public int compareTo(Object obj ) {
    BlogPost post = (BlogPost) obj;
    if (equals(post)) { //<-----------changed this to match the video
      return 0;
    }
    return 1;
  }
John Paige
John Paige
7,436 Points

Hey, Philip. I used your answer and passed the challenge again (reviewing the whole course so i can get past Data Structures).

The one thing i don't quite understand the why, is using return 1 in task one, and then taking that line back out. It seemed counter-intuitive to return 0 within the if code block and then return 1 right after.

I'd appreciate your input!

Since method must have return value and compareTo() method only returns 0, 1, -1. So initial return value set to 1 is just a place holder. That's how I believe.

When the obj1 is equal to obj2, then return value is set to 0 and the next return 1 line will not be executed.