天天看點

用java實作複數的加減乘除運算1. 背景2. 題目内容3. 具體代碼與解釋

用java實作複數的加減乘除運算

1. 背景

    老師在課上布置了幾道java程式設計題,此為其中之一

2. 題目内容

設計一個類Complex,用于封裝對複數的下列操作:

(1)一個帶參數的構造函數,用于初始化複數成員

(2)一個不帶參數的構造函數,調用代參數的構造函數完成對複數成員的初始化。

(3)實作兩個複數的加法,減法的靜态方法和執行個體方法。

(4)以複數的标準形式:x+iy 輸出此複數

(5) 寫兩個函數,分别獲得複數的實部getReal(),getImage()和虛部。

老師原題如上,自己做了兩個複數的加減乘除運算,使用的是執行個體方法。如果要寫靜态方法,即類方法,要加static,再根據相應變化修改。差別是:執行個體方法既可調用執行個體變量和執行個體方法,又可調用類變量和類方法。類方法隻可調用類變量和類方法。因時間關系,明天還有課,自己就暫且寫了執行個體。

用java實作複數的加減乘除運算1. 背景2. 題目内容3. 具體代碼與解釋

3. 具體代碼與解釋

package Four;
/**
 * @author Kun Sun
 * @Date: 2013.10.15
 */
import java.util.Scanner;

public class Complex { // 複數類
	double real;  // 實部
	double image; // 虛部
	
	Complex(){  // 不帶參數的構造方法
		Scanner input = new Scanner(System.in);
		double real = input.nextDouble();
		double image = input.nextDouble();
		Complex(real,image);
	}

	private void Complex(double real, double image) { // 供不帶參數的構造方法調用
		// TODO Auto-generated method stub
		this.real = real;
		this.image = image;
	}

	Complex(double real,double image){ // 帶參數的構造方法
		this.real = real;
		this.image = image;
	}

	public double getReal() {
		return real;
	}

	public void setReal(double real) {
		this.real = real;
	}

	public double getImage() {
		return image;
	}

	public void setImage(double image) {
		this.image = image;
	}
	
	Complex add(Complex a){ // 複數相加
		double real2 = a.getReal();
		double image2 = a.getImage();
		double newReal = real + real2;
		double newImage = image + image2;
		Complex result = new Complex(newReal,newImage);
		return result;
	}
	
	Complex sub(Complex a){ // 複數相減
		double real2 = a.getReal();
		double image2 = a.getImage();
		double newReal = real - real2;
		double newImage = image - image2;
		Complex result = new Complex(newReal,newImage);
		return result;
	}
	
	Complex mul(Complex a){ // 複數相乘
		double real2 = a.getReal();
		double image2 = a.getImage();
		double newReal = real*real2 - image*image2;
		double newImage = image*real2 + real*image2;
		Complex result = new Complex(newReal,newImage);
		return result;
	}
	
	Complex div(Complex a){ // 複數相除
		double real2 = a.getReal();
		double image2 = a.getImage();
		double newReal = (real*real2 + image*image2)/(real2*real2 + image2*image2);
		double newImage = (image*real2 - real*image2)/(real2*real2 + image2*image2);
		Complex result = new Complex(newReal,newImage);
		return result;
	}
	
	public void print(){ // 輸出
		if(image > 0){
			System.out.println(real + " + " + image + "i");
		}else if(image < 0){
			System.out.println(real + "" + image + "i");
		}else{
			System.out.println(real);
		}
	}
}

           
package Four;
/**
 * @author Kun Sun
 * @Date: 2013.10.15
 */
public class MainClass { // 用于測試複數類

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("請使用者輸入第一個複數的實部和虛部:");
        Complex data1 = new Complex();
        System.out.println("請使用者輸入第二個複數的實部和虛部:");
        Complex data2 = new Complex();
       
        // 以下分别為加減乘除
        Complex result_add = data1.add(data2);
        Complex result_sub = data1.sub(data2);
        Complex result_mul = data1.mul(data2);
        Complex result_div = data1.div(data2);
        
        result_add.print();
        result_sub.print();
        result_mul.print();
        result_div.print();
	}
}

           

4. 測試運作結果截圖

用java實作複數的加減乘除運算1. 背景2. 題目内容3. 具體代碼與解釋