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

Android Build an Interactive Story App (Retired) The Model-View-Controller Pattern Adding a Custom Constructor

Now 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.

Spaceship.java
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
STAFF
Ken Alger
Treehouse Teacher

Prasanna;

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

Ken Alger
Ken Alger
Treehouse Teacher

I 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 the String mType back to the default that was given with the challenge originally.

Happy coding,

Ken

Hello 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
Ken Alger
Treehouse Teacher

Prasanna;

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

Great. I will check them out.

Thanks.

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;
}

}

Cossy 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;
}
                       }