class Complex{
private int realPart;
private int imaginPart;
Complex(){
thisrealPart = 0;
thisimaginPart = 0;
}
Complex(int realPart, int imaginPart){
thisrealPart = realPart;
thisimaginPart = imaginPart;
}
public void setRealPart(int realPart){
thisrealPart = realPart;
}
public void setImaginPart(int imaginPart){
thisimaginPart = imaginPart;
}
public int getRealPart(){
return thisrealPart;
}
public int getImaginPart(){
return thisimaginPart;
}
Complex complexAdd(Complex a){
int realPart = thisrealPart + agetRealPart();
int imaginPart = thisimaginPart + agetImaginPart();
return new Complex(realPart, imaginPart);
}
public String toString(){
if(thisimaginPart == 0) return "" + thisrealPart;
else if(thisimaginPart > 0) return "" + thisrealPart + "+" + thisimaginPart + "i";
else return "" + thisrealPart + thisimaginPart + "i";
}
}
public class TestComplex{
public static void main(String[] args){
Complex a = new Complex(1,2);
Complex b = new Complex(3,4);
Complex sum = acomplexAdd(b);
Systemoutprintln("(" + a + ")" + "+" + "(" + b + ")" + "=" + "(" + sum + ")");
asetRealPart(2);
asetImaginPart(3);
bsetRealPart(bgetRealPart() 2);
bsetImaginPart(bgetImaginPart() 2);
sum = acomplexAdd(b);
Systemoutprintln("(" + a + ")" + "+" + "(" + b + ")" + "=" + "(" + sum + ")");
}
}
以上就是关于JAVA程序设计 急要 谢谢~~~全部的内容,包括:JAVA程序设计 急要 谢谢~~~、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)