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

Java Java Objects (Retired) Delivering the MVP Wrapping up

Gabriel Llanes
Gabriel Llanes
2,894 Points

I am sorry but the instructions for this lesson are way too unclear.

I would really appreciate more specific instructions from someone. I need to know where i can store values and what not

ForumPost.java
public class ForumPost {
  private User mAuthor;
  private String mTitle;
  private String mDescription;

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

  public String getDescription(){
   return mDescription;
  }

  public User getAuthor() {
    return mAuthor;
  }

  public String getTitle() {
    return mTitle;
  }

  // TODO: We need to expose the description
}
User.java
public class User {
  private String mFirstName;
  private String mLastName;

  public User(String firstName, String lastName) {
    mFirstName = firstName;
    mLastName = lastName;
  }

  public String getFirstName(){
    return mFirstName;
  }
  public String getLastName(){
    return mLastName;
  }
}
Forum.java
public class Forum {
  private String mTopic;

  public Forum(String topic){
   mTopic = topic;
  }

  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);
  }
}

1 Answer

I too failed at this my first few times around. Here are the answers for all 4 tasks.

Needless to say this was the most difficult challenge for me out of any here on Treehouse.

Task 1 of 4

public class Forum {
  private String mTopic;

  public Forum(String topic){
   mTopic = topic;
  }

  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());
       */

  }
}

Task 2 of 4

public class User {
  private String mFirstName;
  private String mLastName;

  public User(String firstName, String lastName) {
    mFirstName = firstName;
    mLastName = lastName;
  }

  public String getFirstName(){
    return mFirstName;
  }
  public String getLastName(){
    return mLastName;
  }
}

Task 3 of 4

public class ForumPost {
  private User mAuthor;
  private String mTitle;
  private String mDescription;

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

  public String getDescription(){
   return mDescription;
  }

  public User getAuthor() {
    return mAuthor;
  }

  public String getTitle() {
    return mTitle;
  }

  // TODO: We need to expose the description
}

Task 4 of 4, the hard part. I first uncommented most of the addPost() method in Forum.java

  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());


  }

Then in Example.java I noticed that args[0] and args[1] contained a first and last name because of the if statement at the top of the class. Afterwards, just fill in the constructors and leave the notes commented and uncomment the code parts.

All you really have to do is fill in the two constructors below and recognize they want you to use the args as first and last name.

User(first name, last name) ForumPost(author, book title, book description)

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(args[0], args[1]);
    // Add the author, title and description
    ForumPost post = new ForumPost(author, "Book title", "Book description");
     forum.addPost(post);
  }
}
Gabriel Llanes
Gabriel Llanes
2,894 Points

Wow thank you so much i would have been stuck on this lesson forever without you, they didnt mention anything about treating the author object as array values