Search code examples
javaabstract

How can I access these getPosition.x and getPosition.y values?


I've been stuck on this problem for a while now. It's for a school assignment, our lector provided us with a ForestSimulation.java file, that calls for a tree.getPosition().x, as seen here:tree.getPosition().x

We are told to make the methods that are needed, so I've made a Tree class aswell as a Birch class. enter image description hereenter image description here

Yet I feel that no matter how I declare the x and y-values I can't figure to make it work.

I've tried a non abstract getPosition, and calling this.x in the method body. I've tried (as in SS) to make it an abstract method. But nothing seems to work.

Please help <3


Solution

  • You are trying to match a call like

    tree.getPosition().x
    

    but the method you put into your Tree class is

    void getPosition() { ... }
    

    so this method will never return a point that has x and y coordinates. Change this method into

    Point getPosition() { ... }