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 trialDaniel Schwemmlein
4,960 PointsStill stuck at the java challenge (last one for objects) task2 out of 4
It says there:"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 completely lost. I've tried a few things but of course it would not work. what else should i try?
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
}
public class User {
private String Forum() {
}
private String getTopic() {
}
// 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?
}
}
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());
*/
}
}
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
Christopher Augg
21,223 PointsDaniel,
No problem. Lets look at those instructions again.
"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!"
Ok, it looks like we only need to work on the User object for this part of the challenge. We are provided:
public class User {
public User(String firstName, String lastName) {
// TODO: Set the private fields here
}
}
We need to add private fields....hmm He means variables that are private and can hold the same type as the ones declared within the constructor. Recall that we should use the naming conventions taught earlier like prefixing with the letter m and using camel case.
public class User {
private TYPE mSomeName; //what type do we need? What name would be more appropriate?
private TYPE mAnotherName; //Same goes for this one
public User(String firstName, String lastName) {
// TODO: Set the private fields here
// How do we assign the arguments being passed in (firstName, lastName)
// to the new variables we just made?
// Hint: private variable added = passed in variable
}
}
Ok, now we just need some get methods to return the values of the private variables we added.
public class User {
private TYPE mSomeName; //what type do we need? What name would be more appropriate?
private TYPE mAnotherName; //Same goes for this one
public User(String firstName, String lastName) {
// TODO: Set the private fields here
// How do we assign the arguments being passed in (firstName, lastName)
// to the new variables we just made?
// Hint: private variable added = passed in variable
}
// Get method that returns the first variable
public TYPE getSomeName() {
return mSomeName
}
// Get method that return the second variable
???
}
I hope these hints help you out. However, just give me a shout in the forums if not and I will elaborate further.
Regards,
Chris
p.s. Opps.....put variables in wrong spot .. fixed now