Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
9/1/2011 Methods: Parameters and Return Values CSC116: Intro to Java Programming © Sarah Heckman 1 Outline • Generalization of Tasks • Method Parameters • Formal and Actual Parameters • Exercise 10 • Parameter Limitations • Multiple Parameters CSC116: Intro to Java Programming © Sarah Heckman • • • • • • • Method Javadoc Method Overloading Return Values Return Statements Method Javadoc Math Class Exercise 11 2 1 9/1/2011 Generalization of Tasks • Related tasks can be generalized using parameters • Parameters: Any of a set of characteristics that distinguish different member of a family of tasks • Evolve how we use methods calculatePosition(); CSC116: Intro to Java Programming © Sarah Heckman 3 Example: calculatePosition() public class Position { public static void main(String [] args) { calculatePosition(4.3); //From Exercise 08 calculatePosition(3 * 4 – 5); } public static void calculatePosition(double time) { double initialPosition = 15.2; double initialVelocity = 36.8; double acceleration = 17.20; double position = initialPosition + (initialVelocity * time) + (0.5 * acceleration * time * time); System.out.println("Position: " + position); } } CSC116: Intro to Java Programming © Sarah Heckman 4 2 9/1/2011 Method Syntax Template public static void <name> (<type> <name>) { <stmt or variable declaration>; <stmt or variable declaration>; ... <stmt or variable declaration>; } //calling a parameterized method <name>(<expression>); CSC116: Intro to Java Programming © Sarah Heckman 5 Formal and Actual Parameters • Formal Parameter: A variable that appears in the parentheses in method header that is used to generalize the method’s behavior – public static void calculatePosition(double time) • Actual Parameters: A specific value of expression that appears inside parentheses in a method call – calculatePosition(3 * 4 – 5); CSC116: Intro to Java Programming © Sarah Heckman 6 3 9/1/2011 What is the output? public class Square { public static void main(String [] args) { printArea(3); printArea(17 / 2); int x = 25; printArea(37 – x + 1); } public static void printArea(int side) { System.out.println(“Area is: ” + side * side); } } CSC116: Intro to Java Programming © Sarah Heckman 7 Parameter Limitations • The parameter of a method only has scope within the method – side can only be referenced in printArea() – main cannot use the variable side • Parameters cannot be used to change values of primitive types outside of a method – If there was a variable also named side in the main method, any changes to side in printArea() would ONLY affect the variable side in printArea() CSC116: Intro to Java Programming © Sarah Heckman 9 4 9/1/2011 Multiple Parameters public static void <name> ( <type> <name>, <type> <name>, ..., <type> <name>) { <stmt or variable declaration>; <stmt or variable declaration>; ... <stmt or variable declaration>; } //calling a method with multiple parameters <name>(<expr>, <expr>, ..., <expr>); CSC116: Intro to Java Programming © Sarah Heckman 10 Updated Method Javadoc /** * Describe what * @param <name> * @param <name> * . . . * @param <name> */ CSC116: Intro to Java Programming © Sarah Heckman the method does <description> <description> <description> 11 5 9/1/2011 Example: calculatePosition() public class Position { public static void main(String [] args) { calculatePosition(10, 2.5, 25.0, 9.81); } public static void calculatePosition(double time, double initialPosition, double initialVelocity, double acceleration) { double position = initialPosition + (initialVelocity * time) + (0.5 * acceleration * time * time); System.out.println("Position: " + position); } } CSC116: Intro to Java Programming © Sarah Heckman 12 Method Overloading • The ability to define two or more different methods with the same name but different method signatures. • Method Signatures: The name of a method, along with the number and types of parameters calculatePosition() calculatePosition(int) calculatePosition(int, int) CSC116: Intro to Java Programming © Sarah Heckman 13 6 9/1/2011 Return Values • To send a value out as a result of a method that can be used in an expression in your program public static double calculatePosition(int time) • Void methods do not return any value public static void calculatePosition(int time) CSC116: Intro to Java Programming © Sarah Heckman 14 Return Statements • Specifies the value to return, terminating the method return <expression>; • Statements after the return statement will not be executed • Methods with a non‐void return type must contain one or more return statement(s). – The compiler will let you know if you’re missing one. CSC116: Intro to Java Programming © Sarah Heckman 15 7 9/1/2011 Methods with Return Values public static <type> <name> ( <type> <name>, <type> <name>, ..., <type> <name>) { <stmt or variable declaration>; <stmt or variable declaration>; ... <stmt or variable declaration>; return <expression>; } CSC116: Intro to Java Programming © Sarah Heckman 16 Updated Method Javadoc /** * Describe what the method does * @param <name> <description> * @param <name> <description> * . . . * @param <name> <description> * @return <description> */ CSC116: Intro to Java Programming © Sarah Heckman 17 8 9/1/2011 Example: calculatePosition() public class Position { public static void main(String [] args) { double position = calculatePosition(10, 2.5, 25.0, 9.81); System.out.println("Position: " + position); } public static double calculatePosition(double time, double initialPosition, double initialVelocity, double acceleration) { double position = initialPosition + (initialVelocity * time) + (0.5 * acceleration * time * time); return position; } } CSC116: Intro to Java Programming © Sarah Heckman 18 What is the output? public class Mystery6 { public static void main (String [] args) { int x = 1, y = 2, z = 3; z = mystery(x, z, y); System.out.println(x + “ “ + y + “ “ + x = mystery(z, z, x); System.out.println(x + “ “ + y + “ “ + y = mystery(y, y, z); System.out.println(x + “ “ + y + “ “ + } public static int mystery(int z, int x, int z--; x = 2 * y + z; y = x - 1; System.out.println(y + “ “ + z); return x; } } CSC116: Intro to Java Programming © Sarah Heckman z); z); z); y) { 19 9 9/1/2011 Math Class • Java provides many classes as part of the Java class libraries that provide implementations of many common solutions • Math class has predefined constants and common mathematical functions • Since the mathematical functions/constants are in another class, we use dot notation to call the element <class name>.<element> • See Table 3.2 on p. 150 for useful Math methods CSC116: Intro to Java Programming © Sarah Heckman 20 Example: calculatePosition() public class Position { public static void main(String [] args) { double position = calculatePosition(10, 2.5, 25.0, 9.81); System.out.println("Position: " + position); } public static double calculatePosition(double time, double initialPosition, double initialVelocity, double acceleration) { double position = initialPosition + (initialVelocity * time) + (0.5 * acceleration * Math.pow(time, 2)); return position; } } CSC116: Intro to Java Programming © Sarah Heckman 21 10 9/1/2011 Example: calculateHypotenuse() • Write a method that calculates the length of the hypotenuse of a right triangle given the size of the other two legs. (Pythagorean Theorem) CSC116: Intro to Java Programming © Sarah Heckman 22 Javadoc • • • • About Javadoc Javadoc Comments Javadoc Tags “Javadocing” • a class • a main method • a method with parameters and return value • a void method • class instance variables 11 9/1/2011 About Javadoc • javadoc is a Java utility used to produce professional documentation such as the Java API • We will use javadoc comments to – describe what a class does and give its author – describe what a method does and give information about its parameter(s) and return value, if any – give information about class instance variables, if any Javadoc Comments • Javadoc comments start with /** and end with */ • Note that there are 2 *’s after the initial / This is what distinguishes Javadoc comments from C‐style comments that start with /* • Javadoc comments are placed immediately before whatever they are commenting 12 9/1/2011 Javadoc Tags • Javadoc tags allow us to specify the author of a class and give information about a method’s parameter(s) and return value. • The tags start with an @ sign. These are the tags we will be using: @author ‐ Use to specify the author of a class. @param ‐ Use a param tag for each method parameter. Give the name of the parameter followed by a description of the parameter. @return ‐ Use one return tag for non‐void methods to describe its return value • There are many other Javadoc tags that we will not be using. “Javadocing” a Class import java.util.*; /** * Calculates Body Mass Index (BMI) * @author Bob Jones */ public class BMICalculator { 13 9/1/2011 “Javadocing” a main method /** * Repeatedly prompts the user for weight *(lbs.) and height(in.) and outputs the BMI. * @param args command line arguments */ public static void main(String[] args) { “Javadocing” a method /** * Calculates Body Mass Index (BMI) * @param weight weight in pounds * @param height height in inches * @return Body Mass Index (BMI) */ public static double calculateBMI( double weight, double height) { 14 9/1/2011 “Javadocing” a void method /** * Outputs a word a given number of times * @param word word to output * @param number number of times to output * the word */ public static void printWords(String word, int number) { “Javadocing” class instance variables public class Car { /** * Color of the car */ private Color color; /** Make of the car */ private String make; } 15