资源预览内容
第1页 / 共43页
第2页 / 共43页
第3页 / 共43页
第4页 / 共43页
第5页 / 共43页
第6页 / 共43页
第7页 / 共43页
第8页 / 共43页
第9页 / 共43页
第10页 / 共43页
亲,该文档总共43页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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 second 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 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 coding 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_RATE * 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: -put 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(char)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(char 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 getLetterNum(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 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 char 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); - 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) 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 the result int q = getLetterNum(d);-bad unless d was declared as type char int r = 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 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
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号