Joseph Kwong
Joseph Kwong

Reputation: 15

change object variable from another object's method

This probably is very newbie but I have tried to find an answer for a while now but I could not.

package playground.space;

public class Fourlegs {
    String room;

    public static void main(String[] args) {
        Fourlegs program = new Fourlegs();

        program.start();
    }

    public void start() {
        Fourlegs cat = new Fourlegs();
        cat.room = "office";
        Fourlegs dog = new Fourlegs();
        dog.room = "office";

        //dog moves to the carpark, and the cat follows the dog
        dog.move("carpark");
    }

    public void move(String i) {
        this.room = i;

        //cat cannot be resolved to a variable
        cat.room = this.room; //the cat's room will be the same as the dog's room.
        System.out.println("the cat is in the " + cat.room);
    }
}

I get the error: cat cannot be resolved to a variable.(obviously).

How can I manipulate "cat" from another method?

Upvotes: 1

Views: 2607

Answers (5)

deHaar
deHaar

Reputation: 18588

I think this is not a task to be solved in a single class. From an object-oriented perspective (which should be taken when programming in Java), you are in need of at least 3 classes, which are Location, FourLeggedAnimal and a main class, let's say FourLeggedMain:

An animal should look like this when it is named and in a location:

package fourlegs;

public class FourLeggedAnimal {

    protected String name;
    protected Location location;

    public FourLeggedAnimal(String name, Location room) {
        this.name = name;
        this.location = room;
    }

    public Location getLocation() {
        return location;
    }

    public void follow(FourLeggedAnimal animal) {
        this.location = animal.getLocation();
    }

    public void moveTo(Location room) {
        this.location = room;
    }

    public String getCurrentLocation() {
        return location.getName();
    }
}

The location just needs a name:

package fourlegs;

public class Location {

    private String name;

    public Location(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

And the main performs the logic including the other objects:

package fourlegs;

public class FourLegsMain {

    public static void main(String[] args) {
        Location office = new Location("office");
        Location carpark = new Location("carpark");

        FourLeggedAnimal cat = new FourLeggedAnimal("cat", office);
        FourLeggedAnimal dog = new FourLeggedAnimal("dog", office);

        System.out.println("The cat is at the " + cat.getCurrentLocation());
        System.out.println("The dog is at the " + dog.getCurrentLocation());

        dog.moveTo(carpark);

        System.out.println("The dog went to the " + dog.getCurrentLocation());
        System.out.println("The cat is still at the " + cat.getCurrentLocation());

        cat.follow(dog);

        System.out.println("The cat followed the dog and is at the "
                + cat.getCurrentLocation()
                + " now");
    }
}

Executing it will provide the following output:

The cat is at the office
The dog is at the office
The dog went to the carpark
The cat is still at the office
The cat followed the dog and is at the carpark now

Upvotes: 1

prasad_
prasad_

Reputation: 14317

Here is my attempt. I made some changes to the posted code:

public class Fourlegs {

    String room;
    private String id; // to identify a dog, cat, etc.

    public static void main(String[] args) {
        Fourlegs program = new Fourlegs();
        program.start();
    }

    public void setId(String id) {
        this.id = id;
    }
    public String getId() {
        return this.id;
    }   

    public void start() {
        Fourlegs cat = new Fourlegs();
        cat.setId("Cat-Fluffy");
        cat.room = "office";
        Fourlegs dog = new Fourlegs();
        dog.setId("Dog-Toby");
        dog.room = "office";
        System.out.println(dog.getId() + " is in the " + dog.room);
        System.out.println(cat.getId() + " is in the " + cat.room);
        // dog moves to the carpark, and the cat follows the dog
        dog.move("carpark");
        // the cat's follows the dog.
        cat.move(dog.room);
    }

    public void move(String i) {
        this.room = i;
        System.out.println(this.getId() + " moved " + this.room);
    }
}

The output:

Dog-Toby is in the office
Cat-Fluffy is in the office
Dog-Toby moved to carpark
Cat-Fluffy moved to carpark

Upvotes: 0

ItFreak
ItFreak

Reputation: 2369

You are trying to access the variable outside it's scope, the variable cart only exists within the method start.
You have to pass the object you want to work on to this method:

public void move(String i, Fourlegs fourleg) {
  fourleg.room = this.room
}

Now you can call methods on any instance of Fourlegs

EDIT: new approach:

public class Fourlegs {
    String room;

    public void move(String i) {
        this.room = i;
        //kind of unnecesary:)
        this.room = this.room;
    }
}


public class FourlegStorage {
 private List<Fourleg> fourlegs = new ArrayList<>();
    public void start() {
        Fourlegs cat = new Fourlegs();
        fourlegs.add(cat);
        cat.room = "office";
        Fourlegs dog = new Fourlegs();
        fourlegs.add(dog);
        dog.room = "office";

        //dog moves to the carpark, and the cat follows the dog
        dog.move("carpark");
    }

}

Upvotes: 1

Ashishkumar Singh
Ashishkumar Singh

Reputation: 3600

You have defined object cat in start method and you are using it in method move.

When you define a variable inside a method, it's scope is limited to that method only. To use it in a different method inside the same class, you should define the variable at class level and the error should go away.

Define the variable at class level to resolve the error

package playground.space;

public class Fourlegs {
String room;
Fourlegs cat; // global variable here

public static void main(String[] args) {
    Fourlegs program = new Fourlegs();

    program.start();
}

public void start() {
    Fourlegs cat = new Fourlegs();
    cat.room = "office";
    Fourlegs dog = new Fourlegs();
    dog.room = "office";
    dog.cat = cat; // global variable set here

    //dog moves to the carpark, and the cat follows the dog
    dog.cat = new Fourlegs();
    dog.move("carpark");
}

public void move(String i) {
    this.room = i;

    cat.room = this.room; //the cat's room will be the same as the dog's room.
    System.out.println("the cat is in the " + cat.room);
}


}

Upvotes: 0

chris
chris

Reputation: 1785

I would do it this way:

package playground.space;

public class Fourlegs {

    String room;

    public static void main(String[] args) {
        Fourlegs program = new Fourlegs();

        program.start();
    }

    public void start() {
        Fourlegs cat = new Fourlegs();
        cat.room = "office";
        Fourlegs dog = new Fourlegs();
        dog.room = "office";

        //dog moves to the carpark, and the cat follows the dog
        dog.move("carpark");
        cat.follow(dog);
        System.out.println("the cat is in the " + cat.room);
    }

    public void follow(Fourlegs other) {
        room = other.room;
    }

    public void move(String newRoom) {
        this.room = newRoom;
    }
}

I added a method follow to let every Fourleg follow another Fourleg. Maybe that's a little bit more object oriented.

Upvotes: 0

Related Questions