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

Abe Daniels
PLUS
Abe Daniels
Courses Plus Student 2,781 Points

Not sure how to pass this in to my constructor.

So I am fully aware that 2 things need to be passed in to the author = x area. I am not sure how to write it out. I have tried User.firstName, User.lastName; and it gets upset. I have tried many other combinations of this and they all seem to fail. I need to know, more than anything how you pass in a custom parameter, and how to define it when there are 2 "sub-parameters"

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

  public User getAuthor() {
    return mAuthor;
  }

  public String getTitle() {
    return mTitle;
  }

  public String getDescription() {
    return mDescription;
  }

  public String ForumPost(User author, String title, String description) {
    author = User;
    title = mTitle;
    description = mDescription;
  }
}
User.java
public class User {
  private String mFirstName;
  private String mLastName;
  public User(String firstName, String lastName) {
    // TODO:  Set the private fields here
    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);
  }
}

6 Answers

Abe Daniels
PLUS
Abe Daniels
Courses Plus Student 2,781 Points

I have no idea what just happened but my code works.... I tried author = mAuthor; originally, and it did not work, Now it does. hmph..

Leen Leenaerts
Leen Leenaerts
1,096 Points

it should be {mAuthor = author;mTitle = title;mDescription=description} you are making a "ForumPost" and the member-variable mAuthor should get the User-variable author ... etc.

You should delete the "String" in front of the ForumPost too since you are not return a String

Leen Leenaerts
Leen Leenaerts
1,096 Points

I'm no pro in programming but I think User.firstName will not work outside User.java since it's private I think u just need to use getFirstName and getLastName

Abe Daniels
PLUS
Abe Daniels
Courses Plus Student 2,781 Points

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

tried this, did not work. I continually get told "cannot find symbol" (the most frustrating error ever to exist..)

Abe Daniels
PLUS
Abe Daniels
Courses Plus Student 2,781 Points

That makes sense now.

I was originally assigning ForumPost with String, when it shouldn't have been there. mAuthor = author; works just fine now.

Leen Leenaerts
Leen Leenaerts
1,096 Points

Remember that it's important what you put left and right of the = a = b means you are giving a the value of b, it doesn't mean a equals b