-1

I am trying to create an integral calculator that displays a random polynomial, a random interval, and the definite integral of that polynomial of the random interval.

I previously wrote the code with the end goal of displaying the answer on a JApplet, but now after a few complications, I now need to display my answer on JFrame.

The following is my Integral class

package poopeep;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class Integral
{
    public Integral()
    {
        generator = new Random();

        degree = generator.nextInt(5)+1;

        left = generator.nextInt(11);
        right = generator.nextInt(11) + left + 1;

        xlist = new ArrayList<Integer>();
    }

    public double calc()
    {
        double interval = right - left;
        double dx = interval / 10000000;
        double area = 0;

        // Generates random coefficients
        for (int d = 0; d <= degree; d++)
        {
            int coeff = generator.nextInt(21) - 10;
            xlist.add(coeff);
        }

        for (double i = left; i < right; i += dx)
        {               
            double x = i + dx/2;
            for (int d = degree; d >= 0; d--)
            {
                double areaOfSingleRect = (xlist.get(d) * Math.pow(x, d)) * dx;
                area += areaOfSingleRect;
            }
        }

        DecimalFormat round = new DecimalFormat("##.##");
        round.setRoundingMode(RoundingMode.DOWN);
        return Double.parseDouble(round.format(area));
    }

    public String getEquation()
    {
        String equation = "";
        for (int d = degree; d >= 0; d--)
        {
            equation = equation + String.valueOf(xlist.get(d)) + "x^" + String.valueOf(d) + " + ";
        }
        return equation;
    }

    public String getLeft()
    {
        String leftString = String.valueOf(left);
        return leftString;
    }

    public String getRight()
    {
        String rightString = String.valueOf(right);
        return rightString;
    }

    private int left;
    private int right;
    private int degree;
    private Random generator;
    private List<Integer> xlist;
}

The following is my test class

package poopeep;

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JApplet;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class IntegralTest extends JApplet
{
    public static void main(String[] args) 
    {
        Integral myIntegral = new Integral();

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600,600);
        frame.setTitle("Integral");
        frame.setVisible(true);

        JPanel panel = new JPanel();

    //  JLabel bounds = new JLabel("From " + myIntegral.getLeft() + " to " + myIntegral.getRight());
    //  JLabel answer = new JLabel(String.valueOf(myIntegral.calc()));
        JLabel equation = new JLabel(myIntegral.getEquation());

    //  panel.add(bounds);
    //  panel.add(answer);
        panel.add(equation);

        frame.add(panel);


    }
}

Sorry for the messy code, unused imports, etc.

In my test class, I basically commented out the JFrame code and left the JApplet code running. When I run the program with the JApplet code, the program works perfectly how I want it to.

However, when I switch over to using the JFrame code (and I extend JComponent in Integral class, etc.) Eclipse gives me an error saying that my list indexes, line 59, are out of order, even though I changed nothing in the Integral class.

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 0
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at poopeep.Integral.getEquation(Integral.java:59)
    at poopeep.IntegralTest.main(IntegralTest.java:27)

What is the problem? Thanks

awdreg
  • 39
  • 1
  • 8
  • 2
    Post the code that doesn't work, not the code that works. Remove all the irrelevant code. And don't paraphrase the error messages. Post them, verbatim, completely. – JB Nizet Apr 30 '16 at 22:22

1 Answers1

0

The stack trace shows that you're trying to access the element at index 2 of xlist, in the method getEquation(), and that the size of that list is 0.

And indeed, xlist is populated in calc(), but your code never calls calc(), so the list is empty.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255