复数类

根据要求编程实现复数类ComplexNum:

属性:real,imag,代表实部和虚部,均为double类型。
方法:
ComplexNum(): 成员变量值设为0
ComplexNum(double r,double i):
double getReal() double getImagPart()
void setReal(double d)
void setImag(double d)
ComplexNum add(ComplexNum c):加法,对象加上括弧里的参数对象
ComplexNum add(double d) :加法,对象实部加上括弧里的参数d
ComplexNum minus(ComplexNum c):减法,对象减去括弧里的参数对象
ComplexNum minus(double d) :减法,对象实部减去括弧里的参数d
ComplexNum multi(ComplexNum c):乘法,对象乘以括弧里的参数对象
ComplexNum multi(double d) :乘法,对象乘以括弧里的参数d
ComplexNum div(ComplexNum c):除法,对象除以括弧里的参数对象
ComplexNum div(double d) :除法,对象除以括弧里的参数d
toString():按a+bi形式组合成字符串
要求:
1)在主类中构造两个实部、虚部均不同的复数对象。
2)结合toString()方法依次输出两个实数加减乘除的结果
复数类_第1张图片
复数类_第2张图片


import java.util.Scanner;

/*
 * 姓名:
 * 日期:
 * 功能:根据要求编程实现复数类ComplexNum:
属性:real,imag,代表实部和虚部,均为double类型。
方法:
ComplexNum(): 成员变量值设为0
ComplexNum(double r,double i):
double getReal()  double getImagPart()
void setReal(double d)
void setImag(double d)
ComplexNum add(ComplexNum c):加法,对象加上括弧里的参数对象
ComplexNum add(double d) :加法,对象实部加上括弧里的参数d
ComplexNum minus(ComplexNum c):减法,对象减去括弧里的参数对象
ComplexNum minus(double d) :减法,对象实部减去括弧里的参数d
ComplexNum multi(ComplexNum c):乘法,对象乘以括弧里的参数对象
ComplexNum multi(double d) :乘法,对象乘以括弧里的参数d
ComplexNum div(ComplexNum c):除法,对象除以括弧里的参数对象
ComplexNum div(double d) :除法,对象除以括弧里的参数d
toString():按a+bi形式组合成字符串
要求:
1)在主类中构造两个实部、虚部均不同的复数对象。
2)结合toString()方法依次输出两个实数加减乘除的结果。
 */
public class ComplexNumDemo {
     
	public static void main(String[] args) {
     
		Scanner in=new Scanner(System.in);
		double a,b,c,d;
		a=in.nextDouble();
		b=in.nextDouble();
		c=in.nextDouble();
		d=in.nextDouble();
		ComplexNum i=new ComplexNum(a,b);
		ComplexNum j=new ComplexNum(c,d);
		System.out.println(i.toString(j));
		
	}

}
class ComplexNum
{
     
	private double real,imag;
    public ComplexNum()
    {
     
    	real=0;
    	imag=0;
    }
    public ComplexNum(double real,double imag)
    {
     
    	this.real=real;
    	this.imag=imag;
    }
	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 ComplexNum add(ComplexNum c)
	{
     
      ComplexNum b=new ComplexNum();
	  b.real=this.real+c.real;
	  b.imag=this.imag+c.imag;
	  return b;
	}
    public ComplexNum minus(ComplexNum c)
	{
     
      ComplexNum b=new ComplexNum();
	  b.real=this.real-c.real;
	  b.imag=this.imag-c.imag;
	  return b;
	}
    public ComplexNum multi(ComplexNum c)
	{
     
      ComplexNum b=new ComplexNum();
	  b.real=(this.real*c.real)-(this.imag*c.imag);
	  b.imag=(this.imag*c.real)+(this.real*c.imag);
	  return b;
	}
    public ComplexNum div(ComplexNum c)
	{
     
      ComplexNum b=new ComplexNum();
	  b.real=((this.real*c.real)-(this.imag*(-1)*c.imag))/((c.real*c.real)+(c.imag*c.imag));
	  b.imag=((this.imag*c.real)+(this.real*(-1)*c.imag))/((c.real*c.real)+(c.imag*c.imag));
	  return b;
	}
    
    public String toString(ComplexNum c)
    {
     
    	String info;
    	String i1,i2,i3,j,k,l;
    	if(getImag()>=0)
    	{
     
    		i1="+";
    	}
    	else 
    	{
     
    		i1="";
    	}
    	
    	if(c.imag>=0)
    	{
     
    		i2="+";
    	}
    	else 
    	{
     
    		i2="";
    	}
    	
    	if(add(c).imag>=0)
    	{
     
    		i3="+";
    	}
    	else
    	{
     
    		i3="";
    	}
    	if(minus(c).imag>=0)
    	{
     
    		j="+";
    	}
    	else
    	{
     
    		j="";
    	}
    	if(multi(c).imag>=0)
    	{
     
    		k="+";
    	}
    	else
    	{
     
    		k="";
    	}
    	
    	if(div(c).imag>=0)
    	{
     
    		l="+";
    	}
    	else
    	{
     
    		l="";
    	}
    	info=getReal()+i1+getImag()+"i"+"加上"+c.getReal()+i2+c.getImag()+"i"+"="+add(c).real+i3+add(c).imag+"i"
    			+"\n"+getReal()+i1+getImag()+"i"+"减去"+c.getReal()+i2+c.getImag()+"i"+"="+minus(c).real+j+minus(c).imag+"i"
    			+"\n"+getReal()+i1+getImag()+"i"+"乘以"+c.getReal()+i2+c.getImag()+"i"+"="+multi(c).real+k+multi(c).imag+"i"
    			+"\n"+getReal()+i1+getImag()+"i"+"除以"+c.getReal()+i2+c.getImag()+"i"+"="+div(c).real+l+div(c).imag+"i"; 
    	return info;
    }
}

你可能感兴趣的:(笔记)