標籤:java
/**4-1根據課本第五章習題5-12的要求編程實現題目指定的複數類ComplexNumber*/
public class adu {public static void main (String[] args){}}class ComplexNumber{private int m_dRealPart;//實部private int m_dImaginPart;//虛部ComplexNumber (){this.m_dRealPart = 0;this.m_dImaginPart = 0;}ComplexNumber (double r, double i){this.m_dRealPart = (int)r;this.m_dImaginPart = (int)i;}int getRealPart ()//擷取當前對象的實部{return m_dRealPart;}int getImaginaryPart()//擷取當前對象的虛部{return m_dImaginPart;}void setRealPrat (double d)//把當前對象的實部設定為給定的形參的數字{m_dRealPart = (int)d;}void setImaginaryPart (double d)//把當前對象的虛部設定為給定的形參的數字{m_dImaginPart = (int)d;}ComplexNumber complexAdd (ComplexNumber c) //當前複數對象與形參複數對象相加,所得結果也是複數值,返回給此方法的調用者{this.m_dImaginPart += c.m_dImaginPart;this.m_dRealPart += c.m_dRealPart;return this;}ComplexNumber complexAdd (double c)//當前複數對象與形參實數對象相加,所得的結果仍是複數值,返回給此方法的調用者{this.m_dRealPart += c;return this;}ComplexNumber complexMinus (ComplexNumber c)//當前複數對象與形參複數對象相減,所得的結果仍是複數值,返回給此方法的調用者{this.m_dImaginPart -= c.m_dImaginPart;this.m_dRealPart -= c.m_dRealPart;return this;}ComplexNumber complexMiuns (double c)//當前複數對象與形參實數對象相減,所得的結果仍是複數值,返回給此方法的調用者{this.m_dRealPart -= c;return this;}ComplexNumber complexMulti (ComplexNumber c)//當前複數對象與形參複數對象相乘,所得的結果仍是複數值,返回給此方法的調用者{this.m_dImaginPart *= c.m_dImaginPart;this.m_dRealPart *= c.m_dRealPart;return this;}ComplexNumber complexMulti (double c)//當前複數對象與形參實數對象相乘,所得的結果仍是複數值,返回給此方法的調用者{this.m_dRealPart *= c;return this;}ComplexNumber complexDiv (ComplexNumber c)//當前複數對象與形參複數對象相除,所得的結果仍是複數值,返回給此方法的調用者{this.m_dImaginPart /= c.m_dImaginPart;this.m_dRealPart /= c.m_dRealPart;return this;}ComplexNumber complexDiv (double c)//當前複數對象與形參實數對象相除,所得的結果仍是複數值,返回給此方法的調用者{this.m_dRealPart /= c;return this;}}
本文出自 “hacker” 部落格,請務必保留此出處http://anglecode.blog.51cto.com/5628271/1619848
那些年,一起學的Java 4-1