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

Adam Duffield
Adam Duffield
30,494 Points

Now add a second construct that takes String as parameter task...

Hi,

I generally have no idea what I am doing wrong here, I'm no expert in java but I've been getting the ropes quite simply but nothing I appear to put for this answer is working for me. I've looked at a few other forum answers for this task and I've noticed the answers focus on one snippet of code and not the whole picture, not sure about others but this confuses me due to not knowing where the rest of the code is. I read that constructs should have the same name as their class so in the other forums when referring to "public class spaceship e.c.t." I'm lost whether they mean the class or the construct.

I've attached my code to this post and I'm on task 2 where I need to created a 2nd construct with a String parameter to update mType to a different spaceship value.

Many thanks,

Adam

Spaceship.java
public class Spaceship {
  public String mType = "SHUTTLE";

  public Spaceship(String type){
     mType = type; 
  }

  public String getType() {
    return mType;
  }

  public void setType(String type) {
    mType = type;
  }
}

1 Answer

Hi Adam,

For this challenge you are creating two constructors. The first sets the mType variable to the value "SHUTTLE". You've not done that - you have initialised the variable directly.

So, for taslk one you need to create the constructor to set the value as above. That would look like:

public Spaceship() {
    mType = "SHUTTLE";
}

The next task is to create a second constructor that takes a value and assigns it to mType. You've done that correctly in your snippet.

You will end up with two methods called Spaceship in your class. One takes no parameters, as above, and the other is like the one you have written, taking a string parameter.

I hope that clears things up a bit for you.

Steve.

Adam Duffield
Adam Duffield
30,494 Points

Perfect! Cheers for that Steve, definitely makes things a lot clearer and I hope this helps others out too. I did initially try to make that 1st construct but it wasn't working for me so I started trying anything and everything but that makes much more sense now. For the record... the "attach your code" feature when asking a question is awesome, like a few newcomers here I found inputting my code very confusing at first but you've nailed it now. ;)