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 trialSamson Okai
932 PointsIssue with Comparable exercise
In Task 1 of the Comparable exercise, I'm told to create a compareTo method which takes in an Object and returns an int value 1. Task 1 passes successfully.
In Task 2, I am to expand the compareTo method and cast the Object to a com.example.BlogPost object and then compare the new object to the Object that was passed into the method as a parameter. While the default return value is still set to 1, if the Object that was passed in is equal to the new object, then the return value is set to 0. The final step is to return whichever int depending on the conditional statement.
This is how I built out my compareTo method in Task 2:
public int compareTo(Object obj){ int result = 1; BlogPost object = (BlogPost) obj; boolean isEqual = object.equals(obj); if (isEqual) result = 0; //System.out.println("They're equal"); return result; }
While testing, I put a statement to confirm that I was making it through the equality check and the Preview confirmed my expectations, however, for some reason, I keep getting the "Oops! It looks like Task 1 is no longer passing" message and I'm unclear why this is the case.
I feel like my code does what it's supposed to do: -Set default return value to 1 -Check for object equality -If objects are equal, set return value to 0, otherwise, return default return value -Return the result
What am I missing?
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;
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){
int result = 1;
BlogPost object = (BlogPost) obj;
boolean isEqual = object.equals(obj);
if (isEqual)
result = 0;
return result;
}
// snip
}
1 Answer
andren
28,558 PointsI am to expand the compareTo method and cast the Object to a com.example.BlogPost object and then compare the new object to the Object that was passed into the method as a parameter.
This is where you are going wrong, as that is not actually what the task want you to do. Casting does not change an object, only how it is treated. So the cast object and the one passed will always be considered equal, since they are in fact the same object.
What you are meant to do is compare the current object the code is inside of to the object that was passed in. That is done by calling the equals
method of the current object which can be done either by using this.equals
or simply equals
like this:
public int compareTo(Object obj){
int result = 1;
BlogPost object = (BlogPost) obj;
boolean isEqual = equals(obj);
if (isEqual)
result = 0;
return result;
}
Then the code will work. As you are now calling the equals
method found within the BlogPost
object and using that to compare that object against the object that was passed in.
Samson Okai
932 PointsSamson Okai
932 PointsThanks for the help. I was losing my mind for a second there.