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

How do I do this 4-part challenge??

Hi there,

I'm trying to wrap up this javascript chapter with a final 4-part challenge. It's kind of involved, so you can see for yourself what its about. For all 4 tasks, I just cant seem to understand how to go about doing what they ask. If you could include some code that would be great. Thanks for the help!

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

1 Answer

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,702 Points

Not sure if you have figured this out or still waiting for an answer, I see you've been waiting awhile. I am going to try to help you understand without directly giving you the answers. 1st thing though, you said javascript in your post but you are linking to Java. They are completely different even though they sound the same. Future reference ;)

I will refer to each block of code you posted with a number and a name and then try to explain what needs to happen.

1) ForumPost.java The TODO comment is asking you to "expose" the description. Expose meaning, he wants you to make the description variable (mDescription) "accessible" outside of its own Class without making the variable itself accessible. This is referring to "Getters and Setters". You dont want to directly be able to change a variable by calling it, you should have a function that calls on its OWN class to change it. If that makes more sense??

In any case if you are still confused about this, there are already 2 pre-built "getters" for you set up. One getter is set for exposing Author (mAuthor) and the other for Title (mTitle). You will see getters return something. setters usually modify a variable but dont return anything.

2) User.java Here you are creating the "Constructor" for the User class. When you instantiate a new class the constructor is immediately run. It basically lets you setup the class a certain way each time its called. In this case, the TODO is asking you to set the private fields. So you create your own member variables (private of course). So kind of like ForumPost.java is set up with its variables but you can name them what you want.

public class User{
 //private variables that make up a User.
 private String mFirstName;

//Constructor: when new class is instantiated must have 1 required parameter
// that will be stored inside the  firstName variable.
public User(String firstName){
 //whatever is passed in to the constructor, we want it to be stored in the class variable fName
 mFirstName = firstName;
}

technically gave you the answer on that one, but you still have to create the lastName part.

3) Forum.java So this one has given you class which still needs a Constructor. The addPost method like the comments say, you will uncomment that at the end on Step 4. It is not ACTUALLY adding the post, right now it is just printing to the console to make it seem like it.

4) Example.java Here you are just going to uncomment the lines of code only, make sure you dont uncomment the lines that tell you what to do. Or if you do makes sure you remove them completely because it will error out.

a new ForumPost(); takes 3 parameters. 1st author, 2nd topic, 3rd description. Which goes between the parenthesis, so they get "passed" into the constructor so it can be created.

I will stop there. See how far you get with that information and come back if you run into problems or dont understand.

Thank you, I got it!