中国高校课件下载中心 》 教学资源 》 大学文库

《Java程序设计》课程教学课件(PPT讲稿)第5章 方法

文档信息
资源类别:文库
文档格式:PPT
文档页数:12
文件大小:249.5KB
团购合买:点击进入团购
内容简介
《Java程序设计》课程教学课件(PPT讲稿)第5章 方法
刷新页面文档预览

第5章方法Liang,Introduction toJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 第5章 方 法

重载方法重载max方法 max (double numl, doublepublic static doublenum2)if (numl > num2)return numl;elsenum2 ;returnTestMethodOverloadingLiang,Introduction toJava Programming,EighthEdition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 2 重载方法 重载 max方法 public static double max(double num1, double num2) { if (num1 > num2) return num1; else return num2; } TestMethodOverloading

歧义调用有时可能会有两个或两个以上和方法调用相匹配,但是编译器无法判断哪个是最精确的匹配。这个问题被称为歧义调用歧义调用月(ambiguous invocation是一个编译错误Liang,Introduction toJava Programming,EighthEdition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 3 歧义调用 有时可能会有两个或两个以上和方法调 用相匹配,但是编译器无法判断哪个是 最精确的匹配。这个问题被称为歧义调 用(ambiguous invocation)。 歧义调用 是一个编译错误

歧义调用public class AmbiguousOverloadingpublic static void main(string[l args) (System.out.println(max(1,2));1public static double max(int numl, double num2) (if(numl > num2)return numlielsereturn num2;1public static double max(double numl, int num2) {1if(numl > num2)return numl;elsereturn num2;1AoLtoJayTogrammng,EomEorSOrightsreserved.0132130807

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 4 歧义调用 public class AmbiguousOverloading { public static void main(String[] args) { System.out.println(max(1, 2)); } public static double max(int num1, double num2) { if (num1 > num2) return num1; else return num2; } public static double max(double num1, int num2) { if (num1 > num2) return num1; else return num2; } }

问题:将十进制数转换成十六进制数编写一个将十进制整数转换成十六进制整数的程序。Decimal2HexConversionLiang,Introduction toJava Programming,EighthEdition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 5 问题:将十进制数转换成十六进制数 编写一个将十进制整数转换成十六进制整数 的程序。 Decimal2HexConversion

Math类类常量- PIE-类方法一三角函数方法一指数函数方法-取整方法-min、max、abs和random方法Liang,Introduction toJava Programming,EighthEdition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 6 Math类 类常量 – PI – E 类方法 – 三角函数方法 – 指数函数方法 – 取整方法 – min、max、abs和random方法

三角函数方法举例:sin(doubleacos(double返回0.0Math.sin(0)/6)返回0.5tan(doubleMath.sin(Math.PI0Math.sin(Math.PI / 2)返回1.0acos(doublea返回1.0Math.cos (0)asin(doubleMath.cos (Math.PI / 6)返回0.866aMath.cos(Math.PI / 2)返回Oatan(doublea弧度toRadians(90)Liang,Introduction toJava Programming.EighthEdition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 7 三角函数方法 sin(double a) cos(double a) tan(double a) acos(double a) asin(double a) atan(double a) 弧度 toRadians(90) 举例: Math.sin(0) 返回 0.0 Math.sin(Math.PI / 6) 返回 0.5 Math.sin(Math.PI / 2)返回 1.0 Math.cos(0) 返回 1.0 Math.cos(Math.PI / 6)返回 0.866 Math.cos(Math.PI / 2)返回 0

指数函数方法举例:exp(double a)返回e的a次方。返回2.71Math.exp(1)log(double a)Math.1og(2.71)返回1.0返回a 的自然对数Math.pow(2,3) 返回8.0logl0(double a)Math.pow(3,2)返回9.0返回以10为底的a的对数。返回Math.pow(3.5, 2.5)22.91765pow(doublea,double b)返回2.0Math.sqrt(4)返回a的b次方。返回3.24Math.sqrt(10.5)sqrt(double a)返回a的平方根。Liang,Introduction toJava Programming,EighthEdition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 8 指数函数方法 exp(double a) 返回 e 的 a次方。 log(double a) 返回 a 的自然对数。 log10(double a) 返回 以10为底的a的对数。 pow(double a, double b) 返回 a 的b次方。 sqrt(double a) 返回 a 的平方根。 举例: Math.exp(1) 返回 2.71 Math.log(2.71) 返回 1.0 Math.pow(2, 3) 返回 8.0 Math.pow(3, 2) 返回 9.0 Math.pow(3.5, 2.5) 返回 22.91765 Math.sqrt(4) 返回 2.0 Math.sqrt(10.5) 返回 3.24

取整方法doubleceil(double x)x取向下离它最接近的整数,这个整数将以一个double类型的值返回double floor(doublex)x取向下离它最接近的整数,这个整数将以一个double类型的值返回。double rint(double x)x取离它最接近的整数。如果x距离两个整数同样接近,就返回成偶数的double类型值int round(float x)返回(int)Math.floor(x+0.5)。long round(double x)返回(long)Math.floor(x+0.5)。Liang,IntroductiontoJava Programming,EighthEdition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 9 取整方法 double ceil(double x) x取向下离它最接近的整数,这个整数将以一个double类型的值返回。 double floor(double x) x取向下离它最接近的整数,这个整数将以一个double 类型的值返回。 double rint(double x) x取离它最接近的整数。如果x 距离两个整数同样接近,就返回成偶数的 double类型值。 int round(float x) 返回 (int)Math.floor(x+0.5)。 long round(double x) 返回 (long)Math.floor(x+0.5)

取整方法举例返回3.0Math.cei1(2.1)返回2.0Math.ceil(2.0)返回-2.0Math.ceil(-2.0)返回 -2.0Math.ceil(-2.1)返回2.0Math.floor(2.1)返回2.0Math.floor (2.0)返回-2.0Math.floor (-2.0)返回-3.0Math.floor(-2.1)Math.rint(2.1)返回 2.0Math.rint(2.0)返回 2.0返回-2.0Math.rint(-2.0)返回-2.0Math.rint(-2.1)返回2.0Math.rint(2.5)返回-2.0Math.rint(-2.5)返回3Math.round(2.6f)返回2Math.round(2.0)返回-2Math.round(-2.0f)Math.round(-2.6)返回-3Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.All10rights reserved.0132130807

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 10 取整方法举例 Math.ceil(2.1) 返回 3.0 Math.ceil(2.0) 返回 2.0 Math.ceil(-2.0) 返回 –2.0 Math.ceil(-2.1) 返回 -2.0 Math.floor(2.1) 返回 2.0 Math.floor(2.0) 返回 2.0 Math.floor(-2.0) 返回 –2.0 Math.floor(-2.1) 返回 -3.0 Math.rint(2.1) 返回 2.0 Math.rint(2.0) 返回 2.0 Math.rint(-2.0) 返回 –2.0 Math.rint(-2.1) 返回 -2.0 Math.rint(2.5) 返回 2.0 Math.rint(-2.5) 返回 -2.0 Math.round(2.6f) 返回 3 Math.round(2.0) 返回 2 Math.round(-2.0f) 返回 -2 Math.round(-2.6) 返回 -3

共12页,试读已结束,阅读完整版请下载
刷新页面下载完整文档
VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
相关文档