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, Delivering the MVP, Stage 4, Wrapping Up, Challenge Task 1 of 4

Challenge Task 1 of 4

We are currently in the process of building a Forum. There is some skeleton code here, but I need your help to finish it up. Let's do this! In the Forum class, add a constructor that takes a String for the topic and sets the private field mTopic.

The given skeleton code is this:

//Forum.java code:
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());
      */
  }
}

//This is the code I think should satisfy challenge 1 of 4, 
//but it does not:

  public void setTopic (String topic) {
    mTopic = topic;
  }

//Note: I also tried make private instead of public,  
but it still says:
//Bummer! 
//Did you add a new constructor that takes accepts a string?

It seems like the above code is a new constructor that takes accepts a string? I'm totally confused..am I supposed to name the constructor something else besides "setTopic" --if so what?

// try this public Forum (String topic) { mTopic = topic;

7 Answers

the task states: In the Forum class, add a constructor that takes a String for the topic and sets the private field mTopic.

creating a constructor generally looks like this:

public Classname() {

}

in our case the class is Forum and we need the constructor to accept a string argument (the topic in this case).

public Forum(String topic) {

}

we also need to set the mTopic member variable inside this constructor:

public Forum(String topic) {
//set mTopic to the topic passed in to the constructor
mTopic = topic;
}

this does not work for me for some reason.

    public class Forum {
      private String mTopic;

      public String getTopic() {
          return mTopic;
      }

      public Forum(String topic) {
    //set mTopic to the topic passed in to the constructor
        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());

      }
   }

** edit

When it says "When all is ready uncomment this..." don't do it.

It seems a little misleading to have that shown on the first challenge. I just commented that out and it worked.

Fiddled around and got it to pass with:

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

}

Now on to Challenge 3 of 4...I'll start a new thread, this one is getting a little long..

I guess I should close this thread and say the issue was resolved. Thanks for the taking a look, though.

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

}

after little changes, this worked for me.

public class User { private String firstName; private String lastName;

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

public String getFirstName(){ return firstName; }

public String getLastName(){ return lastName; }

}

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

public setTopic(String topic) { this.topic = topic; }

public String getTopic() { return topic; }

So this needed new constructor has to have the same name as the class (i.e. Forum)?

Also it gave me a syntax error for:

public Forum(String topic) {
//set mTopic to the topic passed in to the constructor
mTopic = topic
}

//so I added an extra semi-colon and it passed

public Forum(String topic) {
//set mTopic to the topic passed in to the constructor
mTopic = topic;
}

On to challenge 2 of 4. Thanks, Stone, for your help so far...

oops forgot my semicolon. good catch

Uh, oh..stuck again on challenge 2 of 4:

"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!"

I'm guessing the User object has something to do with the code in the User.java tab, but all its has for initial code was:

public class User {

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

//So I added the private fields but it still isn't working:


public class User {

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




//For the Preview output.html it's giving me:


./User.java:5: error: illegal start of expression
      private String firstName;
      ^
./User.java:6: error: illegal start of expression
      private String lastName;
      ^
./User.java:5: error: variable firstName is already defined in constructor User(String,String)
      private String firstName;
                     ^
./User.java:6: error: variable lastName is already defined in constructor User(String,String)
      private String lastName;
                     ^
4 errors

So am I supposed to create some private fields that are OUTSIDE the getter?

Then what does it want inside the getter? Confused again...???

By the way here is the link for the challenge: http://teamtreehouse.com/library/java-objects/delivering-the-mvp/wrapping-up-2

..and the only other forum thread I found that had anything to do with this challenge was here: https://teamtreehouse.com/forum/stage-4-wrapping-up-challenge-task-4-of-4