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 trialisaac schwartzman
963 Pointsevery time I try to submit all it tells me is bummer try again
is it my code ?
public class Forum {
private String topic;
// TODO: add a constructor that accepts a topic and sets the private field topic
public String getTopic() {
return topic;
}
public static Topic (String topic) {
private String topic = this.topic;
topic = "";
}
/* Uncomment this when you are prompted to do so
public void addPost(ForumPost post) {
System.out.printf("A new post in %s topic from %s %s about %s is available",
topic,
post.getAuthor().getFirstName(),
post.getAuthor().getLastName(),
post.getTitle()
);
}
*/
}
public class User {
// TODO: add private fields for firstName and lastName
public User(String firstName, String lastName) {
// TODO: set and add the private fields
}
public static Names (String firstName, String lastName) {
private String firstName = this.firstName;
firstName = "";
private String lastName = this.lastName;
lastName = '';
}
public static getNames (){
String lastNameIs = User(lastName);
String firstNameIs = User(firstName);
return lastNameIs;
return firstNameIs;
}
// TODO: add getters for firstName and lastName
}
public class ForumPost {
private User author;
private String title;
private String description;
// TODO: add a constructor that accepts the author, title and description
public User getAuthor() {
return author;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Beginning forum example");
if (args.length < 2) {
System.out.println("Usage: java Main <first name> <last name>");
System.err.println("<first name> and <last name> are required");
System.exit(1);
}
/* Uncomment this when prompted
Forum forum = new Forum("Java");
// TODO: pass in the first name and last name that are in the args parameter
User author = new User();
// TODO: initialize the forum post with the user created above and a title and description of your choice
ForumPost post = new ForumPost();
forum.addPost(post);
// */
}
}
2 Answers
Timothy Hilley
2,292 Pointstopic = ""; needs to be private outside of the method to declare and initialize the String topic
private String topic = "";
also, if you have topc = ""; right after declaring this.topc (same variable), youre overwriting what you just put in it (the methods take in topic)
private String topic = "";
// TODO: add a constructor that accepts a topic and sets the private field topic
public String getTopic() {
return topic;
}
public static Topic (String topic) {
this.topic = topic; //the topic being placed in the methods(thispart) is now saved to the class's topic
}
also, you dont have to (and cant) privatize a variable inside a scope that is public.
Timothy Hilley
2,292 Pointsand just to clear up anything on constructors: A constructor is the main attributes of the object that creates an object from the template. Look at a class as a play-dough mold, and the variables it takes in is the play-dough, creating an object (the instance). Without the constructor, instances cant be made. The class itself is not an object, but a blueprint on making that object. the instances are the objects. So in other classes, i can make as many playdough molds as i please and use them separately as seperate objects of the same class, because of the constructor.
public class PlayDoughStar {
private String starColor;
public PlayDoughStar(String starColor) {
this.starColor= starColor;
}
//the rest of the class code
}
so in a different class, i want to make a play-dough star:
public class Table {
PlayDoughStar redStar = new PlaydoughStar("red");
PlayDoughStar blueStar = new PlaydoughStar("blue");
//Tables constructor here {
//}
Timothy Hilley
2,292 PointsTimothy Hilley
2,292 Pointsbe sure to uncomment the blocks of code already supplied when prompted btw!
isaac schwartzman
963 Pointsisaac schwartzman
963 Pointsthanks for your post it actually cleared up my confusion of constructors, and sadly I was really unclear but my problem is the user section about the whole " In User.java, add private fields to store firstName and lastName, and initialize them in the provided constructor. Add public getters for firstName and lastName. " but still your response taught me a few things :)
Timothy Hilley
2,292 PointsTimothy Hilley
2,292 Pointsthink about what you just wrote: and then press enter every now and then. thats called psuedo code: