mrybak834
mrybak834

Reputation: 147

Simple Java Method won't run. Method inside method

Here is my code:

public class Project1mrybak3
{   
    public static void main (String[] args)   
    {
        System.out.println("Begin Java Exection");  
        System.out.println("");

        // put your Java Program here

        //choose picture

        String  picfile = FileChooser.pickAFile();  
        Picture pic     = new Picture(picfile);

        //Create turtle 1   
        Turtle t1 = new Turtle (pic);   
        t1.setPenWidth(10);

        //Create turtle 2  
        Turtle t2 = new Turtle (pic);

        flower(t1,200);

        //show picture    
        pic.show();

        System.out.println("");     
        System.out.println("End Java Exection");
    }//end of main class


    public static void flower (Turtle tparam, int x )     
    {     
        tparam.forward(x);     
    }

    public static void flowers ()       
    {      
        flower(t1,15);    
    }
} // end of Template class

So as you can tell, it doesn't make much sense but I have just started writing it. My question is, for the last method flowers, when I try to run it, it says that it cannot find the symbol variable t1. If I take out the parameters, it says that it requires parameters Turtle and int. Can I not put methods inside of methods?

Ultimately my goal is to create about 4 methods to draw parts of the flower, then put them all inside of one method, then inside my main code, I can call that method with turtle t1 and some x variable.

Thank you for any help and your time!

Upvotes: 1

Views: 7414

Answers (3)

IHazABone
IHazABone

Reputation: 525

You cannot define methods within methods, but you can call methods inside methods - such as to re-run a method in a loop with possibly differing parameters until a condition is met.

Upvotes: 0

Josh
Josh

Reputation: 1553

Like NickC said, you can't put methods inside of methods. Something else important to note here, though, is that t1 in your example isn't a method; it's an object (Method objects from the reflection API being a little beyond the scope of this question...). I don't mean to presume your design, but it seems like you might want your various flower-drawing methods inside a separate Turtle class, and you can have another method inside that class that calls all the helper methods in turn, like so:

public void drawFlower(int xPosition) {
    drawStem(xPosition);
    drawStamen(xPosition);
    drawPetals(xPosition);
    ...
}

That way, all of those helper methods have access to the Turtle that's (I think) doing the drawing (via the this keyword, as NickC pointed out), and they can return a modified xPosition if you want so that you can pass a different starting point into the next method.

Upvotes: 1

Nicole
Nicole

Reputation: 33197

No, you can't put methods inside of methods.

If you need to use multiple methods and it doesn't make sense to pass your variables as parameters, you should make the variables you need into fields (or members) of a class (read that link it will help). Move their declaration outside of the method they are local to (such as main) and into the scope of the class itself. Then, all methods in that class can refer to those variables:

  • With this. prefixed
  • Or just as they are, as long as there is no local variable with the same name.

Upvotes: 3

Related Questions