Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Delivering the MVP Wrapping up

Gaspar Santiago
Gaspar Santiago
3,377 Points

ForumPost instance

In the Example Class, whenever I go to complete the required parameters for the instance of ForumPost, for the first parameter, I can't use post.getAuthor(), nor can I use user.getFirstName() or getLastName(). I'm lost at this point as to what to use to fulfill the first parameter of the ForumPost post object. Also, it won't let me hit the "Get Help" button while I'm in the code, so I have to close out and get back in in order to use this feature which erases all my progress. So it looks like I haven't done anything when in fact I'm almost done (Step 4).

ForumPost.java
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
}
User.java
public class User {

  public User(String firstName, String lastName) {
    // TODO:  Set the private fields here
  }
}
Forum.java
public 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());
      */
  }
}
Example.java
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);
  }
}

2 Answers

Benjamin Barslev Nielsen
Benjamin Barslev Nielsen
18,958 Points

Here is the constructor for ForumPost:

public ForumPost(User author, String title, String description){
    mAuthor = author;
    mTitle = title;
    mDescription = description;
  }

Note that in line 10 in Example.java we should instantiate a user object, by using the args array:

User author = new User(args[0], args[1]);

We can see in the constructor for ForumPost, that it takes a user, so lets instantiate ForumPost with author and two arbitrary strings for the title and description:

// Add the author, title and description
ForumPost post = new ForumPost(author, "someTitle", "someDescription");
Gaspar Santiago
Gaspar Santiago
3,377 Points

Ok I'll give this a try. I used author but like this - author.getAuthor() I'll try it without the method call... thanks!