public Complex(String cStr){
this(cStr.split("(?=\\+)|(?=\\-)")); // splits cStr at + or - into an
// array of strings having two elements. The first element of the
// resultant array will be the real portion, while the second is the
// imaginary portion. This array is passed to the next constructor.
How do I determine what the complex number itself is?
// TODO: Write a getter called getComplex() that returns this Complex number itself.
// NOTE: you must return a Complex type, not a String type.
}
如果有人可以在此代码的任何部分提供任何帮助,将不胜感激!
对于初学者,我将定义整个类。所以可能是这样的:
class Complex {
float real, img;
public Complex(float real, float img) {
this.real = real;
this.img = img;
}
}
此后,您可以轻松完成自己想做的事情:
public Complex(String real, String img) {
this(Float.parseFloat(real), Float.parseFloat(img));
}
public Complex(String str) {
String[] components = str.split("(?=\\+)|(?=\\-)");
this(components[0], components[1]);
}
您的问题不是很清楚,但是我想这就是您想要的。我建议写一个叫做Complex.parseComplex(String str)
的静态方法,因为这是Java中解析字符串中数字类型的标准方法。希望对您有所帮助!