Search code examples
javabluej

Java code is not executing instance method


I am using the BlueJ IDE. I have a main class entitled ProgramOne, and another class StarTurtle (intended to serve an instance method).

Here is the code of ProgramOne:

public class ProgramOne
{
     public static void main (String[ ] args)  
     {  
          StarTurtle turtle1 = new StarTurtle(5);
          turtle1.StartTurtle();

        }
    }

Here is the code of StarTurtle:

public class StarTurtle 
{ 

private int points;      

public int StartTurtle(int x) 
{ 
points = x; 

Turtle sue;
sue = new Turtle();
sue.paint (90, 40); 

} 

} 

(The turtle method you see is from two other classes that I have not pasted here for the sake of brevity. These classes are found in the http://www.cs.ccsu.edu/~jones/book.htm manual)

The code only compiles, and there is no option to execute. However, there is no option to execute void main (String[ ] args), which there should be to execute the main class. Does anyone know what is the cause of this? I am assuming that there is a problem in the code itself.

Edit: When I mean "option to execute", I am referring to this BlueJ functionality:

BlueJ

Edit: Changing the code in the manner which Titus and Redge described (in the answers and comments of answers) fixed the StarTurtle class, but the main class ProgramOne still does not execute.


Solution

  • So ... I was previously unfamiliar with BlueJ but I just downloaded it and created the code that we have all been suggesting. It now looks like this.

    ProgramOne.java

    public class ProgramOne
    {
         public static void main (String[ ] args)  
         {  
              StarTurtle turtle1 = new StarTurtle();
              turtle1.StartTurtle(5);
    
         }
    }
    

    StarTurtle.java

    public class StarTurtle 
    { 
    
        private int points;      
    
        public void StartTurtle(int x) 
        { 
            points = x; 
    
            Turtle sue;
            sue = new Turtle();
            sue.paint (90, 40); 
    
        } 
    
    } 
    

    With this code the context menu looks exactly like the one in the original question:

    enter image description here

    The menu item that reads void main(String[] args) pops up a dialog as follows :

    dialog

    If I hit the OK button I seem to get a window called "Turtle Drawings" that contains a vertical line.

    I changed the StartTurtle(int) method as follows:

        public void StartTurtle(int x) 
        { 
            points = x; 
    
            Turtle sue;
            sue = new Turtle(true, 200,90);
            sue.sleep(1000);
            sue.say("Something");
            sue.sleep(1000);
            sue.paint (90, 40); 
            sue.sleep(1000);
            sue.fillCircle(5);
            sue.sleep(1000);
            sue.fillBox(10, 20);
    
        } 
    
    } 
    

    This modified code displays the message, a vertical line, a circle and then a rectangle at 1 second intervals.

    I can only conclude from this that the code is now valid and that you should execute it by chosing the menu item that reads void main(String{} args).