2014-10-29 52 views
-1

我是新来的java,我试图看看方法public String toString()是否正确表示多项式函数。我不知道如何给主系数赋予系数Funcjava toString representation

package ro.utcluj.poo.lab04; 

import java.util.Scanner; 

class Func { 

    public double[] coef; //the coefficients 
    public int nrCoef; //coefficients number 


    public Func(double[] input) 
    { 
     nrCoef = input.length; 
     this.coef = new double[nrCoef]; 

     for (int counter = 0; counter < input.length; counter++) 
      coef[counter] = input[counter]; 

    } 
    public double getFuncValue(double x) 
    {   
     double exponent = nrCoef; 
     double y = 0; 
     double sum = 0; 

     for(int i = nrCoef; i >= 0; i--) 
     { 
      y = coef[i]*Math.pow(x, exponent-1); //n grade polynomial function 

      exponent--; 
      sum += y; //the sume for each member 
     } 
     return sum; 
    } 



    public double getDerivValue(double x) 
    { 
     double deriv = 0; 
     double rezDeriv = 0; 

     for(int i = 0; i < nrCoef - 1; i++) 
     { 
      deriv = coef[i]*(nrCoef - i)*Math.pow(x, nrCoef - i -1); 
      rezDeriv += deriv; 
     } 

     return rezDeriv; 
    } 

    public String toString() 
    { 
     String s = new String(" "); 
     int exp = nrCoef-1; 

     for(int i = 0; i < nrCoef; i++) 
     { 
      if(exp == 0 && coef[i] > 0) 
       s +="+" + coef[i]; 
      else if(exp == 0 && coef[i] < 0) 
       s +=coef[i]; 
      else if(exp == 1 && coef[i] > 0 && i == 0) 
       s +="+" + coef[i] + "x"; 
      else if(exp == 1 && coef[i] >0) 
       s +="+" + coef[i]; 
      else if(exp == 1 && coef[i] < 0) 
       s+=coef[i]; 
      else if(coef[i] == 0) 
       s += ""; 
      else if(coef[i] > 0 && i!=0) 
       s +="+" + coef[i]+"x^" + exp; 
      else 
       s +=coef[i] + "x^" + exp; 
      exp--; 
      System.out.println(s); 
     } 

     return s; 
    } 
} 

public class Main04 { 

    public static void main(String[] args) { 

     double[] v = new double[]{3,5,4}; 
     Func f = new Func(v); 

    } 

} 
+0

'的System.out.println(F)'(这将调用'的toString()''上F')? – 2014-10-29 16:43:19

+0

它看起来像你已* *已经通过你的'Func'实例的系数;这就是'new Func(v);'中'v'参数正在做的! – JonK 2014-10-29 16:46:25

+1

你也应该避免字符串串联(使用['StringBuilder'](http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html)) – 2014-10-29 16:46:47

回答

0

如果你想看到什么toString()main做你的对象f上,所有你需要做的是

System.out.println(f); 

f已具有您传递到它的构造函数的系数。 println将调用对象的toString()方法并输出结果字符串供您查看。

而且,史蒂芬在评论中指出的那样,你不需要把:

System.out.println(s); 

toString()方法本身。 toString应该产生并返回字符串。您的main方法可以处理打印出来。

0

这工作,但如果我给的值{-3,-5,-4}我收到此:

-3.0x^2-5.0-4.0 

它缺少X从第二项(-5.0x)。只有当第二个值是负值时,这才是幸运的。对于正值,它工作正常。

+0

我发现了这个问题。有一个** x **丢失:else if(exp == 1 && coef [i] <0)s + = coef [i] + **“x”**; – sixfeet 2014-10-29 17:29:36

0

这是很简单的,看看有什么的toString()在main不上对象f ... 你只需要使用哟: System.out.println(f); 这种方法将打印的toString()命令行的结果。 就是这样;)

0

试试这个方法。

类Func键{

public double[] coef; // the coefficients 
public int nrCoef; // coefficients number 
private StringBuilder sbl = new StringBuilder(); 
private StringBuilder tsbl = new StringBuilder(); 

public Func(double[] input) { 
    nrCoef = input.length; 
    this.coef = new double[nrCoef]; 

    sbl.append("\nF(x) = "); 
    int exp = 0; 
    for (int counter = 0; counter < nrCoef; counter++) { 
     coef[counter] = input[counter]; 
     if (coef[counter] != 0) { 
      if (counter != 0) { 
       sbl.append(coef[counter] < 0 ? " - " : " + "); 
      } else if (coef[counter] < 0) { 
       sbl.append(" - "); 
      } 
      exp = nrCoef - counter - 1; 
      sbl.append(Math.abs(coef[counter])+(exp == 0 ? "" : exp == 1 ? "*x" : "*x^"+exp)); 
     } 
    } 
} 

public String toString() { 
    return tsbl.toString().isEmpty() ? sbl.toString() : tsbl.toString(); 
} 

public double getFuncValue(double x) { 
    double sum = 0; 

    for (int index = 0; index < nrCoef; index++) { 
     sum += coef[index] * Math.pow(x, nrCoef - index - 1); // n grade polynomial 
    } 
    tsbl = new StringBuilder(); 
    tsbl.append(sbl.toString()); 
    tsbl.append("\nF("); 
    tsbl.append(x); 
    tsbl.append(") = "+sum); 
    return sum; 
} 

...