需要帮助制作String toString()方法

时间:2015-04-02 04:35:18

标签: java tostring

我正在创建一个基本上显示复数的程序,实部和虚部都有自己的数据成员,然后它显示了一些数学运算(加,减,乘)。
我有两个类文件public class Complex()和公共类ComplexTester()。在我的Complex()课程中,我不知道如何处理toString()方法,以使输出看起来像这样:http://i.gyazo.com/4fd9e08113e4668b6d4ac3678387092b.png

以下是我Complex()课程中的一些主要内容,可以让您了解它正在做什么。

//**********Methods to perform calculations with object**********

public Complex add(Complex c){
    return new Complex((c.realPart + this.realPart) , (c.imaginaryPart + this.imaginaryPart));
}

public Complex subtract(Complex c){
    return new Complex((this.realPart - c.realPart) , (this.imaginaryPart - c.imaginaryPart));
}

public Complex multiply(Complex c){
    return new Complex();
}  



//Checks if an object is equal to the parameter
    public boolean equals(Complex c){
        return isEqual(c.getReal(), this.getReal()) && isEqual(c.getImaginary(), this.getImaginary());
    }

    //Allows checking of equality of doubles
    public boolean isEqual(double x, double y){
        //multiplier for 8 decimal places
        final double dec = Math.pow(10, 8);

        return (Math.round((x * dec)) == Math.round(y * dec));
    }

然后是测试人员:


public class ComplexTester
{
    public static void main (String[] args)
    {
        //Declare five objects in the Complex class
        Complex c1, c2, c3, c4, c5;

        //Initialize Complex objects using constructors
        c1 = new Complex(1.5, -0.8);
        c2 = new Complex(2,3);
        c3 = new Complex();
        c4 = new Complex(2);
        c5 = new Complex(0,7);

        //Show all four objects
        System.out.println("c1 is " + c1);
        System.out.println("c2 is " + c2);
        System.out.println("c3 is " + c3);
        System.out.println("c4 is " + c4);
        System.out.println("c5 is " + c5);
        System.out.println();

        //Determine if objects are real numbers
        System.out.println(c1 + " is " + (c1.isReal() ? "real" : "NOT real"));
        System.out.println(c4 + " is " + (c4.isReal() ? "real" : "NOT real"));
        System.out.println();

        //Manually set a value
        c3.setValue(c1.getReal(), c1.getImaginary());
        System.out.println("Changed c3 to " + c3);

        //Test for equality
        System.out.print (c1 + " is ");
        System.out.print ( (c1.equals(c3)) ? "equal" : "NOT equal");
        System.out.println (" to " + c3);
        System.out.println();

        System.out.print (c1 + " is ");
        System.out.print ( (c1.equals(c2)) ? "equal" : "NOT equal");
        System.out.print (" to " + c2);
        System.out.println();


        //Demonstrate math operations
        System.out.println ("\n Some Math Operations:");
        System.out.println (c1 + " + " + c2 + " = " + c1.add(c2));
        System.out.println (c1 + " - " + c2 + " = " + c1.subtract(c2));
        System.out.println ("(" + c1 + ") * " + "(" + c2 + ") = " + c1.multiply(c2));
        System.out.println();

        System.out.println("This concludes the test of the Complex number class.");
        System.out.println();

    }
}

3 个答案:

答案 0 :(得分:1)

如果您想要toString方法输出格式a+bi的复数,可以使用以下内容:

public String toString() {
    return String.valueOf(realPart) + "+" +
           String.valueOf(imagPart) + "i";
}

当然,您可以看中并找出个别位是否缺失:

public String toString() {
    if (imaginaryPart == 0)
        return String.valueOf(realPart);
    if (realPart == 0)
        return String.valueOf(imagPart) + "i";
    return realPart + "+" + imagPart + "i";
}

例如,参见以下测试程序:

public class Cplx {
    private double realPart, imagPart;

    public Cplx(double r, double i) {
        realPart = r;
        imagPart = i;
    }

    public String toString() {
        if (imagPart == 0)
            return String.valueOf(realPart);

        if (realPart == 0)
            return String.valueOf(imagPart) + "i";

        return realPart + "+" + imagPart + "i";
    }

    public static void main(String[] args) {
        Cplx n;
        n = new Cplx(0,0); System.out.println(n);
        n = new Cplx(1,0); System.out.println(n);
        n = new Cplx(0,2); System.out.println(n);
        n = new Cplx(1,2); System.out.println(n);
    }
}

输出:

0.0
1.0
2.0i
1.0+2.0i

答案 1 :(得分:1)

toString()返回Object的String表示。

例如,在您的Complex类中,您有实部和虚部的变量,您可能有一个toString()方法,如下所示:

@Override
public String toString() {
    return "Complex [realPart=" + realPart + ", imaginaryPart="
            + imaginaryPart + "]";
}

如果您正在使用eclipse进行Java开发,您实际上可以自动生成toString()和其他一些方法。通常,您不会在toString()方法中格式化输出。你应该为它做一个单独的显示方法。

答案 2 :(得分:0)

Simpliy用于打印,您可以使用

System.out.printf("%.3f + %.3fi",1.21312939123,3.44);

您可以将小数位数更改为您的要求

在类似的行上,你可以形成一个带有实部变量+"的字符串。 +" + complexpart +" I"