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
W Joel Baker
1,012 PointsHow do you write a new class in Android Studio?
Challenge Task 1 of 4 from https://teamtreehouse.com/library/build-an-interactive-story-app/the-modelviewcontroller-pattern/creating-a-data-model
Let's create a new class for our data model! Start by declaring a new class named Spaceship. Make it public.
I thought it was a simple answer until I got the "Bummer" notice.
public Spaceship mSpaceship
3 Answers
Steve Hunter
57,712 PointsNearly, Joel,
public class Spaceship{
}
Steve.
Steve Hunter
57,712 PointsHi Joel,
If you look at Ben's code, you can see the class called Page being defined.
What you've got in your first snippet is creating two instances of the Choice class.
So, using the new Spaceship class means we can have many instances of it.
public Spaceship marsRover;
public Spaceship moonOrbiter;
public Spaceship voyagerTwo;
I hope that makes sense.
Steve.
W Joel Baker
1,012 PointsOh, wow. OK I see it. Thanks Steve! Here is where I got confused. From the lesson we created the new class by typing these. If these are not actually classes what are they?
private Choice mChoice1;
private Choice mChoice2;
Full code
package teamtreehouse.com.interactivestory.model;
/**
* Created by benjakuben on 11/2/14.
*/
public class Page {
private int mImageId;
private String mText;
private Choice mChoice1;
private Choice mChoice2;
public int getImageId() {
return mImageId;
}
public String getText() {
return mText;
}
public void setText(String text) {
mText = text;
}
public Choice getChoice1() {
return mChoice1;
}
public void setChoice1(Choice choice1) {
mChoice1 = choice1;
}
public Choice getChoice2() {
return mChoice2;
}
public void setChoice2(Choice choice2) {
mChoice2 = choice2;
}
public void setImageId(int id) {
mImageId = id;
}
}
W Joel Baker
1,012 PointsHmm.... I have
public class Spaceship;
Error message is
./Spaceship.java:1: error: '{' expected
public class Spaceship;
^
./Spaceship.java:1: error: reached end of file while parsing
public class Spaceship;
^
2 errors
Got it!
public class Spaceship {
}
Steve Hunter
57,712 PointsYeah - I forgot the braces! Ooopsie!
W Joel Baker
1,012 PointsW Joel Baker
1,012 PointsHaha! Exact same time! Thanks Steve!