java概述课件05方法_第1页
java概述课件05方法_第2页
java概述课件05方法_第3页
java概述课件05方法_第4页
java概述课件05方法_第5页
已阅读5页,还剩70页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、1,第5,2,引言,从1到10的整数之和,从20到30,从35到45,分别为?,3,问题,int sum = 0; for (int i = 1; i = 10; i+) sum += i; System.out.println(Sum from 1 to 10 is + sum); sum = 0; for (int i = 20; i = 30; i+) sum += i; System.out.println(Sum from 20 to 30 is + sum); sum = 0; for (int i = 35; i = 45; i+) sum += i; System.out.pr

2、intln(Sum from 35 to 45 is + sum);,4,问题,int sum = 0; for (int i = 1; i = 10; i+) sum += i; System.out.println(Sum from 1 to 10 is + sum); sum = 0; for (int i = 20; i = 30; i+) sum += i; System.out.println(Sum from 20 to 30 is + sum); sum = 0; for (int i = 35; i = 45; i+) sum += i; System.out.println

3、(Sum from 35 to 45 is + sum);,5,解决,public static int sum(int i1, int i2) int sum = 0; for (int i = i1; i = i2; i+) sum += i; return sum; public static void main(String args) System.out.println(Sum from 1 to 10 is + sum(1, 10); System.out.println(Sum from 20 to 30 is + sum(20, 30); System.out.println

4、(Sum from 35 to 45 is + sum(35, 45); ,6,学习目标,定义方法、调用待返回值的方法、调用无返回值的方法 、按值传参(5.2-5.5). 开发模块化的、易读、易调试和易维护的可重用代码(5.6). 编写方法实现十进制转化十六进制(5.7). 使用方法的重载、理解歧义重载(5.8). 确定变量的作用域 (5.9). 使用Math类中的方法解决数学问题 (5.10-5.11). 在软件开发中应用方法抽象的概念 (5.12). 使用逐步求精的办法设计和实现方法 (5.12).,7,定义方法,方法许多语句的组合,共同组成这种操作。,8,方法的特征,方法定义(speci

5、fication):,9,形式参数,被定义在方法头上的参数被称为形式参数。,10,实际参数,当一个方法被调用的时候所传递的参数被称为实际参数。,11,返回类型,方法可以返回一个值。返回类型是一种数据类型,是由方法头确定的。如果方法不返回一个值,则该返回类型是关键字void。例如,这个返回类型在这个main方法里是void。,12,调用方法,测试方法: 这个程序演示调用一个方法去获取最大值。,TestMax,13,调用方法,animation,14,调用方法,i is now 5,animation,15,调用方法,j is now 2,animation,16,调用方法,invoke max(

6、i, j),animation,17,调用方法,invoke max(i, j) Pass the value of i to num1 Pass the value of j to num2,animation,18,调用方法,declare variable result,animation,19,调用方法,(num1 num2) is true since num1 is 5 and num2 is 2,animation,20,调用方法,result is now 5,animation,21,调用方法,return result, which is 5,animation,22,调用

7、方法,return max(i, j) and assign the return value to k,animation,23,调用方法,Execute the print statement,animation,24,注意,一个有返回类型的方法必须有对应的return方法。如下左边看上去是正确的,但是在编译时候,编译器会误认为方法可能没有返回值。,为了解决这个问题,删除黄色部分,使编译器便可以理解为不论如何这个方法一定有返回值。,25,从其他类的复用方法,注:一种方法的好处是可重用的。该方法可以被另外的任类调用。如果我们创建一个新的类的测试,我们可以调用方法使用classname.met

8、hodname(例如,TestMax.max()。,26,调用堆栈,27,调用堆栈,i is declared and initialized,animation,28,调用堆栈,j is declared and initialized,animation,29,调用堆栈,Declare k,animation,30,调用堆栈,Invoke max(i, j),animation,31,调用堆栈,pass the values of i and j to num1 and num2,animation,32,调用堆栈,pass the values of i and j to num1 an

9、d num2,animation,33,调用堆栈,(num1 num2) is true,animation,34,调用堆栈,Return result and assign it to k,animation,35,调用堆栈,Assign num1 to result,animation,36,调用堆栈,Execute print statement,animation,37,void 方法举例,这个方法没有返回类型。,TestVoidMethod,38,参数的值传递,public static void nPrintln(String message, int n) for (int i

10、= 0; i n; i+) System.out.println(message); ,如果我们这样调用方法 nPrintln(“Welcome to Java”, 5); 会有怎样的输出呢?? 如果我们这样调用方法 nPrintln(“Computer Science”, 15); 会有怎样的输出呢?,39,参数的值传递,这个程序演示了传值的方法。,Increment,40,参数的值传递,由值测试通过 这个程序演示了传值的方法。,TestPassByValue,41,按值传递(续),42,模块化代码,使用方法可以减少冗余的代码,提高代码的复用性。方法也可以用来模块化代码,以提高程序的质量。,

11、GreatestCommonDivisorMethod,PrimeNumberMethod,43,重载方法,重载 max 方法 public static double max(double num1, double num2) if (num1 num2) return num1; else return num2; ,TestMethodOverloading,44,模糊的调用,有时可能有两个或更多的方法被一个方法调用,但编译器无法确定最匹配。这是被称为模糊调用。模糊调用是一个编译错误。,45,模糊的调用,public class AmbiguousOverloading public s

12、tatic 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; ,46,问题:将十进制转化为十六进制,写一个方法实现十进制转化十六进制,Decimal2HexConver

13、sion,47,局部变量的作用域,局部变量:变量定义在一个方法。 范围:程序的一部分,其中的变量可以引用。 这种局部变量的作用域是从它被定义起到这块代码的结束。 一个局部变量在使用前必须先声明。 我们可以在不同的模块中定义名字相同的变量,但我们不能在一个嵌套模块中定义相同变量。,48,局部变量的作用域,在for循环头中初始声明的一个变量,其作用域是整个for循环。但是在for循环体内声明的变量,其作用域只限于循环体内部,是从他的声明处开始,到包含该变量的块结束为止。,49,局部变量的作用域,50,局部变量的作用域,/ Fine with no errors public static void

14、 correctMethod() int x = 1; int y = 1; / i is declared for (int i = 1; i 10; i+) x += i; / i is declared again for (int i = 1; i 10; i+) y += i; ,51,局部变量的作用域,/ With no errors public static void incorrectMethod() int x = 1; int y = 1; for (int i = 1; i 10; i+) int x = 0; x += i; ,52,方法的抽象,方法的实现对用户隐藏在

15、“黑匣子”中。,53,方法的好处,一次编写多处调用 信息的隐蔽性: 把关键核心信息不暴露给调用方法的对象 减少复杂性,54,Math 类,Class 常数: PI E Class 方法: 三角函数 指数函数 舍入方法 最小, 最大, 绝对值, 和随机值方法,55,三角函数,sin(double a) cos(double a) tan(double a) acos(double a) asin(double a) atan(double a),Radians toRadians(90),Examples: Math.sin(0) returns 0.0 Math.sin(Math.PI / 6

16、) returns 0.5 Math.sin(Math.PI / 2) returns 1.0 Math.cos(0) returns 1.0 Math.cos(Math.PI / 6) returns 0.866 Math.cos(Math.PI / 2) returns 0,56,指数函数,exp(double a) Returns e raised to the power of a. log(double a) Returns the natural logarithm of a. log10(double a) Returns the 10-based logarithm of a.

17、 pow(double a, double b) Returns a raised to the power of b. sqrt(double a) Returns the square root of a.,Examples: Math.exp(1) returns 2.71 Math.log(2.71) returns 1.0 Math.pow(2, 3) returns 8.0 Math.pow(3, 2) returns 9.0 Math.pow(3.5, 2.5) returns 22.91765 Math.sqrt(4) returns 2.0 Math.sqrt(10.5) r

18、eturns 3.24,57,取整方法,double ceil(double x) X向上舍入到最近的整数。返回一个double值。 double floor(double x) X是向下舍入到最接近的整数。返回一个double值。 double rint(double x) X舍入到最近的整数。如果X是同样接近两个整数,返回一个。 int round(float x) Return (int)Math.floor(x+0.5). long round(double x) Return (long)Math.floor(x+0.5).,58,取整方法 举例,Math.ceil(2.1) ret

19、urns 3.0 Math.ceil(2.0) returns 2.0 Math.ceil(-2.0) returns 2.0 Math.ceil(-2.1) returns -2.0 Math.floor(2.1) returns 2.0 Math.floor(2.0) returns 2.0 Math.floor(-2.0) returns 2.0 Math.floor(-2.1) returns -3.0 Math.rint(2.1) returns 2.0 Math.rint(2.0) returns 2.0 Math.rint(-2.0) returns 2.0 Math.rint(

20、-2.1) returns -2.0 Math.rint(2.5) returns 2.0 Math.rint(-2.5) returns -2.0 Math.round(2.6f) returns 3 Math.round(2.0) returns 2 Math.round(-2.0f) returns -2 Math.round(-2.6) returns -3,59,最小、最大绝、对值,max(a, b)and min(a, b) Returns the maximum or minimum of two parameters. abs(a) Returns the absolute v

21、alue of the parameter. random() Returns a random double valuein the range 0.0, 1.0).,Examples: Math.max(2, 3) returns 3 Math.max(2.5, 3) returns 3.0 Math.min(2.5, 3.6) returns 2.5 Math.abs(-2) returns 2 Math.abs(-2.1) returns 2.1,60,随机数方法,生成一个随机的双值大于或等于0和小于1(,Examples:,In general,61,案例研究:产生随机的字符,计算机

22、程序处理的是数值数据和字符。前面已经看到了许多涉及数值数据的例子,了解字符和如何处理字符很重要。 正如前边所介绍的,每个字符都有一个唯一的在十六进制数0到FFFF即(65535)之间的统一码。生成一个随机字符就是使用下面的表达式,生成从0-65535之间的一个随机整数。(注意:因为0=Math.random()1.0,必须给65535加上1) (int)(Math.random() * (65535 + 1),62,案例研究:产生随机的字符,现在让我们来考虑如何生成一个随机小写字符。小写字母的统一码是一串连续的整数,从小写字母a的统一码开始,然后是b、c、.和z的统一码。a的统一码是: (in

23、t)a 因此,(int)a和(int)z之间的随机整数是 (int)(int)a + Math.random() * (int)z - (int)a + 1),63,案例研究:产生随机的字符,正如2.13.3节中所讨论的,所有的数字操作符都可以应用到char操作数上。如果另一个操作数是数字或者字符,那么char型操作数就会被转换成数字。这样前面表达式就可以简化为如下” a + Math.random() * (z - a + 1) 这样随机小写字母是: (char)(a + Math.random() * (z - a + 1),64,RandomCharacter 类,/ RandomCha

24、racter.java: Generate random characters public class RandomCharacter /* Generate a random character between ch1 and ch2 */ public static char getRandomCharacter(char ch1, char ch2) return (char)(ch1 + Math.random() * (ch2 - ch1 + 1); /* Generate a random lowercase letter */ public static char getRandomLowerCaseLetter() return getRandomCharacter(a, z); /* Generate a random uppercase letter */ public static char getRandomUpperCaseLetter() return getRandomCharacter(A, Z); /* Generate a random digit character */ public static char getRandomDigit

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论