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 trialbohot hard
405 Pointscreating new objects of choice class
Hello i did not understood why we are just creating new objects of choice class in the constructor of story class and i am confusing with these two lines of code hope i will get things clear ...Thank you
pages[0].setChoice1(new Choice()); pages[0].setChoice2(new Choice());
1 Answer
Lauren Moineau
9,483 PointsHi @bohot hard. In this app, we have 3 model classes: Story, Page and Choice. We know that a story has pages and pages have choices (usually 2, but sometimes 1).
In this lesson, we're building the first page of our story (page 0), the welcoming page, and we want that page to have 2 choices the user can choose from. So let's look at the constructor for Page, the one with 2 choices:
public Page(int imageId, int textId, Choice choice1, Choice choice2) {
this.imageId = imageId;
this.textId = textId;
this.choice1 = choice1;
this.choice2 = choice2;
}
This is the blueprint we have to use to build that first page we want. Thanks to this constructor, we know that page 0 should have an image, some text and 2 choices.
So we create that page and use the setters defined in that Page model class to give it its image (passing in the value R.drawable.page0
), text (passing in the value R.string.page0
) and choices.
pages[0] = new Page();
pages[0].setImageId(R.drawable.page0);
pages[0].setTextId(R.string.page0);
pages[0].setChoice1(new Choice());
pages[0].setChoice2(new Choice());
Regarding
pages[0].setChoice(new Choice));
It is a shortcut. We could have written:
Choice choice1 = new Choice(); // creating an instance of a Choice object
Choice choice2 = new Choice(); // creating a second instance of a Choice object
pages[0].setChoice1(choice1));
pages[0].setChoice2(choice2));
At this stage, choice1 and choice2 have no value, they will be defined later in the course and passed in setChoice1()
and setChoice2()
then.
So for now, our story object has an image, a text and 2 choices (with no value).
I hope that helps :)