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 trialprasannakulkarni2
14,918 PointsAdding the second constructor.
Not able to cross this second challenge. Here is my code:
public class Spaceship {
public String mType= "SHUTTLE";
public String type;
public String getType() {
return mType;
}
public void setType(String type) {
mType = type;
}
}
3 Answers
Ken Alger
Treehouse TeacherI answered this in your other post on this same challenge.
Michael McKenna
3,451 PointsI read Ken's explanation and I still can't get it either. Has anyone else been able to do this?
Ken Alger
Treehouse TeacherMichael;
Let's take a look at this:
Task 1
Our first constructor sets a default type of Spaceship
as "SHUTTLE", our code then looks like:
public Spaceship() {
mType = "SHUTTLE";
}
Now when you call Spaceship()
in your code it will default to the type of SHUTTLE.
Task 2
The second task (constructor) needs to be able to take a different type for the spaceship, a constructor that will set the type to the type that is passed in.
public Spaceship(String type) {
mType = type;
}
Now if we can create Spaceships and call them anything other than SHUTTLE.
Does that make any sense?
Ken
Michael McKenna
3,451 PointsYes, thank you very much. I appreciate you taking your time for your swift and thorough explanations.
Ken Alger
Treehouse TeacherMichael;
No problem at all. Welcome to Treehouse and Happy Holidays.
Ken
W Joel Baker
1,012 PointsW Joel Baker
1,012 PointsHey Ken,
Thank you for your detailed explanation below for the second part of the challenge. It helps clear some of the fog. Still not really wrapping my head around it yet but I keep getting bits and pieces.
Again, thank you! Joel