-1

I'm trying to call a method from a different class but I get "Cannot find Symbol" error

I've been trying different variations of declaring variables but my understanding is rather poor. The class that I am calling from (Lawn) compiles and runs correctly. The method setLawnWidth exists and works in the class Lawn.

public class sqrFootCalculator
{
    private double pricePerSquareFoot;
    private Lawn lawn;

    /**
     * Constructor for objects of class sqrFootCalculator
     */
    public sqrFootCalculator(Lawn lawn)
    {

        setLawnWidth(lawn);

    }

My first class Lawn, does have a method called setLawnWidth, that sets the variable lawnWidth correctly. I'm just unclear on how to call it from an external class. I am setting each variable through it own method which is why nothing is currently written for pricePerSquareFoot.

kill9all
  • 101
  • 1
  • 8
  • Sorry, it should say setLawnWidth(lawn) not room. – kill9all Jun 06 '19 at 21:29
  • 1
    Then [edit] your question. Don't leave bad code up there – Hovercraft Full Of Eels Jun 06 '19 at 21:29
  • You're not calling `setLawnWidth()` on the Lawn object for starters. So even if that class has the method, you're not calling it on an object of that class, but rather are calling it on the current sqrFootCalculator object, the `this`, and seeing that trying to do this is not correct. – Hovercraft Full Of Eels Jun 06 '19 at 21:30
  • But having said that, it doesn't even look like you would *want* to call `setLawnWidth()` in this situation. Isn't this method supposed to **get** the width of the Lawn object passed in, and then use it for its calculations? – Hovercraft Full Of Eels Jun 06 '19 at 21:34
  • Many other problems with that code, including having a method that has the exact same name as the class, almost a "pseudo"-constructor, starting the class name with a lower-case letter, not actually doing any calculations in a method that states that it should calculate something, not having this same calculating method return a value, the result of the calculation... I fear that you may not be fully understanding your assignment instructions. – Hovercraft Full Of Eels Jun 06 '19 at 21:36
  • Yes, it **is** a pseudo-constructor because your comments to the method state that it should be a constructor -- get rid of the void, and make it a full-fledged constructor. – Hovercraft Full Of Eels Jun 06 '19 at 21:37
  • Thanks for the edit tip, I wasn't clear on how to do that. This is for a homework assignment for Level 1 programming so it's supposed to be a simple exercise for how to call internal and external methods. The Lawn class just contains a constructor and methods for setting and getting the variables for the lawn size. The class I'm working on is supposed to calculate the two but as I said, I'm having issues getting it to call the external class. Apologies if I'm not clear on the terminology, I am VERY new. – kill9all Jun 06 '19 at 21:39
  • I removed the void (I was experimenting). The error I get states: "Cannot find symbol - setLawnWidth (Lawn). I assume this mean that it is unable to locate or access my class Lawn. Does this sound accurate? – kill9all Jun 06 '19 at 21:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194561/discussion-between-kill9all-and-hovercraft-full-of-eels). – kill9all Jun 06 '19 at 21:56
  • No, your assessment is not correct. Again, you're not calling the method on the Lawn object but instead are trying to pass the Lawn object into the method via its parameters. These errors are with Java at its most basic -- creating constructors, calling methods off of instance, and suggest that a decent review of your text or the [Java Tutorials](https://docs.oracle.com/javase/tutorial/java/index.html) could help you. – Hovercraft Full Of Eels Jun 06 '19 at 22:04

1 Answers1

0

Use lawn.setLawnWidth(...) not just setLawnWidth. You use that for methods in the same class

Patrick
  • 552
  • 5
  • 17