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 trialCasey Huckel
Courses Plus Student 4,257 PointsWhat's needed here?
I'll attach my code:
package com.example;
import java.io.Serializable;
import java.util.Date;
public class BlogPost implements Comparable, Serializable {
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;
}
@Override public int compareTo(BlogPost other) {
if (equals(other)) {
return 0;
}
return mCreationDate.compareTo(other.mCreationDate);
}
public String[] getWords() {
return mBody.split("[^\\w']+");
}
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;
}
}
package com.example;
import java.io.Serializable;
import java.util.Date;
public class BlogPost implements Comparable, Serializable {
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;
}
@Override public int compareTo(BlogPost other) {
if (equals(other)) {
return 0;
}
return mCreationDate.compareTo(other.mCreationDate);
}
public String[] getWords() {
return mBody.split("[^\\w']+");
}
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;
}
}
3 Answers
Ken Alger
Treehouse TeacherCasey;
Let's have a look:
Task 1
Now that we know about Generics, let's upgrade our
Comparable
interface defined on theBlogPost
class to use it. Remember to change the parameter in thecompareTo
method to be of typeBlogPost
.
I'm going to work backwards here a bit and hopefully by the end you will see why. Our given compareTo()
method is:
public int compareTo(Object obj) {
BlogPost other = (BlogPost) obj;
if (equals(other)) {
return 0;
}
return mCreationDate.compareTo(other.mCreationDate);
}
We're asked to "upgrade" this to use a BlogPost
instead of an Object
here and in doing so remove the casting statement inside our method.
public int compareTo(BlogPost other) {
if (equals(other)) {
return 0;
}
return mCreationDate.compareTo(other.mCreationDate);
}
Great. Almost there. If we run that code, we get back an error:
BlogPost is not abstract and does not override abstract method compareTo(Object) in Comparable
Great. What does that mean? Well, if we think for a second, during our upgrade process we forgot to "upgrade" our class interface in addition to our code. We need to let our class know that we want to utilize Comparable<BlogPost>
to be able to compare BlogPost
objects. Therefore we need to add that up top where we define our class:
public class BlogPost implements Comparable<BlogPost>, Serializable {
// other stuff here
Take a look at the Comparable documentation for more information.
Post back if you are still stuck or have further questions.
Happy coding,
Ken
Casey Huckel
Courses Plus Student 4,257 PointsI got it Ken, thanks!
John Miller
4,007 PointsThank you Ken, I didn't realize I had to change my generic in the call to compareTo and was starting to bang my head against the monitor. I go through so many monitors that way.
Huggins Tatenda Mafigu
7,091 Pointspublic class BlogPost implements Comparable<BlogPost>, Serializable { private String mAuthor; private String mTitle; private String mBody; private String mCategory; private Date mCreationDate;
public int compareTo(BlogPost other) { //BlogPost other = (BlogPost) obj; if (equals(other)) { return 0; } return mCreationDate.compareTo(other.mCreationDate);
But im still getting errors pliz help!!!
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsChris Jones
Java Web Development Techdegree Graduate 23,933 PointsThanks, Ken!
Just FYI to anyone else reading this - if you comment out the casting line in the compareTo method like below, the code challenge doesn't realize it's commented out. You actually have to delete this line to pass the challenge.
Just wanted to save some other people some frustration!
//BlogPost other = (BlogPost) obj;