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

Sean Walker
Sean Walker
1,792 Points

shipType!!!!! last question...adding setter method!

hey everyone! the console keeps giving me syntax errors and i've no idea what i'm doing wrong! help pls!

Spaceship.java
public class Spaceship {

  public String shipType;

  public String getShipType() {
        return shipType;
       public String setShipType(shipType) {
        this.shipType = shipType;
    }


}
Sean Walker
Sean Walker
1,792 Points

i've also tried

public String setShipType(String shipType) { }

3 Answers

andren
andren
28,469 Points

There are a couple of issues:

  1. You have declared the setShipType method inside the getShipType method. You can't place a method inside of another method like that in Java.

  2. The method is missing a closing } bracket

  3. You have defined the return type as String, but a setter method does not actually return any value, so the return type should be set as void.

  4. You are missing the type of the shipType parameter, which should be String.

If you fix those issues like this:

public class Spaceship {

  public String shipType;

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

Then your code will work.

Hey Sean, try the code below:

public class Spaceship {
   public String mType;
   public String shipType;

 public String getType() {
   return mType;
 }

 public String getShipType(){ 
   return shipType; 
 }

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

 public void setShipType(String ShipType) {
   shipType = ShipType;
  }
}
Igor Gunger
Igor Gunger
8,046 Points

Exactly, you need to use a void type instead of String one in your set method. Try to make your member variable private.