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

Daniel Schwemmlein
Daniel Schwemmlein
4,960 Points

last challenge in java objects. add private fields and appropriate getters

And once again I'm lost at a java challenge. It says that I'm supposed to 'add the private fields to store the parameters defined in the constructor? And also add appropriate getters' should I just add private Field (firstName) or something like that?

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 Forum(String topic) {
    mTopic = topic;
  }


  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

Daniel Schwemmlein
Daniel Schwemmlein
4,960 Points

solution turned out to be:

      public class User {

      private String mFirstName;  //we make it a String type : TYPE and mSomeName was a hint
      private String mLastName;  //we also make this one a String type : TYPE and mAnotherName was also a Hint

      public User(String firstName, String lastName) {

          // TODO:  Set the private fields here          
          mFirstName = firstName;  //we are assigning firstName to mFirstName
          mLastName = lastName;   //we are assigning lastName to mLastName
      }


       // TYPE , getSomeName(), and mSomeName were all hints. 
       // We will also need a String TYPE to return because we are returning mFirstName and it is a String
       public String getFirstName() {
           return mFirstName;
       }

       // TYPE , getAnotherName(), and mAnotherName were all hints 
       public String getLastName() {
           return mLastName;
       }        
    }
Christopher Augg
Christopher Augg
21,223 Points

Daniel,

Good job! I edited you answer to show the code correctly. Please refer to the Markdown Cheatsheet at the bottom of the page for examples.

Regards,

Chris

Thanks man, I get it.

Christopher Augg
Christopher Augg
21,223 Points

Daniel,

It looks like you are on step 2 of 4 on the challenge where the instructions state, "Okay, so let's work on the User object now. I really didn't get too far. 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? Thanks!"

According to the instructions, we need to focus on the User class.

     public class User {

          // Do I need private fields here?
          // How many?
          // names to use?


          public User(String firstName, String lastName) {
              // TODO:  Set the private fields here
          }

         //Do I need get methods here?

     }

The answer to your question is yes. You should add private fields to this class in which you can use the arguments passed into the constructor for the purposes of assignment. Remember to use naming conventions like mFirstName etc. You also need to declare get methods that can return those values when called.

I hope this helps.

Regards,

Chris

Dont really get it either. I wrote this for my private

public User(String firstName, String lastName) { private String mfirstName; private String mlastName; }