版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
java語言程序設計基礎篇第十版第十三章練習答案BigIntegergcd=gcd(numerator,denominator);if(pareTo(Biglnteger.ZERO)<0)this.numerator=numerator.multiply(newBigInteger("-1")).divide(gcd);elsethis.numerator=numerator.divide(gcd);this.denominator=denominator.abs().divide(gcd);}/**FindGCDoftwonumbers*/privatestaticBigIntegergcd(BigIntegern,BigIntegerd){BigIntegern1=n.abs();BigIntegern2=d.abs();BigIntegergcd=BigInteger.ONE;for(BigIntegerk=BigInteger.ONE;pareTo(nl)<=0&&pareTo(n2)<=0;k=k.add(BigInteger.ONE)){if(nl.remainder(k).equals(BigInteger.ZERO)&&n2.remainder(k).equals(BigInteger.ZERO))gcd=k;}returngcd;}/**Returnnumerator*/publicBigIntegergetNumerator(){returnnumerator;}/**Returndenominator*/publicBigIntegergetDenominator(){returndenominator;}/**Addarationalnumbertothisrational*/publicRationaladd(RationalsecondRational){BigIntegern=numerator.multiply(secondRational.getDenominator()).add(denominator.multiply(secondRational.getNumerator()));BigIntegerd=denominator.multiply(secondRational.getDenominator());returnnewRational(n,d);}
/**Subtractarationalnumberfromthisrational*/publicRationalsubtract(RationalsecondRational){BigIntegern=numerator.multiply(secondRational.getDenominator()).subtract(denominator.multiply(secondRational.getNumerator()));BigIntegerd=denominator.multiply(secondRational.getDenominator());returnnewRational(n,d);}/**Multiplyarationalnumbertothisrational*/publicRationalmultiply(RationalsecondRational){BigIntegern=numerator.multiply(secondRational.getNumerator());BigIntegerd=denominator.multiply(secondRational.getDenominator());returnnewRational(n,d);}/**Dividearationalnumberfromthisrational*/publicRationaldivide(RationalsecondRational){BigIntegern=numerator.multiply(secondRational.getDenominator());BigIntegerd=denominator.multiply(secondRational.numerator);returnnewRational(n,d);}@OverridepublicStringtoString(){if(denominator.equals(BigInteger.ONE))returnnumerator+"";elsereturnnumerator+"/"+denominator;}@Override/**OverridetheequalsmethodintheObjectclass*/publicbooleanequals(Objectparml){if((this.subtract((Rational)(parm1))).getNumerator().equals(BigInteger.ONE))returntrue;elsereturnfalse;}@Override/**OverridethehashCodemethodintheObjectclass*/publicinthashCode(){returnnewDouble(this.doubleValue()).hashCode();}@Override/**OverridetheabstractintValuemethodinjava.lang.Number*/
publicintintValue(){return(int)doubleValue();}@Override/**OverridetheabstractfloatValuemethodinjava.lang.Number*/publicfloatfloatValue(){return(float)doubleValue();}@Override/**OverridethedoubleValuemethodinjava.lang.Number*/publicdoubledoubleValue(){returnnumerator.doubleValue()/denominator.doubleValue();}@Override/**OverridetheabstractlongValuemethodinjava.lang.Number*/publiclonglongValue(){return(long)doubleValue();}@OverridepublicintcompareTo(Rationalo){if((this.subtract((Rational)o)).getNumerator().compareTo(BigInteger.ZERO)>0)return1;elseif((this.subtract((Rational)o)).getNumerator().compareTo(BigInteger.ZERO)<0)return-1;elsereturn0;}}}16publicclassExercise13_16{publicstaticvoidmain(String口args){Rationalresult=newRational。1);if(args.length!=1){System.out.println("Usage:javaExercise13_16\"operand1operatoroperand2\"");System.exit(1);}String[]tokens=args[0].split("");
switch(tokens[1].charAt(0)){case'+':result=getRational(tokens[0]).add(getRational(tokens[2]));break;case'-':result=getRational(tokens[0]).subtract(getRational(tokens[2]));break;case'*':result=getRational(tokens[0]).multiply(getRational(tokens[2]));break;case'/':result=getRational(tokens[0]).divide(getRational(tokens[2]));}System.out.println(tokens[0]+""+tokens[1]+""+tokens[2]+"="+result);}staticRationalgetRational(Strings){String口st=s.split("/");intnumer=Integer.parseInt(st[0]);intdenom=Integer.parseInt(st[1]);returnnewRational(numer,denom);}}/*Alternatively,youcanuseStringTokenizer.SeeSupplementIII.AAonStringTokenizerasalternativeimportjava.util.StringTokenizer;publicclassExercise15_18{publicstaticvoidmain(String口args){Rationalresult=newRational。1);if(args.length!=3){System.out.println("Usage:javaExercise15_22operand1operatoroperand2");System.exit(0);}switch(tokens[1].charAt(0)){case'+':result=getRational(tokens[0]).add(getRational(tokens[2]));break;case'-':result=getRational(tokens[0]).subtract(getRational(tokens[2]));break;case'*':result=getRational(tokens[0]).multiply(getRational(tokens[2]));break;case'/':result=getRational(tokens[0]).divide(getRational(tokens[2]));
System.out.println(tokens[0]+""+tokens[1]+""+tokens[2]+"="+result);}staticRationalgetRational(Strings){StringTokenizerst=newStringTokenizer(s,"/");intnumer=newInteger(st.nextToken()).intValue();intdenom=newInteger(st.nextToken()).intValue();returnnewRational(numer,denom);}}*/17importjava.util.Scanner;publicclassExercise13_17{publicstaticvoidmain(String口args){Scannerinput=newScanner(System.in);System.out.print("Enterthefirstcomplexnumber:");doublea=input.nextDouble();doubleb=input.nextDouble();Complexc1=newComplex(a,b);System.out.print("Enterthesecondcomplexnumber:");doublec=input.nextDouble();doubled=input.nextDouble();Complexc2=newComplex(c,d);System.out.println("("+c1+")"+"+"+"("+c2+")"+"="+c1.add(c2));System.out.println("("+c1+")"+"-"+"("+c2+")"+"="+c1.subtract(c2));System.out.println("("+c1+")"+"*"+"("+c2+")"+"="+c1.multiply(c2));System.out.println("("+cl+")"+"/"+"("+c2+")"+"="+c1.divide(c2));System.out.println("|"+cl+"|="+c1.abs());Complexc3=(Complex)c1.clone();System.out.println(c1==c3);System.out.println(c3.getRealPart());System.out.println(c3.getImaginaryPart());}}classCompleximplementsCloneable{privatedoublea=0,b=0;
publicComplex。{}Complex(doublea,doubleb){this.a=a;this.b=b;}publicComplex(doublea){this.a=a;}publicdoublegetA(){returna;}publicdoublegetB(){returnb;}publicComplexadd(ComplexsecondComplex){doublenewA=a+secondComplex.getA();doublenewB=b+secondComplex.getB();returnnewComplex(newA,newB);}publicComplexsubtract(ComplexsecondComplex){doublenewA=a-secondComplex.getA();doublenewB=b-secondComplex.getB();returnnewComplex(newA,newB);}publicComplexmultiply(ComplexsecondComplex){doublenewA=a*secondComplex.getA()-b*secondComplex.getB();doublenewB=b*secondComplex.getA()+a*secondComplex.getB();returnnewComplex(newA,newB);}publicComplexdivide(ComplexsecondComplex){doublenewA=(a*secondComplex.getA()+b*secondComplex.getB())/(Math.pow(secondComplex.getA(),2.0)+Math.pow(secondComplex.getB(),2.0));doublenewB=(b*secondComplex.getA()-a*secondComplex.getB())
/(Math.pow(secondComplex.getA(),2.0)+Math.pow(secondComplex.getB(),2.0));returnnewComplex(newA,newB);}publicdoubleabs(){returnMath.sqrt(a*a+b*b);}@OverridepublicStringtoString(){if(b!=0)returna+"+"+b+"i";returna+"";}publicdoublegetRealPart(){returna;}publicdoublegetImaginaryPart(){returnb;}@Override/**ImplementtheclonemethodintheObjectclass*/publicObjectclone(){//Implementittry{returnsuper.clone();//Automaticallyperformashallowcopy}catch(CloneNotSupportedExceptionex){returnnull;}}}18publicclassExercise13_18{publicstaticvoidmain(String[]args){finalintN=100;Rationalsum=newRational();for(inti=1;i<=N;i++)sum=sum.add(newRational(i-1,i));
System.out.println("Thesumofthefirstseriesis"+sum+"="+sum.doubleValue());}}19importjava.math.BigInteger;importjava.util.Scanner;publicclassExercise13_19{publicstaticvoidmain(String口args){Scannerinput=newScanner(System.in);System.out.print("Enteradecimal:");Stringdecimal=input.nextLine();System.out.println("Thefractionnumberis"+getFraction(decimal));}privatestaticRationalgetFraction(Stringdecimal){String[]items=decimal.split("[.]");if(items.length==1){returnnewRational(newBigInteger(items[0]),BigInteger.ONE);}else{Rationalr=newRational(newBigInteger(items[0]+items[1]),getDenominator(items[1].length()));returnr;}}privatestaticBigIntegergetDenominator(intsize){BigIntegerresult=BigInteger.ONE;for(inti=0;i<size;i++)result=result.multiply(newBigInteger("10"));returnresult;}staticclassRationalextendsNumberimplementsComparable<Rational>{//DatafieldsfornumeratoranddenominatorprivateBigIntegernumerator=BigInteger.ZERO;privateBigIntegerdenominator=BigInteger.ONE;
/**Constructarationalwithdefaultproperties*/publicRational。{this(BigInteger.ZERO,BigInteger.ONE);}/**Constructarationalwithspecifiednumeratoranddenominator*/publicRational(BigIntegernumerator,BigIntegerdenominator){BigIntegergcd=gcd(numerator,denominator);if(pareTo(BigInteger.ZERO)<0)this.numerator=numerator.multiply(newBigInteger("-1")).divide(gcd);elsethis.numerator=numerator.divide(gcd);this.denominator=denominator.abs().divide(gcd);}/**FindGCDoftwonumbers*/privatestaticBigIntegergcd(BigIntegern,BigIntegerd){BigIntegern1=n.abs();BigIntegern2=d.abs();BigIntegergcd=BigInteger.ONE;for(BigIntegerk=BigInteger.ONE;pareTo(n1)<=0&&pareTo(n2)<=0;k=k.add(BigInteger.ONE)){if(n1.remainder(k).equals(BigInteger.ZERO)&&n2.remainder(k).equals(BigInteger.ZERO))gcd=k;}returngcd;}/**Returnnumerator*/publicBigIntegergetNumerator(){returnnumerator;}/**Returndenominator*/publicBigIntegergetDenominator(){returndenominator;}
/**Addarationalnumbertothisrational*/publicRationaladd(RationalsecondRational){BigIntegern=numerator.multiply(secondRational.getDenominator()).add(denominator.multiply(secondRational.getNumerator()));BigIntegerd=denominator.multiply(secondRational.getDenominator());returnnewRational(n,d);}/**Subtractarationalnumberfromthisrational*/publicRationalsubtract(RationalsecondRational){BigIntegern=numerator.multiply(secondRational.getDenominator()).subtract(denominator.multiply(secondRational.getNumerator()));BigIntegerd=denominator.multiply(secondRational.getDenominator());returnnewRational(n,d);}/**Multiplyarationalnumbertothisrational*/publicRationalmultiply(RationalsecondRational){BigIntegern=numerator.multiply(secondRational.getNumerator());BigIntegerd=denominator.multiply(secondRational.getDenominator());returnnewRational(n,d);}/**Dividearationalnumberfromthisrational*/publicRationaldivide(RationalsecondRational){BigIntegern=numerator.multiply(secondRational.getDenominator());BigIntegerd=denominator.multiply(secondRational.numerator);returnnewRational(n,d);}@OverridepublicStringtoString(){if(denominator.equals(BigInteger.ONE))returnnumerator+"";elsereturnnumerator+"/"+denominator;}@Override/**OverridetheequalsmethodintheObjectclass*/publicbooleanequals(Objectparml){if((this.subtract((Rational)(parm1))).getNumerator().equals(BigInteger.ONE))returntrue;elsereturnfalse;
@Override/**OverridethehashCodemethodintheObjectclass*/publicinthashCode(){returnnewDouble(this.doubleValue()).hashCode();}@Override/**OverridetheabstractintValuemethodinjava.lang.Number*/publicintintValue(){return(int)doubleValue();}@Override/**OverridetheabstractfloatValuemethodinjava.lang.Number*/publicfloatfloatValue(){return(float)doubleValue();}@Override/**OverridethedoubleValuemethodinjava.lang.Number*/publicdoubledoubleValue(){returnnumerator.doubleValue()/denominator.doubleValue();}@Override/**OverridetheabstractlongValuemethodinjava.lang.Number*/publiclonglongValue(){return(long)doubleValue();}@OverridepublicintcompareTo(Rationalo){if((this.subtract((Rational)o)).getNumerator().compareTo(BigI
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- Module 4 Unit 2 I was very nervous(說課稿)-2023-2024學年外研版(一起)英語四年級下冊001
- 25 憶讀書說課稿-2024-2025學年五年級上冊語文統(tǒng)編版
- 1 我們的好朋友-《好朋友真友誼》(說課稿)-部編版道德與法治四年級下冊
- 2025年度綠色環(huán)保外墻涂料施工合同標準范文2篇
- 2025年房產(chǎn)獨家出售合同3篇
- 2025年度食堂食材采購合同及供應鏈金融服務協(xié)議3篇
- 第五章一元函數(shù)的導數(shù)及其應用本章小結(jié)第二課時說課稿-2023-2024學年高二下學期數(shù)學人教A版(2019)選擇性必修第二冊
- 足球大腿、胸部停球 說課稿-2023-2024學年高二上學期體育與健康人教版必修第一冊
- 2025年外研銜接版七年級物理上冊月考試卷
- 2025年人教A版九年級物理下冊階段測試試卷
- 影視作品價值評估-洞察分析
- 公司員工出差車輛免責協(xié)議書
- 2023年浙江杭州師范大學附屬醫(yī)院招聘聘用人員筆試真題
- DL∕T 1631-2016 并網(wǎng)風電場繼電保護配置及整定技術規(guī)范
- 藥用輔料聚乙二醇400特性、用法用量
- 《中小學機器人教育研究(論文)11000字》
- GB/T 22085.1-2008電子束及激光焊接接頭缺欠質(zhì)量分級指南第1部分:鋼
- 全過程人民民主學習心得體會
- 2023年上海期貨交易所招聘筆試題庫及答案解析
- word企業(yè)管理封面-可編輯
- 全國醫(yī)療服務價格項目規(guī)范(2012年版)-工作手冊
評論
0/150
提交評論