Hi there,

What i'm trying to accomplish is:

These two methods:

f(x : double) : double
post: return x

integral(og : double, bg : double, step : double) : double
post : return (f(og) + f(og+stap) + … + f(bg)) * step

So I can call those two in my main to calculate the value of the integral of og=0.0, bg=5.0 step=1.0

What I have now is:

public class Opdracht3_1{
        public static double f(double x){
          return x;
        }

        public static double integraal(double og, double bg, double stap) {
          for(double x = og; x <= bg; x = x + stap) {
               double cal = f(og);
               og = og+stap;
               return cal;
          }
        }
        public static void main(String[] args) {
        double og = 0.0; double bg = 5.0; double stap = 1.0;
                double cal = integraal(og,bg,stap);
        System.out.println(cal);
        }
}

The compiler gives the next error:

java:12: missing return statement
}

Any help is appreciated.

in your integraal method, let's say that the og you pass is 5, and bg is 3. your return statement will never be reached.
that's the problem. also, what's the use of putting a return statement inside a loop? you won't return several values, if that's what you're trying to do.

Well, og means underborder and bg means topborder. Therefore og is always smaller than bg. The return statement in the for loop is a mistake as you pointed out. However I get the next error when getting the return statement outside the for loop.

cannot find symbol: return cal;

You declare cal inside the loop. A Java variable's scope is from the immediately preceeding { to the immediately following }, so cal is out of scope after you exit the loop. You could declare (and initialise) it before entering the loop

and as for og being always smaller than bg...
you know that, but the JVM doesn't.
well, actually... neither do you. you don't intend the method to be used with og being larger than bg, but that doesn't mean it can't be used as such.

Well I got it to this:

        public static double integraal(double og, double bg, double stap) {
          double cal = 0;
          for(double x = og; x <= bg; x = x + stap) {
               x = x+(x+stap);
               cal = f(x)*stap;
          }
          return cal;
        }

No errors but it doesnt print me the right numbers.
How do I actually get the forloop to return me:
(f(og) + f(og+stap) + … + f(bg)) * step

You sem to be doing
x = x + stap
in the loop control, then another
x = x+(x+stap)
inside the loop, by which time you have added stap twice and x once

f(x : double) : double
post: return x

integral(og : double, bg : double, step : double) : double
post : return (f(og) + f(og+stap) + … + f(bg)) * step

Your function definition doesn't really make much sense. The f(x) function does nothing but returns the same value as the argument... The integral() function simply adds all increments of the og and stap up to bg and then multiple with stap again.

Anyway, your loop doesn't do it correctly; especially, you keep multiplying stap with the addition. What you need to do is to separate the way you compute og and stap addition from multiplying with stap again. The first part, you need to look at James comment about why your summation is wrong. Then the second part is that you need to take the multiplication line out of the loop.

PS: I am not so sure about why your integral() function definition looks like that anyway... Just odd...

It's an integration, which in general look like this:

integral = 0
for x = lower to upper by step size
   integral += f(x) * step size
return integral

Think of f(x) as defining a curve, the integral is the area under the curve between lower and upper. It divides it into vertical slices of width step size, treats each as a rectangle, and adds up all the areas. The smaller the step size the more accurate the area calculation is.

Thanks James. Actually, after I have a thought, it is a simplification equation of the summation. Though, the f(x) function still doesn't make much sense when it does nothing but return the argument value...

True the f(x) doesn't do much but I don't make these exercises hehe.
Anyways I got it to work.

Thanks all!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.