System.out.print(tm[i][j]+"\t");
System.out.println();
}
}
}
实验(三)类和对象
publicclassComplex{
privatedoublerealPart;
privatedoubleimaginaryPart;
publicComplex(){
}
publicComplex(doublerealPart){
}
publicComplex(doublerealPart,doubleimaginaryPart){
}
publicdoublegetRealPart(){
}
publicvoidsetRealPart(doubleval){
}
publicdoublegetImaginaryPart(){
}
publicvoidsetImaginaryPart(doubleval){
}
publicComplexplus(Complexcomplex){
}
publicComplexminus(Complexcomplex){
}
publicComplextimes(Complexcomplex){
}
publicComplexdivideBy(Complexcomplex){
}
publicComplexconjugate(){
}
publicStringtoString(){
}
●实现Complex类中的方法
publicclassComplex{
privatedoublerealPart;
privatedoubleimaginaryPart;
publicComplex()
{
this(0,0);
//realPart=0.0;
//imaginaryPart=0.0;
}
publicComplex(doublerealPart)
{
this(realPart,0);
}
publicComplex(doublerealPart,doubleimaginaryPart)
{
this.realPart=realPart;
this.imaginaryPart=imaginaryPart;
}
/**
*得到实部的值
*/
publicdoublegetRealPart()
{
returnrealPart;
}
publicvoidsetRealPart(doubleval)
{
realPart=val;
}
/**
*得到虚部的值
*/
publicdoublegetImaginaryPart()
{
returnimaginaryPart;
}
publicvoidsetImaginaryPart(doubleval)
{
imaginaryPart=val;
}
/**
*复数相加
*/
publicComplexplus(Complexcomplex)
{
ComplextmpComplex=newComplex();
tmpComplex.realPart=this.realPart+complex.realPart;
tmpComplex.imaginaryPart=this.imaginaryPart+complex.imaginaryPart;
returntmpComplex;
}
/**
*复数相减
*/
publicComplexminus(Complexcomplex)
{
ComplextmpComplex=newComplex();
tmpComplex.realPart=this.realPart-complex.realPart;
tmpComplex.imaginaryPart=this.imaginaryPart-complex.imaginaryPart;
returntmpComplex;
}
/**
*复数相乘
*/
publicComplextimes(Complexcomplex)
{
ComplextmpComplex=newComplex();
tmpComplex.realPart=this.realPart*complex.realPart-this.imaginaryPart*complex.imaginaryPart;
tmpComplex.imaginaryPart=this.realPart*complex.imaginaryPart+this.imaginaryPart*complex.realPart;
returntmpComplex;
}
/**
*复数相除
*/
publicComplexdivideBy(Complexcomplex)
{
ComplextmpComplex=newComplex();
if(complex.realPart==0.0&&complex.imaginaryPart==0.0)
{
System.out.println("dividedbyzero");
System.exit(0);
}
doubletmp=complex.realPart*complex.realPart+complex.imaginaryPart*complex.imaginaryPart;
tmpComplex.realPart=(this.realPart*complex.realPart+this.imaginaryPart*complex.imaginaryPart)/tmp;
tmpComplex.imaginaryPart=(this.imaginaryPart*complex.realPart-this.realPart*complex.imaginaryPart)/tmp;
returntmpComplex;
}
/**
*共轭复数
*/
publicComplexconjugate()
{
ComplextmpComplex=newComplex();
tmpComplex.realPart=this.realPart;
tmpComplex.imaginaryPart=-this.imaginaryPart;
returntmpComplex;
}
@Override
publicStringtoString()
{
Stringstr=(this.imaginaryPart>=0.0?
"+":
"");
return(this.realPart+str+this.imaginaryPart+"i");
}
publicstaticvoidmain(String[]args){
Complextmp=newComplex();
System.out.print("第一个复数:
");
ComplexcomplexNumA=newComplex(1,2);
System.out.println(complexNumA.toString());
System.out.print("第二个复数:
");
ComplexcomplexNumB=newComplex(5,-3);
System.out.println(complexNumB.toString());
System.out.println("复数相加:
");
tmp=complexNumA.plus(complexNumB);
System.out.println(tmp.toString());
System.out.println("复数相减:
");
tmp=complexNumA.minus(complexNumB);
System.out.println(tmp.toString());
System.out.println("复数相乘:
");
tmp=complexNumA.times(complexNumB);
System.out.println(tmp.toString());
System.out.println("复数相除:
");
tmp=complexNumA.divideBy(complexNumB);
System.out.println(tmp.toString());
System.out.println("第一个的共轭复数:
");
tmp=complexNumA.con