需要帮助制作 String toString() 方法
Need help making String toString() method
我正在制作一个基本上显示复数的程序,实部和虚部都有自己的数据成员,然后显示用它们完成的一些数学运算(加、减、乘)。
我有两个 class 文件,public class Complex()
和 public class ComplexTester()
。在我的 Complex()
class 中,我不知道如何处理 toString()
方法才能使输出看起来像这样: http://i.gyazo.com/4fd9e08113e4668b6d4ac3678387092b.png
下面是我 Complex()
class 中的一些主要内容,让您了解它在做什么。
//**********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();
}
}
如果您想要一个 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
toString() returns 对象的字符串表示形式。
例如,在您的 Complex class 中,您有实部和虚部的变量,您可能有一个 toString() 方法,如下所示:
@Override
public String toString() {
return "Complex [realPart=" + realPart + ", imaginaryPart="
+ imaginaryPart + "]";
}
如果您使用 eclipse 进行 Java 开发,您实际上可以自动生成 toString() 和一些其他方法。通常,您不会在 toString() 方法中格式化输出。您应该为此制作一个单独的显示方法。
打印简单,可以用
System.out.printf("%.3f + %.3fi",1.21312939123,3.44);
您可以根据需要更改小数位
在类似的行上,你可以用实数部分变量+“+”+复数部分+"i".
组成一个字符串
我正在制作一个基本上显示复数的程序,实部和虚部都有自己的数据成员,然后显示用它们完成的一些数学运算(加、减、乘)。
我有两个 class 文件,public class Complex()
和 public class ComplexTester()
。在我的 Complex()
class 中,我不知道如何处理 toString()
方法才能使输出看起来像这样: http://i.gyazo.com/4fd9e08113e4668b6d4ac3678387092b.png
下面是我 Complex()
class 中的一些主要内容,让您了解它在做什么。
//**********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();
}
}
如果您想要一个 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
toString() returns 对象的字符串表示形式。
例如,在您的 Complex class 中,您有实部和虚部的变量,您可能有一个 toString() 方法,如下所示:
@Override
public String toString() {
return "Complex [realPart=" + realPart + ", imaginaryPart="
+ imaginaryPart + "]";
}
如果您使用 eclipse 进行 Java 开发,您实际上可以自动生成 toString() 和一些其他方法。通常,您不会在 toString() 方法中格式化输出。您应该为此制作一个单独的显示方法。
打印简单,可以用
System.out.printf("%.3f + %.3fi",1.21312939123,3.44);
您可以根据需要更改小数位
在类似的行上,你可以用实数部分变量+“+”+复数部分+"i".
组成一个字符串