java toString表示

时间:2014-10-29 16:41:17

标签: java

我是java的新手,我正在尝试查看方法public String toString()是否正确表示多项式函数。我不知道如何从main给出系数,以便类Func接收它们。

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

    }

}

4 个答案:

答案 0 :(得分:0)

如果您想查看toString()fmain对象System.out.println(f); 的作用,您需要做的只是

f

println已经有你传递给构造函数的系数。 toString()会调用对象的System.out.println(s); 方法并输出结果字符串供您查看。

此外,正如史蒂文在评论中指出的那样,你不需要提出:

toString()

在您的toString方法中。 main应该生成并返回字符串。您的{{1}}方法可以处理打印出来。

答案 1 :(得分:0)

虽然有效,但如果我给出值{-3,-5,-4},我会收到:

-3.0x^2-5.0-4.0

它错过了第二个词的 x (-5.0x)。只有当第二个值是负值时,这才是幸福的。对于正值,它可以正常工作。

答案 2 :(得分:0)

f中查看toString()对对象main执行的操作非常简单... 你只需要使用: System.out.println(f); 此方法将toString()的结果打印到命令行。 这就是全部;)

答案 3 :(得分:0)

试试这种方式。

class 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;
}

...