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

William Richert
William Richert
6,418 Points

Stumped on passing final code challenge

I cannot see what I am missing here, suffice to say there are error messages pointing me to the parameters that I am passing into the new User constructor call.

 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
    String firstName = args[0];
    String lastName = args[1];
    User author = new User(String firstName, String lastName);
    ForumPost post = new ForumPost(User author, String "Java Rocks!", String "Onwards!");
    forum.addPost(post);
  }
}
ForumPost.java
public class ForumPost {
  private User mAuthor;
  private String mTitle;
  private String mDescription;

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

  public User getAuthor() {
    return mAuthor;
  }

  public String getTitle() {
    return mTitle;
  }

  // TODO: We need to expose the description
  public String getDescription() {
    return mDescription;  
  }


}
User.java
public class User {
    private String mFirstName;
    private String mLastName;

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

  public String getFirstName() {
    return this.mFirstName;  
  }

  public String getLastName() {
    return this.mLastName;  
  }
}
Forum.java
public class Forum {
  private String mTopic;

  public String getTopic() {
      return mTopic;
  }

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

  public void addPost(ForumPost post) {
      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
    String firstName = args[0];
    String lastName = args[1];
    User author = new User(String firstName, String lastName);
    ForumPost post = new ForumPost(User author, String "Java Rocks!", String "Onwards!");
    forum.addPost(post);
  }
}

1 Answer

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

William;

In Example.java you have multiple errors, although similar and easy to fix.

In these two lines...

Example_Snippet.java
    User author = new User(String firstName, String lastName);
    ForumPost post = new ForumPost(User author, String "Java Rocks!", String "Onwards!");

You don't need to pass in they data types here, just the variable names. If, for example firstName is not of type String Java will let you know. Removing the type declarations will get your code to pass.

Happy coding,
Ken

William Richert
William Richert
6,418 Points

Thank you very much. I had a real misconception about adding the datatypes into method and constructor calls.

Ken Alger
Ken Alger
Treehouse Teacher

William;

Yes, when you call methods, or create new objects you typically don't want to include the types. There are cases in which you would but one of the things that I personally like about Java is that it is strongly typed and knows that when you create a new User, in this case, that you need to pass in a String and String. If you attempt to pass in something else, an int, for example, it won't like that.

Happy coding,
Ken