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) Finishing the User Interface Using a Model in the Controller

Constructors

I dont know how to answer this question using constructors

LandingActivity.java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class LandingActivity extends Activity {

    public Button mThrustButton;
    public TextView mTypeLabel;
    public EditText mPassengersField;

    public Spaceship mSpaceship;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_landing);

        Spaceship mSpaceship=new Spaceship();
        mThrustButton = (Button)findViewById(R.id.thrustButton);
        mTypeLabel = (TextView)findViewById(R.id.typeTextView);
        mPassengersField = (EditText)findViewById(R.id.passengersEditText);

        // Add your code here!
        public LandingActivity(String x){
              ;
        }
    }
}
Spaceship.java
public class Spaceship {
    private String mType;
    private int mNumPassengers = 0;

    public String getType() {
      return mType;
    }

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

    public int getNumPassengers() {
      return mNumPassengers;
    }

    public void setNumPassengers(int numPassengers) {
      mNumPassengers = numPassengers;
    }

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

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

1 Answer

After Task 1 your onCreate() function should look like this:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_landing);

        mThrustButton = (Button)findViewById(R.id.thrustButton);
        mTypeLabel = (TextView)findViewById(R.id.typeTextView);
        mPassengersField = (EditText)findViewById(R.id.passengersEditText);

        // Add your code here!
        mSpaceship = new Spaceship("FIREFLY");

    }

You have this, and I've marked the changes you need to make with comments:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_landing);

        Spaceship mSpaceship=new Spaceship();  //delete this
        mThrustButton = (Button)findViewById(R.id.thrustButton);
        mTypeLabel = (TextView)findViewById(R.id.typeTextView);
        mPassengersField = (EditText)findViewById(R.id.passengersEditText);

        // Add your code here!
        public LandingActivity(String x){  //replace this
              ;
        }
    }

When they say "Use the custom constructor that takes a String parameter, and pass in "FIREFLY" as the parameter." what they want you to do is create a Spaceship object and assign it to the mSpaceship variable.

Constructors are like methods, but they have no return value, and must be named the same (exactly) as their class. So the constructor for the Spaceship class would be Spaceship(). To create an object you must use the keyword new in front of the constructor. And if the constructor takes any parameters, you must provide them. In this case the constructor requires a String, so we are passing in "FIREFLY".