英文java讲义lecture14-methodspart3

上传人:小** 文档编号:47860610 上传时间:2018-07-05 格式:PDF 页数:43 大小:576.37KB
返回 下载 相关 举报
英文java讲义lecture14-methodspart3_第1页
第1页 / 共43页
英文java讲义lecture14-methodspart3_第2页
第2页 / 共43页
英文java讲义lecture14-methodspart3_第3页
第3页 / 共43页
英文java讲义lecture14-methodspart3_第4页
第4页 / 共43页
英文java讲义lecture14-methodspart3_第5页
第5页 / 共43页
点击查看更多>>
资源描述

《英文java讲义lecture14-methodspart3》由会员分享,可在线阅读,更多相关《英文java讲义lecture14-methodspart3(43页珍藏版)》请在金锄头文库上搜索。

1、What is a method? A method is sequence of programming statements that perform an action it can take in input parameters it can return a result Example double Math.pow(double a, double b) takes double arguments, returns a double which is the value of the first argument raised to the power of the seco

2、nd argumentpublic static int getLetterNum(char d) return c A; public static void sayHi() System.out.println(“Hi!”); Method ModifiersReturn TypeMethod NameParentheses around optional parameter listMethod header Specifies the entire interface someone just needs to read the comment and the interface to

3、 know how to use the method Arguments and parameters must match in: Number Order AND datatype widening conversions allowedMethod header returnType: primitive type, an object (e.g., String), or void methodName: follow Java identifier rules (e.g., cannot start with a digit, allowed characters) Java co

4、ding conventions: lower case letter, start with a verb Parameter list: this is where the parameters are declared Error if you declare them again in the method body Remember! They are copy of the values of the argumentsCalling or Invoking a method Within any expression Assignment double taxes = TAX_R

5、ATE * calcGrossPay(hours, rate); Standalone for methods that dont return a value System.out.println(“Hi”); No parameters but a return num = rollDice(); No parameters, no return System.out.println();Calling methodspublic static int getLetterNum(char d)When I call this method I must do two things: -pu

6、t an expression of type char in between ( ) -put the method call somewhere that an int is valid.Using methodspublic static int getLetterNum(char d)Example: int result = getLetterNum(c); System.out.println(getLetterNum(e); System.out.println(getLetterNum(f) + getLetterNum(g); int wow = getLetterNum(c

7、har)getLetterNum(s)What about these?public static int getLetterNum(char d) getLetterNum(); getLetterNum(30); getLetterNum(d); int x = getLetterNum(e); System.out.println(Math.sqrt(getLetterNu m(d); int q = getLetterNum(d); int r = getLetterNum(“d“);What about these?public static int getLetterNum(cha

8、r d) getLetterNum(); - no arguments given getLetterNum(30); getLetterNum(d); int x = getLetterNum(e); System.out.println(Math.sqrt(getLetterNu m(d); int q = getLetterNum(d); int r = getLetterNum(“d“);What about these?public static int getLetterNum(char d) getLetterNum(); - no arguments given getLett

9、erNum(30); - needs char not int getLetterNum(d); int x = getLetterNum(e); System.out.println(Math.sqrt(getLetterNu m(d); int q = getLetterNum(d); int r = getLetterNum(“d“);What about these?public static int getLetterNum(char d) getLetterNum(); - no arguments given getLetterNum(30); - needs char not

10、int getLetterNum(d);-compiles but ignores return value int x = getLetterNum(e); System.out.println(Math.sqrt(getLetterNu m(d); int q = getLetterNum(d); int r = getLetterNum(“d“);What about these?public static int getLetterNum(char d) getLetterNum(); - no arguments given getLetterNum(30); - needs cha

11、r not int getLetterNum(d);-compiles but ignores return value int x = getLetterNum(e);- great! System.out.println(Math.sqrt(getLetterNu m(d); int q = getLetterNum(d); int r = getLetterNum(“d“);What about these?public static int getLetterNum(char d) getLetterNum(); - no arguments given getLetterNum(30

12、); - needs char not int getLetterNum(d);-compiles but ignores return value int x = getLetterNum(e);- great! System.out.println(Math.sqrt(getLetterNu m(d);-prints the sqrt of the result int q = getLetterNum(d); int r = getLetterNum(“d“);What about these?public static int getLetterNum(char d) getLette

13、rNum(); - no arguments given getLetterNum(30); - needs char not int getLetterNum(d);-compiles but ignores return value int x = getLetterNum(e);- great! System.out.println(Math.sqrt(getLetterNum(d );-prints the sqrt of the result int q = getLetterNum(d);-bad unless d was declared as type char int r =

14、 getLetterNum(“d“);What about these?public static int getLetterNum(char d) getLetterNum(); - no arguments given getLetterNum(30); - needs char not int getLetterNum(d);-compiles but ignores return value int x = getLetterNum(e);- great! System.out.println(Math.sqrt(getLetterNum(d );-prints the sqrt of

15、 the result int q = getLetterNum(d);-bad unless d was declared as type char int r = getLetterNum(“d“);-bad since Strings cant get converted to char automatically5-16Passing Multiple Argumentsdouble num2=5,num1=10; double result = showSum(num2, num1); public static double showSum(double num1, double num2) double sum;/to hold the sum sum = num1 + num2; System.out.println(“The sum is “ + sum); return sum; The value 5 is copied into the num1 parameter.The value 10 is copied into the num2 parameter.Data type compatibility Arguments are copied into parameters in order, regardless of their

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业/管理/HR > 宣传企划

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号