Java 使用OOP结构加减复数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18550771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding and subtract complex numbers using OOP structure
提问by Gyneth Ashtoreth
I have here a code that should print the sum and difference of two complex numbers. The instructions given are:
make the methods add
, subtract
, and print
to be void
and
test using the constructor's object.
我这里有一个代码,应该打印两个复数的和和差。给出的说明是:
使方法add
、subtract
和print
成为void
并
使用构造函数的对象进行测试。
public class Complex {
/**
* @param args
*/
public double real;
public double imag;
public String output = "";
public Complex(double real, double imag){
this.real += real;
this.imag += imag;
}
public Complex(){
real = 0;
imag = 0;
}
public double getReal(){
return real;
}
public void setReal(double real){
this.real = real;
}
public double getImag(){
return imag;
}
public void setImag(double imag){
this.imag = imag;
}
public void add(Complex num){
this.real = real + num.real;
this.imag = imag + num.imag;
}
public void subtract(Complex num){
this.real = real - num.real;
this.imag = imag - num.imag;
}
public void print(){
//
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Complex c1 = new Complex(4.0, 8.5);
Complex c2 = new Complex(8.0, 4.5);
c1.add(c2);
c1.subtract(c2);
c1.print(); //expected answer 12.0 + 13.0i
//-4.0 - 4.0i
}
}
The expected answers are 12.0 + 13.0i and -4.0 - 4.0i. Please help me with the method print
. Thank you.
预期的答案是 12.0 + 13.0i 和 -4.0 - 4.0i。请帮助我的方法print
。谢谢你。
回答by Mirko
Perhaps this is not what you're looking for, but to make the number be printed isn't enough to make something like this in your print method?
也许这不是您要找的东西,但是要打印数字还不足以在您的打印方法中制作这样的东西?
System.out.print("The number is: " +real +"+i" +imag);
System.out.print("数字为:" +real +"+i" +imag);
回答by Harsha Jayamanna
public void print(){
if(this.imag <0){
System.out.println(this.real+" "+this.imag+"i");
}
if(this.imag >0){
System.out.println(this.real+"+"+this.imag+"i");
}
}
回答by Aleksei Bulgak
You incorrectly use print merhod. if you want to see correct result you need to rewrite add method like this:
您错误地使用了print merhod。如果你想看到正确的结果,你需要像这样重写 add 方法:
public void add(Complex num, Complex num2){
this.real = num.real + num2.real;
this.imag = num.imag + num2.imag;
}
rewrite subtract method also.
也重写减法方法。
public void subtract(Complex num){
this.real = real - num.real;
this.imag = imag - num.imag;
}
Now main method look like this:
现在主要方法如下所示:
public static void main(String[] args) {
Complex c1 = new Complex(4.0, 8.5);
Complex c2 = new Complex(8.0, 4.5);
Complex result = new Complex(8.0, 4.5);
result.add(c1,c2);
result.print();
result.subtract(c1,c2);
result.print();
print method as I told previously look like:
我之前说过的打印方法如下所示:
public void print(){
System.out.println(real + " " + imag +"i");
}
Explanation:
解释:
In your code you have error. You add c2 to c1 and then subtract c2 frim c1 and then print result. Mathematically this looks like : c1= c1+c2-c2;
在您的代码中,您有错误。您将 c2 添加到 c1,然后从 c1 中减去 c2,然后打印结果。数学上这看起来像: c1= c1+c2-c2;