Troubleshooting a Polynomial Function in Java

  • Context: Comp Sci 
  • Thread starter Thread starter schapman22
  • Start date Start date
  • Tags Tags
    Debugging Java
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a Java class designed to evaluate polynomial functions. Participants address an array out of bounds exception encountered during execution, as well as issues related to the evaluation of polynomial expressions.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant identifies that the array "coefficient" is initialized with a size of zero, leading to the out of bounds exception.
  • Another participant suggests that the constructor should initialize the "coefficient" array after setting the "size" variable.
  • A different participant points out that the uninitialized "degree" variable may cause logical issues in the constructor.
  • One participant expresses confusion over the evaluation result, expecting a specific output but receiving a different value, indicating a potential error in the evaluation logic.
  • Another participant later revises the code to correct the evaluation method, changing the loop to iterate from "degree" downwards.
  • A suggestion is made to change the parameter type of the "evaluate" method from int to double to allow for more flexible evaluations.

Areas of Agreement / Disagreement

Participants generally agree on the issues present in the initial implementation, but there are multiple views on the best approach to resolve them. The discussion remains unresolved regarding the optimal design of the class and its methods.

Contextual Notes

Limitations include the initial lack of initialization for the "degree" variable and the potential confusion regarding the purpose of the "max" parameter in the constructor. The evaluation logic also had issues that were later addressed.

schapman22
Messages
74
Reaction score
0

Homework Statement



We must make a class used to evaluate a polynomial function of x. My problem is I am getting an array out of bounds exception when I run the main, and I can't figure out why.
If you could help me figure out this out of bounds exception it would be very appreciated thank you.

Homework Equations



ex. P(x)= a0 + a1x + a2x2...

The Attempt at a Solution



This is the class

Code:
public class Polynomial 
{
    private int degree, i=0, size;
    private double answer;
    private double[] coefficient = new double[size];
    
    public Polynomial(int max)
    {
        if(degree>=0) 
            degree = max;
        size=max+1;
               
    }
    
    public void setConstant(int subscript, double value)
    {
        i = subscript;
        coefficient[i] = value;        
    }
    
    public double evaluate(int x)
    {
        for(int index=0;index<coefficient.length;index++)
        {
            answer = coefficient[index] * (Math.pow(x, degree));
        }
            return answer;        
    }
}

This is the main

Code:
public class PolynomialDemo 
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        Polynomial p = new Polynomial(3);
        
        p.setConstant(0, 3);
        p.setConstant(1, 5);
        p.setConstant(2, 0);
        p.setConstant(3, 2);
        
        System.out.println(p.evaluate(7));
    }
}
 
Last edited:
Physics news on Phys.org
It is because the length of the array coefficient is zero!

When you are creating the object of the class Polynomial, you are calling the constructor and it is updating the value of "size" but not the creating an array "coefficient" of length "size". It will be better if you define the length of the "coefficient" array inside the constructor.
therefore the correct version should look like this:

private double[] coefficient ;

public Polynomial(int max)
{
if(degree>=0)
degree = max;
size=max+1;
coefficient = new double[size];

}
 
schapman22 said:

Homework Statement



We must make a class used to evaluate a polynomial function of x. My problem is I am getting an array out of bounds exception when I run the main, and I can't figure out why.
If you could help me figure out this out of bounds exception it would be very appreciated thank you.

Homework Equations



ex. P(x)= a0 + a1x + a2x2...

The Attempt at a Solution



This is the class

Code:
public class Polynomial 
{
    private int degree, i=0, size;
    private double answer;
    private double[] coefficient = new double[size];
    
    public Polynomial(int max)
    {
        if(degree>=0) 
            degree = max;
        size=max+1;
               
    }
    
    public void setConstant(int subscript, double value)
    {
        i = subscript;
        coefficient[i] = value;        
    }
    
    public double evaluate(int x)
    {
        for(int index=0;index<coefficient.length;index++)
        {
            answer = coefficient[index] * (Math.pow(x, degree));
        }
        return answer;        
    }
}

This is the main

Code:
public class PolynomialDemo 
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        Polynomial p = new Polynomial(3);
        
        p.setConstant(0, 3);
        p.setConstant(1, 5);
        p.setConstant(2, 0);
        p.setConstant(3, 2);
        
        System.out.println(p.evaluate(7));
    }
}

The problem seems to be that your constructor does not initialize degree. Here's what I think is going wrong.
In main:
Code:
Polynomial p = new Polynomial(3);

This causes the constructor to be called, with the max parameter set to 3.
Code:
public Polynomial(int max)
{
    if(degree>=0) 
         degree = max;
    size=max+1;
}
Since degree is uninitialized, there's no telling whether degree>=0 will be true. I'm not sure what the purpose of the max variable is - it could possibly be eliminated altogether, and you could have the parameter of the constructor be degree.
 
Thank you, and now, that example i used in the main should set a polynomial like
P(x) = 3 + 5x + 2x^3 and when i evaluate for x=7 should result in 724, but I'm getting 686 which is just 2x^3. Can anyone see what I did wrong?
 
Never mind it works properly now. Revised the code to this.
Code:
public class Polynomial 
{
    private int degree, i=0, size;
    private double answer;
    private double[] coefficient = new double[size];
    
    public Polynomial(int max)
    {
        if(max>=0) 
            degree = max;
        size=max+1;
        coefficient = new double[size];
    }
    
    public void setConstant(int subscript, double value)
    {
        i = subscript;
        coefficient[i] = value;        
    }
    
    public double evaluate(int x)
    {
        for(int index=degree;index>=0;index--)
        {
            answer += coefficient[index] * (Math.pow(x, degree));
            degree--;
        }
            return answer;        
    }
}
 
The parameter of your evaluate method should be double instead of int, so that you can evaluate the polynomial at values such as 2.5.
 
Ok I will do that thank you.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K