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 trialNicolas Haney
3,445 PointsDefault constructor not working.
public void setType() { mType = "SHUTTLE"; }
This code should work but is not acceptable. I cant figure out why.
public class Spaceship {
public String mType;
public String getType() {
return mType;
}
public void setType(String type) {
mType = type;
}
public void setType()
{
mType = "SHUTTLE";
}
}
3 Answers
orbyt
10,358 PointsOk so the challenge is asking you to create a constructor for the Spaceship
class without any parameters, and then setting mType inside it.
Try something like:
public Spaceship() {
mType = "SHUTTLE";
}
Grigorij Schleifer
10,365 PointsHi Nicolas,
in the first part of the challenge you should create a constructor:
public Spaceship() {
// constructors Name should be same as the class Name without a return type
// then two {}
mType = "SHUTTLE";
// here you initiate your mType variable with a String called "SHUTTLE"
}
The next Task is to create a second constructot that takes a String parameter which describes the Type or Name of the Spaceship:
public Spaceship(String shipType) {
// the constructor takes a String
mType = shipType;
// here you initialise mType to shipType
}
So if you wanΒ΄t fight against Aliens you will need many different ships/objects of the Spaceship class. So if you are creating a Spaceship obect somewhere else in your code. You can call your type of the ship this way.
Spaceship shipOne = new Spaceship("Destroyer");
Spaceship shipTwo = new Spaceship("Killer")
And so on ....
May the force be with you !
Grigorij
Prasoon Shukla
3,426 PointsIf you are writing a constructor, there is no need to have return type (here void). Also, keep the name of constructor same as Class. Then set the value of member variable equal to static value you want to pass. In case you are writing a constructor with a parameter, set the value of member variable equal to parameter you have passed in the constructor.
Grigorij Schleifer
10,365 PointsGrigorij Schleifer
10,365 PointsHey Nicolas,
could we help you?
Grigorij