I'm trying to figure out if I understand various java stuff by trying it out and seeing what happens (and then, inevitably, trying to figure out what went wrong). I guess I don't understand functions, because every attempt I've made has caused DrJava to balk.
The nearest-to-working-so-far one I'm trying to do now is
import java.awt.Color;
public class Functional {
int r;
int g;
int b;
public int re(int r){
r = (int)(Math.random() * (255 - 0)) + 0;
return r;
}
public int gr(int g){
g = (int)(Math.random() * (255 - 0)) + 0;
return g;
}
public int bl(int b){
b = (int)(Math.random() * (255 - 0)) + 0;
return b;
}
public void main(String[] args) {
StdDraw.setPenColor(new Color (r, b, g));
StdDraw.filledCircle(.5, .5, .6);
StdDraw.setPenColor( new Color (r, g, b));
StdDraw.filledCircle(.5, .5, .2);
}
}
It compiles, but when I try to run it java barfs.
EDIT: I don't know what the technical term is, the teacher just called it barfing when it spews red code over the screen.
EDIT 2: Still barfs with the statics added back in. new error message is
>java.lang.NullPointerException
>at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>at java.lang.reflect.Method.invoke(Unknown Source)
>at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
EDIT 3:
(Also , dont try to learn just like that if you havent got any serious programming background. >Things can get tricky tough.)
But... but the class is Intro to CS...
I don't understand what the difference is between a function and a method, or what "Create a new instance of the class" means if you're not starting an entirely new program.
Your main method is not static and it doesn't create an instance of your class Functional.
Try this:
You coud probably also make the methods
re()
,gr()
andbl()
static since they don't use any field of the class instance.What Java book are you using to learn?