创建一个复数类并实现加减乘除运算

package exe01;

import java.util.Scanner;
/*name:Excise1                   功能:创建一个复数类并实现加减乘除运算
 */
public class first1 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		//创建类
		class Complex {
			//定义成员变量
			float RealPart;
			float ImaginaryPart;
			//构造函数
			Complex(float dbRealPart,float dbImaginaryPart) {                     
				this.RealPart=dbRealPart;
				this.ImaginaryPart=dbImaginaryPart;
			}
			
			
			
			private void Complex(float dbRealPart, float dbImaginaryPart) {       
				this.RealPart=dbRealPart;
				this.ImaginaryPart=dbImaginaryPart;
			}
			Complex() {                                                   
				Scanner sc=new Scanner(System.in );
				float RealPart=sc.nextFloat();
				float ImaginaryPart=sc.nextFloat();
				Complex(RealPart,ImaginaryPart);
			}
			
			
			public float getRealPart() {
				return RealPart;
			}
			public void setRealPart(float RealPart) {
				this.RealPart=RealPart;
			}
			public float getImaginaryPart() {
				return ImaginaryPart;
			}
			public void setImaginaryPart(float ImaginaryPart) {
				this.ImaginaryPart=ImaginaryPart;
			}
			
			Complex Add(Complex a) {                                               //加法运算
				float RealPart1=a.getRealPart();
				float ImaginaryPart1=a.getImaginaryPart();
				float newRealPart = RealPart+RealPart1;
				float newImaginaryPart = ImaginaryPart+ImaginaryPart1;
				Complex result = new Complex(newRealPart,newImaginaryPart);
				return result;
			}
			
			Complex Reduce(Complex a) {                                            //减法运算
				float RealPart1=a.getRealPart();
				float ImaginaryPart1=a.getImaginaryPart();
				float newRealPart = RealPart-RealPart1;
				float newImaginaryPart = ImaginaryPart-ImaginaryPart1;
				Complex result = new Complex(newRealPart,newImaginaryPart);
				return result;
			}
			Complex Multiply(Complex a) {                                               //乘法运算
				float RealPart1=a.getRealPart();
				float ImaginaryPart1=a.getImaginaryPart();
				float newRealPart = RealPart*RealPart1-ImaginaryPart*ImaginaryPart1;
				float newImaginaryPart = RealPart*ImaginaryPart1+ImaginaryPart*RealPart1;
				Complex result = new Complex(newRealPart,newImaginaryPart);
				return result;
			}
			Complex Divide(Complex a) {                                               //除法运算
				float RealPart1=a.getRealPart();
				float ImaginaryPart1=a.getImaginaryPart();
				float newRealPart = (RealPart*RealPart1+ImaginaryPart*ImaginaryPart1)/(RealPart1*RealPart1+ImaginaryPart1*ImaginaryPart1);
				float newImaginaryPart = (ImaginaryPart*RealPart1-RealPart*ImaginaryPart1)/(RealPart1*RealPart1+ImaginaryPart1*ImaginaryPart1);
				Complex result = new Complex(newRealPart,newImaginaryPart);
				return result;
			}
			public void printf() {                                                    //输出结果
				System.out.println("("+RealPart+","+ImaginaryPart+")");
			}
			
		}
		
		
		
		
		
		
		
		      System.out.println("请输入第一个复数的实部和虚部:");
              Complex first = new Complex();
              System.out.println("请输入第二个复数的实部和虚部:");
              Complex second = new Complex();
              
              Complex result_Add = first.Add(second);                    //是第一个对第二个的操作
              Complex result_Reduce = first.Reduce(second);
              Complex result_Multiply = first.Multiply(second);
              Complex result_Divide = first.Divide(second);
              
              System.out.println("两个复数相加后:");
              result_Add.printf();
              System.out.println("两个复数相减后:");
              result_Reduce.printf();
              System.out.println("两个复数相乘后:");
              result_Multiply.printf();
              System.out.println("两个复数相除后:");
              result_Divide.printf();
	}

}/*
 * 法二:
 * Complex Add(Complex a,Complex b) {                                               //加法运算
	//float RealPart1=a.getRealPart();
	//float ImaginaryPart1=a.getImaginaryPart();
	float newRealPart = a.RealPart+b.RealPart;
	float newImaginaryPart = a.ImaginaryPart+b.ImaginaryPart;
	Complex result = new Complex(newRealPart,newImaginaryPart);
	return result;
}
 * 
*/

 

你可能感兴趣的:(Java基础)