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 trialahmed alghafli
3,446 PointsWe are currently in the process of building a Forum. There is some skeleton code here, but I need your help to finish it
ammm can anyone help me with this
We are currently in the process of building a Forum. There is some skeleton code here, but I need your help to finish it up. Let's do this! In the Forum class, add a constructor that takes a String for the topic and sets the private field mTopic.
public class ForumPost {
private User mAuthor;
private String mTitle;
private String mDescription;
public User getAuthor() {
return mAuthor;
}
public String getTitle() {
return mTitle;
}
// TODO: We need to expose the description
}
public class User {
public User(String firstName, String lastName) {
// TODO: Set the private fields here
}
}
public class Forum {
private String mTopic;
public void setTopic(String topic) {
mTopic = topic;
}
public String getTopic() {
return mTopic;
}
public void addPost(ForumPost post) {
System.out.printf("New post from %s %s about %s.\n",
post.getAuthor().getFirstName(),
post.getAuthor().getLastName(),
post.getTitle());
}
}
public class Example {
public static void main(String[] args) {
System.out.println("Starting forum example...");
if (args.length < 2) {
System.out.println("first and last name are required. eg: java Example Craig Dennis");
}
// Forum forum = new Forum("Java");
// Take the first two elements passed args
// User author = new User();
// Add the author, title and description
// ForumPost post = new ForumPost();
// forum.addPost(post);
}
}
4 Answers
Ken Alger
Treehouse TeacherAhmed;
If you have forgotten about constructors in Java, I would suggest watching the Constructors video again. Mr. Dennis does a nice job of explaining their reasoning for existence and their use. In summary constructors are methods you can use to set initial values for field variables. When the object is created, Java calls the constructor first. Any code you have in your constructor will then get executed. You don't need to make any special calls to a constructor method - they happen automatically when you create a new object.
Constructor methods take the same name as the class, so for this challenge we need to create a method with the same name as our Forum
class that takes a String
for the topic and sets the private field mTopic
. Let's work on syntax first, then add functionality.
We need to create a method with the same name as the class, in this case, Forum
, that looks like:
Forum() {}
It needs to take a String
for the topic...
Forum(String topic) {}
Now, we should probably add an access modifier to our constructor. If we do not add an access modifier they default to package private
, so outside our package the constructor would not be accessible. That will work for this challenge to leave off the access modifier, but for our purposes at this point in the challenge setting it to public
is a good practice. We would then have:
public Forum(String topic) {}
Great, now we have our constructor defined, but it isn't doing anything. We were asked to set the private mTopic
to the value passed into the method, topic. Inside our function we need to do an assignment of mTopic = topic;
That should do it. I hope that helps explain things a bit, but post back if you are still stuck or if I just confused you even more.
Happy coding,
Ken
Ken Alger
Treehouse TeacherAhmed;
A couple of things here.
- I don't see a constructor in your Forum class like is required for the challenge.
- It looks like you uncommented the code prior to the end of the challenge, that will cause issues until the remainder of the challenge is completed.
Post back if you are still stuck.
Ken
ahmed alghafli
3,446 Pointsthat's what I need because I don't know how to do it ?!!
hubertl
8,483 PointsHi Ahmed, not sure if this is sorted or yet or not. If not please ensure that you will use class Forum (select the correct file Forum.java). Constructor needs to look as follow: (as explained by Ken - where Forum is class name - topic is a string variable and mTopic is already declared private string variable.
public Forum(String topic) { mTopic = topic; }
Thanks, Hubert
MUZ140876 Andrew Ndhlangisa
4,804 Pointspublic class Forum { private String mTopic;
public String getTopic() { return mTopic; }
public void addPost(ForumPost post) { /* When all is ready uncomment this... System.out.printf("New post from %s %s about %s.\n", post.getAuthor().getFirstName(), post.getAuthor().getLastName(), post.getTitle()); */ }
public Forum(String getTopic) { mTopic = getTopic; }
}
ahmed alghafli
3,446 Pointsahmed alghafli
3,446 PointsThanks Ken you are explain it very well ,
Happy coding ^_*
MUZ140584 Heredity Mbundire
4,979 PointsMUZ140584 Heredity Mbundire
4,979 Pointsts not working still have problem with this code this my code public Forum(string topic) { private User mAuthor; private String mTitle; private String mDescription;
public User getAuthor() { return mAuthor; }
public String getTitle() { return mTitle; }
// TODO: We need to expose the description }