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 Organizing Data Comparable

task 1 is not responding

task 1 not responding

com/example/BlogPost.java
package com.example;

import java.util.Date;

public class BlogPost implements Comparable {
  private String mAuthor;
  private String mTitle;
  private String mBody;
  private String mCategory;
  private Date mCreationDate;
  @Override
  public int compareTo(Object obj){
    BlogPost blogPost=(BlogPost)obj;
    if(blogPost.equals(obj)){
     return 0; 
    }
   return 1; 
  }

  public BlogPost(String author, String title, String body, String category, Date creationDate) {
    mAuthor = author;
    mTitle = title;
    mBody = body;
    mCategory = category;
    mCreationDate = creationDate;
  }

  public String[] getWords() {
    return mBody.split("\\s+");
  }

  public String getAuthor() {
    return mAuthor;
  }

  public String getTitle() {
    return mTitle;
  }

  public String getBody() {
    return mBody;
  }

  public String getCategory() {
    return mCategory;
  }

  public Date getCreationDate() {
    return mCreationDate;
  }
}

4 Answers

Hi there,

I think you want to compare the instance we're operating inside to the variable you've called blogPost.

You can use just equals(blogPost) for this but that does lack a little clarity. Adding the keyword this helps with clarity.

So, in your if statement, see if(this.equals(blogPost)). The rest of your code looks OK, but I don't think you need the @Override.

Steve.

Kill me Steve :) Always ready for your extensive and in-depth tips. :)

  @Override
  public int compareTo(Object obj){
    BlogPost blogPost = (BlogPost) obj;
    if (this.equals(blogPost)) {
      return 0;
    }
    int creationDate = mCreationDate.compareTo(blogPost.mCreationDate);
    if (creationDate > 0) {
      return 1;
    } else return -1;
  }

:+1:

I don't think I changed any of your code (bar the bp name) and got this to pass:

  public int compareTo(Object obj){
    BlogPost bp = (BlogPost) obj;
    if(equals(bp)){
      return 0;
    }
    int creationDate = mCreationDate.compareTo(bp.mCreationDate);
    if (creationDate > 0) {
      return 1;
    } else return -1;
  }

What error do you get when you hit the Preview button?

Steve.

I didn't get any error. Frankly speaking, I solved this task pretty quickly, which surprised me a little - positively of course. I wrote to you because I was wondering if it could be solved more optimally by someone with practical experience.

Only a little:

  public int compareTo(Object obj){
    BlogPost bp = (BlogPost) obj;
    if(equals(bp)){
      return 0;
    }
    if (mCreationDate.compareTo(bp.mCreationDate) > 0) {
      return 1;
    } else return -1;
  }

Wow, a lot of learning still ahead of me. Now I see it in my code but as I write I am solely focused on the ultimate result, not the efficiency itself. This is probably the problem of every newbie programmer :).

Don't worry about efficiency right now. Any IDE will let you know when you're creating a variable that's unnecessary - and if you get good at testing you'll learn about efficiency too.

Don`t stress; keep asking questions and keep these things in mind. But it'll take a while to read the code and see the duplication. Simple things like if you only declare a variable to then return it; return the expression rather than declare a variable. If you create an if statement to return a true or a false, just return the condition ...

Too much - learn steadily. Work through the courses. Try to look through Community posts and answer some. It really helps to read other people's interpretation of their problem. And, always, shout me if I can help. You can @ mention me, if needed.

Steve.