1

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?

3
  • cat is declared in the method start(), that means it is only available in the scope of that method. Declare it as a class attribute (like room) and you can use it in all methods of the class. Commented Aug 23, 2018 at 11:29
  • Why would calling move on dog also affect cat? Commented Aug 23, 2018 at 11:39
  • Probably knowing a bit of Object Refreneces in Java may help. Here is a post on StackOverflow where I have explained it: Two newly created objects seem to refer to the same address. Commented Aug 23, 2018 at 12:46

5 Answers 5

1

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");
    }

}
Sign up to request clarification or add additional context in comments.

5 Comments

Your solution worked well. Thanks! However, I am more interested in a fixing the scopes, because, well what if I had another "fourleg": "tiger"? for example.
You will have to think about a kind of storage for your objects, like an array or a list. Maybe you could change the class, so the class only represents a Fourleg. Then you could call on any Fourleg-instance the method move and pass the desired string and the object, you called the method at, will execute the method
Addition: Then you will need another class to manage and maintaint your Fourleg-Objects, so you would have for example a class FourlegStorage where you create Fourleg objects and call methods on them
@JosephKwong So you would pass FourLeg reference with tiger to this method. This solution is correct.
@JosephKwong check deHaar's answer for a detailed example and a clean split in objects. if you are just interested in understanding scopes and classes, my solution is fine for you.
1

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

Comments

0

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);
}


}

10 Comments

would that be like public Object cat;?
In your case, it should be Fourlegs cat = new Fourlegs();. You can define it as public if you want to but I would suggest to make it private a create a public getter method to access the value
Having cat as a field in FourLegs does not make a lot of sense from a design perspective (and it will cause a stack overflow because now you are recursively creating instances of FourLegs).
Your edit has fixed the stack overflow, but it still doesn't make sense as a design (why would FourLegs have another FourLegs...). The move shouldn't need to access cat, it just needs to modify the room field of FourLegs.
I agree. This was just an attempt to resolve the issue OP was facing.
|
0

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.

1 Comment

how does this come close to even answering the question at hand?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.