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
nobodyinhere
3,418 PointsClass Review @ Java Data Structures
This is only review for previously lessons. In this lesson I got some Engine errors.
PS: I shared codes of challenges.
5 Answers
nobodyinhere
3,418 PointsI think I figure out what was going on. There was a problem with Firefox Developer Edition. I refreshed my currently page and it fixed.
I completed challenge and I'm going to share my codes for everyone. If anyone needs them.
Challenge Task 1 of 3
Add declarations for private member fields for author, title, body, category, and creation date. The date should be of type java.util.Date (make sure you import it).
They want we import a util class which named "java.util.Date" then we have to create our variables which are our member fields as private.
package com.example;
import java.util.Date;
public class BlogPost {
private String mAuthor;
private String mTitle;
private String mBody;
private String mCategory;
private Date mCreationDate;
}
Challenge Task 2 of 3
Now add a constructor that accepts author, title, body, category and creation date. In your constructor, initialize the private variables you created in Step 1 using the arguments passed in.
Here we go! Now all we need is a constructor with the name of class (same name) then we initialize new variables with our member fields.
package com.example;
import java.util.Date;
public class BlogPost {
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;
}
}
Challenge Task 3 of 3
Finally, please add a getter method for the body.
You didn't think I was going to make you type them all out, did you?!!
Now we need to add our variables' getter methods after constructor. And here our getter methods:
private String getAuthor() {
return mAuthor;
}
private String getTitle() {
return mTitle;
}
private String getBody() {
return mBody;
}
private String getCategory() {
return mCategory;
}
private Date getCreationDate() {
return mCreationDate;
}
You have these code blocks at all in the last challenge:
package com.example;
import java.util.Date;
public class BlogPost {
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;
}
}
private String getAuthor() {
return mAuthor;
}
private String getTitle() {
return mTitle;
}
private String getBody() {
return mBody;
}
private String getCategory() {
return mCategory;
}
private Date getCreationDate() {
return mCreationDate;
}
}
Edward Torres
6,158 PointsI have exactly what you have but this is what is says in the compiler every time. I've been stuck on the problem for a long time please help.
./com/example/BlogPost.java:22: error: class, interface, or enum expected private String getAuthor() { ^ ./com/example/BlogPost.java:24: error: class, interface, or enum expected } ^ ./com/example/BlogPost.java:28: error: class, interface, or enum expected } ^ ./com/example/BlogPost.java:32: error: class, interface, or enum expected } ^ ./com/example/BlogPost.java:36: error: class, interface, or enum expected } ^ ./com/example/BlogPost.java:40: error: class, interface, or enum expected } ^ 6 errors
Luis Santos
3,566 PointsI am having trouble on the last the final question. For the life of me it doesn't seem to want to register the final line of code.
MUZ140616 Gift CHibaya
1,319 Pointswell challenge 3 of 3 did not necessarily require us to add all the getter methods, it only called for the "BODY" method
The method for "body" is as follows public String getBody() { return mBody }
if you dont get it, ask further
Albert Tomasura
11,041 PointsAttempting the very first challenge with syntactically and semantically correct code results in "illegal start of expression/not a statement" errors for package, import, and class (basically every line of code at this point).
Also this appears: "Bummer: Make sure you keep the package statement on the first line. There seems to be some problems, use preview to see the compilation errors."
...Even though package is still on line 1. I am fairly confident it is an error within the code challenge itself. It's possible there is some Java code that runs before the user's code challenge attempt that has not been closed properly (missing semicolon, parentheses, etc.).
saurabh agrawal
1,702 Pointssaurabh agrawal
1,702 Pointsuse this code-: package com.example;
import java.util.Date;
public class BlogPost {
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; }
private String getAuthor() { return mAuthor; }
private String getTitle() { return mTitle; }
private String getBody() { return mBody; }
private String getCategory() { return mCategory; }
private Date getCreationDate() { return mCreationDate; }
}