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

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

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

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

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

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

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; } }

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

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

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

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

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)

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每日次数-->可用次数-->下载券;
相关文档