trying and failing to do java functions

697 Views Asked by At

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.

3

There are 3 best solutions below

2
On

Your main method is not static and it doesn't create an instance of your class Functional.

Try this:

import java.awt.Color;

public class Functional { 
  int r;
  int g;
  int b;

  public int re(){
    return (int)(Math.random() * (255 - 0)) + 0;
  }

  public int gr(){
    return (int)(Math.random() * (255 - 0)) + 0;
  }
  public int bl(){
    return (int)(Math.random() * (255 - 0)) + 0;
  }
  public static void main(String[] args) { 

     Functional f = new Functional();

     f.r = f.re();
     f.g = f.gr();
     f.b = f.bl();

    StdDraw.setPenColor(new Color (f.r, f.b, f.g));
    StdDraw.filledCircle(.5, .5, .6);
    StdDraw.setPenColor( new Color (f.r, f.g, f.g));
    StdDraw.filledCircle(.5, .5, .2);

  }  
}

You coud probably also make the methods re(), gr() and bl() static since they don't use any field of the class instance.

What Java book are you using to learn?

0
On

Make the main method static. Create a new instance of the class. (You should have moved the stuff inside the main function in another method, yes its a method , not a function. Also , dont try to learn just like that if you havent got any serious programming background. Things can get tricky tough.) Remember , Google and Documentation are your friends.

0
On

As mwhs said, your main method is not static, and your functions for generating the colors are never called. Meaning your values are null. As a tip, you don't have to include the variable name in the constructor.