Search code examples
javaalgorithmcalculus

Algorithm mistake, finding areas between lines


I was doing my math hw, and it required me to calculate areas between 2 lines. I solved couple of them, I got them right(its online so it shows me whether i got it wrong or right), then I thought about writing a program that does the calculation for me, I created a nice algorithm, used java for the program, but in the end it didn't give me the right answer. I put the data from one of the questions I already solved whether i got it right or wrong.

Can you please tell me the mistake in the algorithm??

public class DailyHelper {

public static double f(double x) {
    double y = 5*x;
    return y;
}

public static double g(double x) {
    double y= 4*x*x;
    return y;
}

public static void main(String[] args) {
    double xLower = 0;
    double xHigher = 5/4;
    double areaF=0;
    double areaG=0;
    double change = (xHigher-xLower)/100000;
    for(double k=xLower; k<xHigher; k=k+change) {
        areaF = areaF+(change*f(k));
    }

    for(double k=xLower; k<xHigher; k=k+change) {
        areaG = areaG+(change*g(k));
    }
    double area = areaF-areaG;
    System.out.println(area);
}
}

Solution

  • Just a quick thought. Your xHigher variable is always 1, since you're dividing int by int. Try 5/4d