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
Printable View
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
create a method:
call method: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;
}
}
why does tab work horribly when writing code?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
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.
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:
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
}
}
Can you post the complete error you ends up with? Methods invoking is not depends on what you are really doing within it.
That is exactly how I see it.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
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.
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:
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();
}
}
This compiler error is one you should get used to
seeing whenever your code "implements an interface."
Your code implements the interface "Runnable".Quote:
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
^
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:
You can begin by copying the above method exactlyCode:public void run() {}
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.
sweet, that helped alot. But I still have one error that I don't know how I would fix. Its the one that says:
Anyone know how to fix this?Code:BallDraw.java:32: draw(java.awt.Graphics) in BallDraw.image cannot be applied to ()
image.draw();
^
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.
Okay I cleaned it up to this:
but I still have this error: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();
}
}
So how would I fix this and still be able to put shapes and objects on the screen?Code:BallDraw.java:27: Draw(java.awt.Graphics) in BallDraw cannot be applied to ()
Draw();
^
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?
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?
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.