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 trialBhawani Shankar
954 PointsAfter you uncomment the code in Main.java and Forum.java, fix the code as described in the comments of Main.java.
After you uncomment the code in Main.java and Forum.java, fix the code as described in the comments of Main.java. The code in main is attempting to use the code you just fixed..
public class Forum {
private String topic;
private forum( String topic){
this.topic= topic;
}
// TODO: add a constructor that accepts a topic and sets the private field topic
public String getTopic() {
return topic;
}
/* Uncomment this when you are prompted to do so
public void addPost(ForumPost post) {
System.out.printf("A new post in %s topic from %s %s about %s is available",
topic,
post.getAuthor().getFirstName(),
post.getAuthor().getLastName(),
post.getTitle()
);
}
*/
}
public class User {
// TODO: add private fields for firstName and lastName
private String firstName;
private String lastName;
public User(String firstName, String lastName) {
// TODO: set and add the private fields
this.firstName=firstName;
this.lastName=lastName;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
// TODO: add getters for firstName and lastName
}
}
public class ForumPost {
private User author;
private String title;
private String description;
private ForumPost( User author,String title,String description){
this.author=author;
this.title=title;
this.description=description;
}
// TODO: add a constructor that accepts the author, title and description
public User getAuthor() {
return author;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Beginning forum example");
if (args.length < 2) {
System.out.println("Usage: java Main <first name> <last name>");
System.err.println("<first name> and <last name> are required");
System.exit(1);
}
/* Uncomment this when prompted
Forum forum = new Forum("Java");
// TODO: pass in the first name and last name that are in the args parameter
User author = new User();
// TODO: initialize the forum post with the user created above and a title and description of your choice
ForumPost post = new ForumPost();
forum.addPost(post);
// */
}
}
2 Answers
Steve Hunter
57,712 PointsHi Bhawani,
Let's start at the beginning - you'd said you were at stage 4 which was what I answered in the previous post.
The first task, as in the previous post, is to create a constructor for Forum
. The question is Add a constructor that accepts a String named topic to Forum.java. Initialize the private field topic in the constructor to the value passed in.
A constructor for the Forum
class will be called the same as the class. It will be public. The question tells us it takes a String parameter so that it can set the private field called topic
with the value that is passed in. Use the keyword this
to identify that you're using the member variable called topic
that is a member of this instance.
That all look like:
public Forum(String topic){
this.topic = topic;
}
Your code had the constructor set to private
and called it forum
not Forum
(capital F is required).
Next ... In User.java, add private fields to store firstName and lastName, and initialize them in the provided constructor. Add public getters for firstName and lastName. This is quite a big task!
Start with the member variables. They are private
and both are of type String
. Their names are given. Add:
private String firstName;
private String lastName;
at the start of the User
class where the comments tell you to. Now we need to initialize them in the constructor. This is the same as above. Set the this
member variable to be the value passed into the constructor:
public User(String firstName, String lastName) {
// TODO: set and add the private fields
this.firstName = firstName;
this.lastName = lastName;
}
Next, we need to add getter methods for these two member variables. These methods will be public
and will return the private
member variable. Call them getFirstName
and getLastName
. They return a data type of String
.
That looks like:
// TODO: add getters for firstName and lastName
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
Next ... Add a constructor to ForumPost which accepts a User named author, a String named title, and another String named description. Initialize the corresponding private fields.
That's simple. Create a constructor to populate the three member variables that are already in the class. These are:
private User author;
private String title;
private String description;
Again, the constructor is called the same as the class and is public
. Start with:
public ForumPost(){
}
Then, pass in the three required parameters and set them inside the method; we've done this before, as above:
public ForumPost(User author, String title, String description){
this.author = author;
this.title = title;
this.description = description;
}
Now, uncomment the two blocks of code in Forum
and Main
- make sure you uncomment the whole of each block. Then work through the comments in Main
. The first is: // TODO: pass in the first name and last name that are in the args parameter
. I answered this in the other post too. The main method is called with an array of strings as its argument. The first two elements of this array are the first and last names. Access the array with square brackets; the array is called args. So, the first name is stored in args[0]
and the last name is in args[1]
.
To complete the author instance would look like:
User author = new User(args[0], args[1]);
The next Todo is // TODO: initialize the forum post with the user created above and a title and description of your choice
. The constructor for ForumPost
takes three parameters; we know this - we wrote it!! We've created an instance of User
, above. It is called author
then you want to pass in two strings of your choice. I'll keep it simple:
// TODO: initialize the forum post with the user created above and a title and description of your choice
ForumPost post = new ForumPost(author, "Title", "Description");
That's it.
I hope that helps.
Steve.
Richard Merritt
962 PointsDoes not work! I am stuck here please help.
Bhawani Shankar
954 PointsBhawani Shankar
954 PointsYou are just mind blowing!!!
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsJack Cummins
17,417 PointsJack Cummins
17,417 PointsSteve Hunter always has the best answers!
Jack Cummins
17,417 PointsJack Cummins
17,417 PointsSteve Hunter
57,712 PointsSteve Hunter
57,712 Points