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 PointsNow add a second constructor that takes a String parameter. Use this parameter to update mType to a specific type of spa
Please help in solving this question. I am stuck and cannot move foreward.
public class Spaceship {
public String mType="SHUTTLE";
public String type = "";
public String getType() {
return mType;
}
public void setType(String type) {
mType = type;
}
}
4 Answers
Ken Alger
Treehouse TeacherPrasanna;
Let's think about what this challenge is asking, with the fact that sometimes the Challenge Engine Checker can allow things to pass when they are not absolutely correct.
Task 1
Let's continue with our custom data model. We need some constructors. First, add a default constructor with no parameters. Inside the constructor, set mType
to "SHUTTLE".
We need to write a constructor. If you don't remember from the videos a constructor declaration looks like a method declaration—except that they use the name of the class and has no return type. Therefore for our class Spaceship
a constructor would look like:
public Spaceship() {
// Constructor code goes here
}
The task asks us to: set mType
to "SHUTTLE". We need to put that inside our constructor like so:
public Spaceship() {
mType = "SHUTTLE";
}
The code you have sets mType
to "SHUTTLE" outside of a constructor.
Task 2
Now add a second constructor that takes a String parameter. Use this parameter to update mType
to a specific type of spaceship.
We need to create an additional constructor for Task 2, but one that takes a String as a parameter, like:
public Spaceship(String type) {
// Constructor code goes here
}
for example. Then, inside the constructor set mType
to the parameter, in this example type
.
Hopefully that points you in the right direction, if not post back with additional questions.
Ken
prasannakulkarni2
14,918 PointsHello Ken,
Thanks for such a great answer. This approach worked for me.
Could you please suggest about how can I further acquaint myself with JAVA? I have taken an Android development class for Spring 2015 in grad school and want to get aquatinted with the language (& Android) before class starts in January. Your help much appreciated.
Thanks, Prasanna.
Ken Alger
Treehouse TeacherPrasanna;
Treehouse offers two very good Java courses, Java Basics and Java Objects. If you are new to Java and will be taking an Android course soon, I would highly recommend you go through those courses as well as staying plugged in on the forum here for any additional questions you may have.
Pleased that I could be of some help.
Happy coding,
Ken
prasannakulkarni2
14,918 PointsGreat. I will check them out.
Thanks.
cossy
17,748 Pointspublic class Spaceship { public String shipType;
public Spaceship() { setShipType("SHUTTLE"); } public Spaceship(String shipType) { this.shipType=shipType; }
public String getShipType() {
return shipType;
}
public void setShipType(String shipType) {
this.shipType = shipType;
}
}
mongst
10,841 PointsCossy did all the work - I'm just putting it all in one box:
public class Spaceship { public String shipType;
public Spaceship() { setShipType("SHUTTLE"); }
public Spaceship(String shipType) { this.shipType=shipType; }
public String getShipType() {
return shipType;
}
public void setShipType(String shipType) {
this.shipType = shipType;
}
}
Ken Alger
Treehouse TeacherKen Alger
Treehouse TeacherI forgot to mention that with these constructors you will need to alter your variable declarations you added/changed at the beginning of the Class. You should remove the
String type
statement and return theString mType
back to the default that was given with the challenge originally.Happy coding,
Ken