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 trialAlex Bratkovskij
5,329 PointsThere is a mistake in checking the task.
Ok, so in last step of this task we're asked to compare creation dates and return oldest one, here is the thing. I wrote code that shouldn't have passed task cuz I wanted to check for syntax errors first. but it passed anyways. My guess is that while testin, first creation date is older than second passed creation date, so my code return 1, if we'd swapped them around, code would've returned -1 and task would be incomplete.
Or Im missing some logic that I did by luck.
code:
public class BlogPost implements Comparable{
private String mAuthor;
private String mTitle;
private String mBody;
private String mCategory;
private Date mCreationDate;
public BlogPost(String author, String title, String body, String category, Date creationDate) {
mAuthor = author;
mTitle = title;
mBody = body;
mCategory = category;
mCreationDate = creationDate;
}
public int compareTo(Object obj){
BlogPost comparable = (BlogPost) obj;
if(equals(comparable)){
return 0;
}
int cmpCreationDate = mCreationDate.compareTo(comparable.getCreationDate());
return cmpCreationDate;
}
1 Answer
markmneimneh
14,132 PointsI think your logic is right.
so let assume
myObject obj1 = new myObject()
myObject obj2 = new myObject()
obj3 = obj2
let assume myObject has createdDate attribute and your override compare method returns
1 if this object > than that object
0 if this object == that object
-1 if this object < that object
then
obj1.equals(obj2) should return 1
obj2.equals(obj1) should return -1
obj2.equals(obj3) should return 0
I hope this answers your question. If so, please mark the question as answered.
Thank you
Alex Bratkovskij
5,329 PointsAlex Bratkovskij
5,329 PointsYup, my guess is, there is sort method in the test and it just works with the answer that we pass at the end of task 3.