Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java集成_Java_Class_Integration_Integrate - Fatal编程技术网

java集成

java集成,java,class,integration,integrate,Java,Class,Integration,Integrate,我为我的一门com sci课程设计了一个多项式类,但我有一个问题,那就是如何正确使用积分方法 有人能帮我吗 /** The polynomial class includes the methods: evaluate , add, multiply, * Differentiate , integrate and square root. */ public class polynomial { private int degree; private double[] coeffi

我为我的一门com sci课程设计了一个多项式类,但我有一个问题,那就是如何正确使用积分方法 有人能帮我吗

/** 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);



}
}
/**多项式类包括以下方法:求值、加法、乘法、,
*微分、积分和平方根。
*/
公共类多项式{
私人智力学位;
私人双[]系数;
//创建degMax次多项式且所有系数均为零的构造函数
公共多项式(int-degMax){
度=degMax;
系数=新的双[度+1];
}
//一种setter方法,允许用户为他们构造的多项式设置系数
公共空隙系数(整数d,双v){
如果(d>度)
{
System.out.println(“Erorr消息:您指定的次数大于您创建的多项式次数”);
}
否则{
系数[d]=v;
}   
}
//返回指定度的系数的getter方法
公共双系数(int i){
返回系数[i];
}
//通过搜索系数数组中的最后一个元素来计算多项式次数的私有方法
//不包含零
私有int getDegree(){
int deg=0;
对于(int i=0;i<系数.length;i++)
如果(系数[i]!=0)deg=i;
返回度;
}
//将多项式打印为字符串的方法
公共字符串打印(){
如果(度==0)返回“”+系数[0];
如果(度==1)返回系数[1]+“x+”+系数[0];
字符串s=系数[度]+“x^”+度;
对于(int i=degree-1;i>=0;i--){
如果(系数[i]==0)继续;
如果(系数[i]>0)s=s++“+”+(系数[i]);
如果(系数[i]<0)s=s+“-”+(-系数[i]);
如果(i==1)s=s+x;
如果(i>1)s=s+“x^”+i;
}
返回s;
}
//以指定值x计算多项式的一种方法
公共双评估(双x){
双结果=0;
对于(int i=度;i>=0;i--)
结果=系数[i]+(x*结果);
返回结果;
}
//一种对两个多项式进行符号相加的方法
公共多项式加法(多项式p2){
多项式p1=这个;
多项式p3=新多项式(数学最大值(p1度,p2度));

对于(int i=0;i您几乎完全正确。唯一的问题是您应该调用:

return (integral.evaluate(b) - integral.evaluate(a));
而不是:

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

否则代码看起来没问题。

添加到Boris的答案中,您可以简化
集成
方法,如下所示:

public double integration(double a, double b) {
    polynomial integral = new polynomial(degree + 1);
    for (int i = 1; i <= degree + 1; i++) {
        integral.coefficients[i] = coefficients[i - 1] / i;
    }
    return integral.evaluate(b) - integral.evaluate(a);
}
公共双积分(双a、双b){
多项式积分=新多项式(次数+1);

对于(int i=1;i如果我是你,我会将这些方法分开:

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));
}
public多项式积分()
{
多项式积分=新的多项式(此次+1);

对于(int i=1;i)问题是什么?它崩溃了吗?它计算错误的答案了吗…?顺便说一下…请使用大写字母作为类的第一个字母。另外,不要创建方法“print”,您应该重写toString方法,这样在调试时更容易。
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 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));
 }