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 trialAbe Daniels
Courses Plus Student 2,781 PointsNot sure how to pass this in to my constructor.
So I am fully aware that 2 things need to be passed in to the author = x area. I am not sure how to write it out. I have tried User.firstName, User.lastName; and it gets upset. I have tried many other combinations of this and they all seem to fail. I need to know, more than anything how you pass in a custom parameter, and how to define it when there are 2 "sub-parameters"
public class ForumPost {
private User mAuthor;
private String mTitle;
private String mDescription;
public User getAuthor() {
return mAuthor;
}
public String getTitle() {
return mTitle;
}
public String getDescription() {
return mDescription;
}
public String ForumPost(User author, String title, String description) {
author = User;
title = mTitle;
description = mDescription;
}
}
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;
}
}
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 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);
}
}
6 Answers
Abe Daniels
Courses Plus Student 2,781 PointsI have no idea what just happened but my code works.... I tried author = mAuthor; originally, and it did not work, Now it does. hmph..
Leen Leenaerts
1,096 Pointsit should be {mAuthor = author;mTitle = title;mDescription=description} you are making a "ForumPost" and the member-variable mAuthor should get the User-variable author ... etc.
You should delete the "String" in front of the ForumPost too since you are not return a String
Leen Leenaerts
1,096 PointsI'm no pro in programming but I think User.firstName will not work outside User.java since it's private I think u just need to use getFirstName and getLastName
Abe Daniels
Courses Plus Student 2,781 Pointspublic ForumPost(User author, String title, String description) { author = getFirstName(), getLastName(); title = mTitle; description = mDescription; }
tried this, did not work. I continually get told "cannot find symbol" (the most frustrating error ever to exist..)
Abe Daniels
Courses Plus Student 2,781 PointsThat makes sense now.
I was originally assigning ForumPost with String, when it shouldn't have been there. mAuthor = author; works just fine now.
Leen Leenaerts
1,096 PointsRemember that it's important what you put left and right of the = a = b means you are giving a the value of b, it doesn't mean a equals b