Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7440621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:52:47+00:00 2026-05-29T10:52:47+00:00

I designing a polynomial class for one of my com sci courses , I

  • 0

I designing a polynomial class for one of my com sci courses , I have a problem of getting the integration method right
can some one help me with that

/** The polynomial class includes the methods: evaluate , add, multiply, 
 * Differentiate , integrate and square root. 
 */

public class polynomial {

private int degree;
private double[] coefficients;


// a constructor that creates a polynomial of degree degMax with all the coefficients are zeroes
public polynomial(int degMax) {
    degree= degMax; 
    coefficients = new double[degree + 1];  
}


// a setter method that let the users set the coefficients for the polynomial they constructed
public void setCoefficient(int d , double v ){
    if (d > degree) 
    {
        System.out.println("Erorr Message: the degree you specified is larger than the polynomial's degree that you have created ");
    }
    else {
        coefficients[d]=v;  
    }   
}

// a getter method to return the coefficient for the specified degree 
public double getCoefficient(int i){
    return coefficients[i];
}

// private method that counts the degree of the polynomial by searching for the last element in the coefficient array that
// does not contain zero
private int getDegree() {
    int deg = 0;
    for (int i = 0; i < coefficients.length; i++)
        if (coefficients[i] != 0) deg = i;
    return deg;
}

// a method that print out the polynomial as a string   
public String print(){
    if (degree ==  0) return "" + coefficients[0];
    if (degree ==  1) return coefficients[1] + "x + " + coefficients[0];
    String s = coefficients[degree] + "x^" + degree;
    for (int i = degree-1; i >= 0; i--) {
        if      (coefficients[i] == 0) continue;
        else if (coefficients[i]  > 0) s = s + " + " + ( coefficients[i]);
        else if (coefficients[i]  < 0) s = s + " - " + (-coefficients[i]);
        if      (i == 1) s = s + "x";
        else if (i >  1) s = s + "x^" + i;
    }
    return s;
}

// a method that evaluate the polynomial at specified value x 
 public double evaluate(double x) {
        double result = 0;
        for (int i = degree; i >= 0; i--)
            result = coefficients[i] + (x * result);
        return result;

    }

 // a method that perform symbolic addition of two polynomial 
 public polynomial addition(polynomial p2) {
        polynomial p1 = this;
        polynomial p3 = new polynomial(Math.max(p1.degree, p2.degree));
        for (int i = 0; i <= p1.degree; i++) p3.coefficients[i] += p1.coefficients[i];
        for (int i = 0; i <= p2.degree; i++) p3.coefficients[i] += p2.coefficients[i];
        p3.degree = p3.getDegree();
        return p3;
    }

 // a method that performs a symbolic multiplication 
 public polynomial multiply(polynomial p2) {
        polynomial p1 = this;
        polynomial p3 = new polynomial(p1.degree + p2.degree); 
        for (int i = 0; i <= p1.degree; i++)
            for (int j = 0; j <= p2.degree; j++)
                p3.coefficients[i+j] += (p1.coefficients[i] * p2.coefficients[j]);
        p3.degree = p3.getDegree();
        return p3;
    }

 // a method that apply differentiation to polynomial  
 public polynomial differentiate() {
        if (degree == 0) return new polynomial(0);
        polynomial derivative = new polynomial(degree - 1);
        derivative.degree = degree - 1;
        for (int i = 0; i < degree; i++){
            derivative.coefficients[i] = (i + 1) * coefficients[i + 1]; 
        }
        return derivative;
    }

 // a method that find a polynomial integral over the interval a to b 
 public double integration(double a , double b) {
     polynomial integral= new polynomial (degree+1);
     integral.degree= degree+1;
     for (int i=0 ; i<= degree+1 ; i++){
         if (i==0) {
             integral.coefficients[i]= 0;
         }
         else {
         integral.coefficients[i]= (coefficients[i-1]/i); 

         }
         }
     return (evaluate(b)- evaluate(a));
 }





public static void main(String[] args) {

polynomial p1   = new polynomial(3);
p1.setCoefficient(0, 3.0);
p1.setCoefficient(3, 5.0);   
String r = p1.print();   //3.0 + 5.0 x^3

polynomial p2   = new polynomial(2);
p2.setCoefficient(1, 4.0);
p2.setCoefficient(2, 2.0);

polynomial n    = p1.addition(p2);
String po       = n.print();

polynomial t    = p1.multiply(p2);
String tr       = t.print();

polynomial di   = p2.differentiate();
String dir      = di.print();

double ev       = p2.evaluate(5.0); 
double inte     = p1.integration(3.0, 7.0); 



System.out.println("p1(x) = " + r );
System.out.println("p1(x) + p2(x) = " + po);
System.out.println("p1(x) * p2(x) = " + tr);
System.out.println("p2'(x) = " + dir);
System.out.println("p1(x) integration over [3.0, 7.0] = " + inte);
System.out.println("p2(5.0) = " + ev);



}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:52:48+00:00Added an answer on May 29, 2026 at 10:52 am

    If I were you, I would split the methods :

    public Polynomial integrate()
    {
        Polynomial integral = new Polynomial(this.degree + 1);
        for (int i = 1; i <= this.degree+1; i++)
        {
                integral.coefficients[i] = (this.coefficients[i - 1] / i);
        }
        return integral;
    }
    
    // a method that find a Polynomial integral over the interval a to b
    public double integration(double a, double b)
    {
        Polynomial integral = integrate();
        return (integral.evaluate(b) - integral.evaluate(a));
    }
    

    Ok now why it didn’t work as you expected :

    public double integration(double a , double b) {
         polynomial integral= new polynomial (degree+1);
         integral.degree= degree+1;
         for (int i=0 ; i<= degree+1 ; i++){
             if (i==0) {
                 integral.coefficients[i]= 0;
             }
             else {
             integral.coefficients[i]= (coefficients[i-1]/i); 
    
             }
             }
         return (evaluate(b)- evaluate(a));
     }
    

    you messed up your “integral” object with the current instance “this”, clean your code first :

    public double integration(double a , double b) {
         polynomial integral= new polynomial (this.degree+1);
         integral.degree= this.degree+1;
         for (int i=0 ; i<= this.degree+1 ; i++){
             if (i==0) {
                 integral.coefficients[i]= 0;
             }
             else {
                integral.coefficients[i]= (this.coefficients[i-1]/i); 
    
             }
             }
         return (this.evaluate(b)- this.evaluate(a));
     }
    

    Here you can see that you evaluate on your instance object instead of “integral” object. That’s why it messed up the result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When designing both the domain-model and class-diagrams I am having some trouble understanding what
Designing forms has always been fun, but getting them to send email on the
When designing user table what would be the must have fields from the security/user
When designing a collection class, is there any reason not to implement locking privately
When designing a ASP.net WebForm application what are some important steps to take (or
While designing applications it is a very good practice to have all the business
In designing class with multiple/nested has-a relationships, who should catch exceptions? I guess this
When designing the base styles for a site, one should strive to address all
im designing a system where i will have multiple users uploading large amount of
While designing the class, I always get confused what to put in Constructor and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.