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

jinhwa yoo
jinhwa yoo
10,042 Points

I tried.. but...

I stuck as I expected.... help me

5 Answers

The first line is complete - we just uncomment that. Next code line is:

User author = new User();

This is an incomplete constructor. We know this because we wrote it!! A User takes two strings, a first name and a last name, remember? So amend that line to include the names:

User author = new User("James", "Bond");

However, Craig has added some args to the main method. These are the names he wants to see in the post, so while the above is OK, this is better:

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

The next line is also incompete and, again, we wrote the constructor for this too!

ForumPost post = new ForumPost();

We know a ForumPost takes three things; a User (already created) and two strings for a title and a description. We've defined the author already - that's the args thing above. Make up two strings for the other two:

    ForumPost post = new ForumPost(author, "A Title", "A Description");
    forum.addPost(post);

Phew!! That should do it.

Any quetions about anything above, just ask, I'll explain as best I can.

Steve.

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Steve,

you are awesome !

Thumbs uuuuuuup !

Grigorij

:-)

Hi Jinhwa,

Let's do this one at a time ... Q1: In the Forum class, add a constructor that takes a String for the topic and sets the private field mTopic.

So, we need to create a constructor. That's a method that returns nothing (and not a void return either) and is called the same name as the class, Forum. This one takes one string and sets the mTopic variable inside it:

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

Next, in the User class: Q2 Can you please add the private fields to store the parameters defined in the constructor? Ooh while you're at it can you add the appropriate getters?

That's quite a lot of typing!! Three jobs: 1. add the member variables based on what the constructor receives, 2. set those member variables inside the constructor and 3. create the getter methods:

public class User {
  private String mFirstName; // 1. added member variables here
  private String mLastName;

  public User(String firstName, String lastName) {
    // TODO:  Set the private fields here
    mFirstName = firstName; // 2. set them in here
    mLastName = lastName;
  }
  // 3. add the getters below
  public String getFirstName(){
    return mFirstName;
  }

  public String getLastName(){
    return mLastName;
  }
}

That's a lot of code - read through it and make sure it is familiar - I've commented the three tasks to highlight them. I'll post the next bits on a separate answer as this one is getting long!!

Steve.

Right - on to task 3 ... It's going to need a constructor that takes a User author a String title and a String description. Don't forget to initialize the member fields in the constructor. Also please add the getter for mDescription.

We're in the ForumPost class now. We need to create a constructor and set the three member variables inside it. Then add a getter for one of them:

// here's the constructor
  public ForumPost(User author, String title, String description){
    mAuthor = author;
    mTitle = title;
    mDescription = description;
  }
  // here's the getter
  public String getDescription(){
    return mDescription;
  }

The next bit is a little long too! First, uncomment the line in Forum:

  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 switch to Example where there's a load of commented out code. Uncomment these one at a time (not he actual comments, just the code lines!:

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

I'll start a new answer again ...

jinhwa yoo
jinhwa yoo
10,042 Points

why do you write " forum.addpost(post); ??????/

and also what is the purpose of "Example.java" ????

Hi there,

There's a few objects here that are all brought together. Example.java does that, it holds the main function which drives the execution of the application.

Let's take the last code block in Example, and read through the comments too:

    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, "A Title", "A Description");
    forum.addPost(post);

First, we create an instance of a Forum, called forum. Then we do the same for a User object and a ForumPost object too.

The last line, forum.addPost(post); adds the instance of the ForumPost object, called post into the instance of a Forum object, called forum. The addPost method defined in the Forum class manages this and takes a post as a parameter.

I hope that explains a little more for you.

Steve.