《英文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