-1

I'm stuck at a part of my homework. Im not sure how transport the agent to another space.

Add a method usePortal to the Agent class that has no parameters and a void return type. This method gets the agent's location's portal, and checks to see if it's null.

• If the portal is not null, have the portal transport the agent using the portal's transport method. You will need to use the word this to refer to the agent that needs to be transported.

• If the portal is null, then don't do anything.

public class Agent {
    private Space _location;
    private String _name;

    public Space get_location() {
        return _location;
    }
    public void set_location(Space _location) {
        this._location = _location;
    }
    public String get_name() {
        return _name;
    }
    public void set_name(String _name) {
        this._name = _name;
    }

    public String toString(){
        return _name;
    }

    public String toStringLong(){
        return _name + " is in " + _location;
    }

    public void usePortal(){

        if(Portal.get_destination() == null){

        }else{

        }
    }
}


public class Portal {
    private static String _name;
    private static String _direction;
    private static Space _destination;


    public String get_name() {
        return _name;
    }

    public void set_name(String  _name) {
        Portal._name = _name;
    }

    public String get_direction() {
        return _direction;
    }

    public void set_direction(String _direction) {
        Portal._direction = _direction;
    }

    public static Space get_destination() {
        return _destination;
    }

    public  void set_destination(Space _destination) {
        Portal._destination = _destination;
    }


    public String toString(){
        return _name + " that goes " + _direction;
    }

    public String toStringLong(){
        return _name + " that goes " + _direction + " to " + _destination;  
    }

    public void transport(Agent student){
        student.set_location(_destination);
    }
}

public class Space {
    Portal p = new Portal();
    private String _name;
    private String _description;
    private Portal _portal;

    public String get_name() {
        return _name;
    }

    public void set_name(String _name) {
        this._name = _name;
    }

    public  String get_description() {
        return _description;

    }

    public void set_description(String _description) {
        this._description = _description;
    }

    public Portal get_portal() {
        return _portal;
    }

    public void set_portal(Portal _portal) {
        this._portal = _portal;
    }

    public String toString() {
        return _name;
    }

    public String toStringLong(){
        if (_portal != null){
        return _name + ": " + _description + " with a " + p.toStringLong();
        }
        return _name + ": " + _description;

    }
}
max
  • 96,212
  • 14
  • 104
  • 165

1 Answers1

0

Lists and Arrays are ZERO based in Java. The first element is 0 not 1.