Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: tutorial

  1. #1
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default tutorial

    does anyone know any good tutorials for methods? Or could someone explain how to create and call methods? Either one would be really helpful. Thanks

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    create a method:
    Java Code:
    public class Exercise{
    
    private int number;
    
    // Constructor
    public Exercise(){
    
    // initialise member field
    number=0;
    }
    
    // method that gets the number 
    public int getNumber(){
    return number;
    }
    }
    call method:
    Java Code:
    public static void main(String args[]){
    Exercise ex=new Exercise();
    // call method of Exercise class
    System.out.println(ex.getNumber()); // prints 0 to the screen
    why does tab work horribly when writing code?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by pizzadude223 View Post
    does anyone know any good tutorials for methods? Or could someone explain how to create and call methods? Either one would be really helpful. Thanks
    Didn't you check it on the Suns' tutorial?

    Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Creating Objects (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    why does tab work horribly when writing code?
    Use four spaces (as code tags preserve spaces) or copy-paste a tab from notepad or something... the forum does not support the use of the tab button in the textarea.

    @ OP: Google is a great resource: java method creation tutorial - Google Search -- the first result is quite promising, as are others listed there. And as Eranga mentioned, Sun's online documentation is fantastic for this kind of information.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Zack View Post
    Use four spaces (as code tags preserve spaces) or copy-paste a tab from notepad or something... the forum does not support the use of the tab button in the textarea.
    Yes it is. My suggestion is if anyone wants to post a code segment (may be commenting on a question), if you are lazy to open your IDE and write the code use a simple text editor like NP++. That's what I'm doing in most of the cases. It works actually.

  6. #6
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    So what if I wanted to call a method that would draw an oval or something. I tried this but it gets errors when it compiles:

    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    public class DrawTest extends Applet implements Runnable//is not abstract and does not 
    //override abstract method run() in java.lang .Runnable error
    {
    	public class Exercise
    	{	
    		// Constructor
    		public Exercise(Graphics g)
    		{
    			g.setColor  (Color.red);
    			g.fillOval (50, 50, 20, 20);
    		}
    	}
    
    	public static void main(String args[])
    	{
    		Exercise ex=new Exercise(); //non static variable, cannot be referred 
     //by static context error
    		ex(); //can't find symbol error
    	}
    }

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Can you post the complete error you ends up with? Methods invoking is not depends on what you are really doing within it.

  8. #8
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    Java Code:
    BallDraw.java:4: BallDraw is not abstract and does not override abstract method run() in java.lang.Runnable
    public class BallDraw extends Applet implements Runnable
             ^
    BallDraw.java:12: inner classes cannot have static declartions
    public static void draw(Graphics g)
                            ^
    BallDraw.java:32: draw(java.awt.Graphics) in BallDraw.image cannot be applied to ()
    image.draw();
           ^
    3 errors
    That is exactly how I see it.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    It really helps if the classnames and code you post here match those in the errors you post.

    Anyway, you are iplementing Runnable, which means you need to implement the methods that Runnable declares.

    As for the rest, without the code that goes with these errors we can't say.

  10. #10
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    Oh, your right it is the wrong code, the files are next to each other and i got mixed up, here is the real one:

    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    public class BallDraw extends Applet implements Runnable
    {
    
    	private Image dbImage;
    	private Graphics dbg;
    	
    	public class image
    	{
    		public void draw(Graphics g)
    		{
    			g.setColor  (Color.red);
    			g.fillOval (50, 50, 20, 20);
    		}
    	}
    
    	public void init()
    	{
    		setBackground (Color.blue);
    	}
    
    	public void start ()
    	{
    		Thread th = new Thread (this);
    		th.start ();
    	}
    
    	public static void main(String[] args) 
    	{
    		image.draw();
    	}
    }

  11. #11
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default Your code requires the "run()" method

    This compiler error is one you should get used to
    seeing whenever your code "implements an interface."

    BallDraw.java:4: BallDraw is not abstract and does not override abstract method run() in java.lang.Runnable
    public class BallDraw extends Applet implements Runnable
    ^
    Your code implements the interface "Runnable".

    Classes that implement interfaces require that
    you add specific methods to them.

    In your case, the "Runnable interface" requires
    that you add to your BallDraw.class the
    following method:

    Java Code:
      public void run() {}
    You can begin by copying the above method exactly
    as it is. This should eliminate that one compiler error.

    Expanding on this error further, you see that
    the compiler states that:

    "BallDraw.java:4: BallDraw is not abstract.."

    "Not being abstract" is another issue that the
    compiler speculates could be a problem with
    your BallDraw.class. In your case, not being
    abstract is not the problem. Abstract classes
    is a subject beyond your level, and no
    programmers will disagree that adding the
    run() method to this class is the solution
    to this compiler error.

  12. #12
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    sweet, that helped alot. But I still have one error that I don't know how I would fix. Its the one that says:

    Java Code:
    BallDraw.java:32: draw(java.awt.Graphics) in BallDraw.image cannot be applied to ()
    image.draw();
           ^
    Anyone know how to fix this?

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    I would suggest, since you're still learning, not putting multiple classes in a single file, or having inner classes (which is what image is). I suspect you'll only get confused by the rules associated with that.

    Next, are you running this as an Applet (isn't JApplet recommended these days?)? I'm not entirely sure why you have it as a Runnable...

    On a naming thing, class names should begi with a capital letter. Attributes and variables begin with lower case.

  14. #14
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    Okay I cleaned it up to this:
    Java Code:
    import java.applet.*;
    import java.awt.*;
    
    public class BallDraw extends Applet
    {
    
    	private Image dbImage;
    	private Graphics dbg;
    	
    	public void run() {}
    	
    	public void Draw(Graphics g)
    	{
    			g.setColor  (Color.red);
    			g.fillOval (50, 50, 20, 20);
    	}
    
    	public void init()
    	{
    		setBackground (Color.blue);
    	}
    
    
    
    	public static void main(String[] args) 
    	{
    		Draw();
    	}
    }
    but I still have this error:
    Java Code:
    BallDraw.java:27: Draw(java.awt.Graphics) in BallDraw cannot be applied to ()
    Draw();
    ^
    So how would I fix this and still be able to put shapes and objects on the screen?

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by pizzadude223 View Post
    but I still have this error:
    Java Code:
    BallDraw.java:27: Draw(java.awt.Graphics) in BallDraw cannot be applied to ()
    Draw();
    ^
    So how would I fix this and still be able to put shapes and objects on the screen?
    Your Draw( ... ) method takes one parameter of type 'Graphics' but you are calling it with no parameters at all; the javac compiler doesn't like that and whines at you.

    kind regards,

    Jos

  16. #16
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    I tried putting "graphics" and "graphics g" in the parameter when I call it, but it didn't work. So How would I fix it? Do I need the graphics g in the method?

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by pizzadude223 View Post
    I tried putting "graphics" and "graphics g" in the parameter when I call it, but it didn't work. So How would I fix it? Do I need the graphics g in the method?
    You need to pass it a thing of type 'Graphics' when you call it; it is not a notational issue; compare this with, say, the Math.abs( ... ) method; it needs a number as a parameter, you can't call it as Math.abs() (no number).

    kind regards,

    Jos

  18. #18
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    Now I tried this:

    Draw(fillOval (50, 50, 20, 20));

    but it says cannot find symbol for fill oval. Am I supposed to be sending something different?

  19. #19
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by pizzadude223 View Post
    Now I tried this:

    Draw(fillOval (50, 50, 20, 20));

    but it says cannot find symbol for fill oval. Am I supposed to be sending something different?
    Don't just guess; there is no method named 'fillOval' in your class.

    kind regards,

    Jos

  20. #20
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    Well what else can I do if I can't get it and no one else will tell me? Thats all there's left to do. Ugh, some frustrated with java.

Page 1 of 2 12 LastLast

Similar Threads

  1. Tutorial
    By shivam0101 in forum NetBeans
    Replies: 2
    Last Post: 03-15-2010, 06:18 PM
  2. Need help with my tutorial
    By annna in forum New To Java
    Replies: 2
    Last Post: 02-05-2010, 04:00 PM
  3. Tutorial help LAST QNS
    By annna in forum New To Java
    Replies: 2
    Last Post: 01-28-2010, 01:22 PM
  4. JSF Tutorial
    By teststest in forum JavaServer Faces (JSF)
    Replies: 9
    Last Post: 11-15-2009, 06:31 AM
  5. tutorial help
    By yuriythebest in forum Java Applets
    Replies: 1
    Last Post: 11-12-2008, 05:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •