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 trialTinashe P Dzawara
2,759 Pointsthis fixed me help
The LandingActivity interacts with the Spaceship model object we created. Start by setting spaceship to a new Spaceship object in the onCreate() method. Use the custom constructor that takes a String parameter, and pass in "FIREFLY" as the parameter.
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);
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");
mTypeLabel.setText(mSpaceship.getType());
mPassengersField.setText(mSpaceship.getNumPassengers() + "");
}
public class Spaceship {
private String shipType;
private int numPassengers = 0;
public String getShipType() {
return shipType;
}
public void setShipType(String shipType) {
this.shipType = shipType;
}
public int getNumPassengers() {
return numPassengers;
}
public void setNumPassengers(int numPassengers) {
this.numPassengers = numPassengers;
}
public Spaceship() {
shipType = "SHUTTLE";
}
public Spaceship(String shipType) {
this.shipType = shipType;
}
public Spaceship(String type) { //<----You have to type a String inside the method
mType = type;
}
}
2 Answers
Steve Hunter
57,712 PointsHi,
I think your code is fine - just leave the variable names alone!
So first, reset the challenge back to the beginning and don't amend any of the code that is already there. That puts the variables back.
Then, for the first task, rather than using your line:
mSpaceship = new Spaceship("FIREFLY");
/// use this
spaceship = new Spaceship("FIREFLY");
Next, you need to use the getShipType()
method to set the TextView. Your code is close:
mTypeLabel.setText(mSpaceship.getType());
// should be
typeLabel.setText(spaceship.getShipType());
Lastly you need to change:
mPassengersField.setText(mSpaceship.getNumPassengers() + "");
// should be
passengersField.setText(spaceship.getNumPassengers() + "");
The main issue was you changing the variable names.
Steve.
Steve Hunter
57,712 PointsHi there,
You've changed the names of the member variables to include the old convention of preceding them with an m
. Whether you should do this or not is academic. The tests in this challenge are looking for a instance of Spaceship
called spaceship
. You've not called it that, so the challenge won't pass. You've renamed most of the member variables in both classes - the static tests can't cope with that.
I'd suggest resetting the challenge back to the beginning and starting again while leaving the existing code alone. Your code is mainly correct, apart from there's no method called getType()
, it's getShipType
.
If you don't know why your code fails the challenge, hit the preview button and you'll see the compiler output which should help.
Good luck!
Steve.
Tinashe P Dzawara
2,759 Pointsso can you help me to pass this challenge
Zach Freitag
20,341 PointsZach Freitag
20,341 PointsVery well explained Steve!