資源簡介
定義復數的類Complex并測試其功能:
1. 復數由實部、虛部兩個部分組成,在類的定義中應包含保存這兩部分信息的內容。
2. 在類中定義構造函數,使用戶能夠在構造對象的同時為對象賦初值。
3. 在類中定義復數的加法、減法、乘法三個成員方法來完成復數間的加、減、乘的功能。
4. 通過重載toString方法,使得Complex類的對象能夠顯示其自身信息。
5. 通過顯式定義一個成員函數完成對象的賦值操作,使用戶能夠對Complex類的對象進行賦值。
6. 編寫包含main方法的測試類,使用戶能夠通過這段主程序輸入復數并進行復數的計算。
代碼片段和文件信息
/**??
?*?復數類??
?*/??
public?class?ComplexNumber????
{???
????/**復數的實部*/??
????private?double?realPart;???
????/**復數的虛部*/??
????private?double?imaginaryPart;???
???????
????/**??
?????*?默認構造函數??
?????*/??
????public?ComplexNumber()???
????{???
????????this.realPart?=?0.0;???
????????this.imaginaryPart?=?0.0;???
????}???
???????
????/**??
?????*?構造函數??
?????*?@param?a?實部??
?????*?@param?b?虛部??
?????*/??
????public?ComplexNumber(double?adouble?b)???
????{???
????????this.realPart?=?a;???
????????this.imaginaryPart?=?b;???
????}???
???????
????/**??
?????*?復數的加法運算??
?????*?c?=?a?+?b的運算法則是:??
?????*?c.實部?=?a.實部?+?b.實部;c.虛部?=?a.虛部?+?b.虛部??
?????*?@param?aComNum?加數??
?????*?@return?加法運算的結果,為一個復數對數??
?????*/??
????public?ComplexNumber?add(ComplexNumber?aComNum)???
????{???
????????if(aComNum==null)???
????????{???
????????????System.err.println(“對象不能夠為null!“);???
????????????return?new?ComplexNumber();???
????????}???
????????return?new?ComplexNumber(this.realPart?+?aComNum.getRealPart()this.imaginaryPart?+?aComNum.getImaginaryPart());???
????}???
???????
????/**??
?????*?復數的減法運算??
?????*?c?=?a?-?b的運算法則是:??
?????*?c.實部?=?a.實部?-?b.實部;c.虛部?=?a.虛部?-?b.虛部??
?????*?@param?aComNum?減數??
?????*?@return?減法運算的結果,為一個復數對象??
?????*/??
????public?ComplexNumber?decrease(ComplexNumber?aComNum)???
????{???
????????if(aComNum==null)???
????????{???
????????????System.err.println(“對象不能夠為null!“);???
????????????return?new?ComplexNumber();???
????????}???
????????return?new?ComplexNumber(this.realPart?-?aComNum.getRealPart()this.imaginaryPart?-?aComNum.getImaginaryPart());???
????}???
???????
????/**??
?????*?復數的乘法運算??
?????*?c?=?a?*?b的運算法則是:??
?????*?c.實部?=?a.實部?*?b.實部?-?a.虛部?*?b.虛部;??
?????*?c.虛部?=?a.虛部?*?b.實部?+?a.實部?*?b.虛部;??
?????*?@param?aComNum?乘數??
?????*?@return?乘法運算的結果,為一個復數對象??
?????*/??
????public?ComplexNumber?multiply(ComplexNumber?aComNum)???
????{???
????????if(aComNum==null)???
????????{???
????????????System.err.println(“對象不能夠為null!“);???
????????????return?new?ComplexNumber();???
????????}???
????????double?newReal?=?this.realPart?*?aComNum.realPart?-?this.imaginaryPart?*?aComNum.imaginaryPart;???
????????double?newImaginary?=?this.realPart?*?aComNum.imaginaryPart?+?this.imaginaryPart?*?aComNum.realPart;???
????????ComplexNumber?result?=?new?ComplexNumber(newRealnewImaginary);???
????????return?result;???
???????????
????}???
???????
????/**??
?????*?復數的除法運算??
?????*?c?=?a?/?b?的運算法則是:??
?????*?c.實部?=?(a.實部?*?b.實部?+?a.虛部?*?b.虛部)/(b.實部?*?b.實部?+?b.虛部?*?b.虛部);??
?????*?c.虛部?=?(a
- 上一篇:Java+sql數據庫+fr
ame圖形化界面 - 下一篇:hibernate.jar
評論
共有 條評論