Android Build an Interactive Story App The Model-View-Presenter Pattern Creating a Data Model

Finally add a setter method

what am i doing wrong

Spaceship.java
public class Spaceship{ public String shipType;

public String getShipType() { return shipType; }

public void setShipType() { this.shipType = shipType; }                      

 public void setType(String type){
  getShipType = type; 


  }

}

2 Answers

Susanne Wagner
Susanne Wagner
16,579 Points

This Class has no contstructor which initialize the class and its attributes when creating an instanze of this class. Further I think class attributes like the String shipType should be private to achieve encapsulation. That is why you need a getter and a setter method to work with the attribute.

I think the getter should be public String getShipType() { return this.shipType; }

setShipType method should have a parameter and reference the class attribute shipType with this.shipType. public void setShipType(shipType){ this.shipType = shipType; }

Why do you have a second setter method? The first one is setting the attribute shipType within the class. You just have one attribute within the Spaceship Class, therefore usually you just need one setter method.

thank you it worked