Presentation is loading. Please wait.

Presentation is loading. Please wait.

©2004 Brooks/Cole Chapter 6 Methods. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Methods We've already seen examples of using methods.

Similar presentations


Presentation on theme: "©2004 Brooks/Cole Chapter 6 Methods. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Methods We've already seen examples of using methods."— Presentation transcript:

1 ©2004 Brooks/Cole Chapter 6 Methods

2 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Methods We've already seen examples of using methods y = Math.sqrt( x); df.format( price); System.out.println( "Output"); Many methods need to be given some data –method call must include the right number and types of data

3 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Calling and Passing Data to a Method

4 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Methods Every method is defined in a class Two kinds of method –instance methods associated with an object ( df.format ) called with objectName.methodName() instance methods may use object data –static methods belong to the class ( Math.sqrt ) call with ClassName.methodName() class methods do not have access to instance data

5 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 General Format of a Method Method definition has two parts –header (signature) - specifies access, return value, name and parameters –body - block of code that is executed when the method runs

6 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The Structure of a static Method’s Header public is a visibility modifier which allows the method to be used anywhere static means the method is called with the name of the class in front of it returnType specifies the type of data that will be returned parameterList is a comma-separated list of type name pairs public static returnType methodName( parameterList)

7 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Signature of main method public static void main( String[] args) –public is visibility modifier –static means class method –void means the method returns nothing –main is the name of the method –There is one parameter called args whose type is String[]

8 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Signature of pow method in Math class public static double pow( double a, double b) –public is visibility modifier –static means class method –double means the method returns a double value –pow is the name of the method –There are two parameter called a and b whose type is double

9 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Signature of println method in PrintStream class public void print(String s) –public is visibility modifier –void means the method returns a String –print is the name of the method –There is one parameter called s whose type is String

10 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Signature of format method in DecimalFormat class public String format(double number) –public is visibility modifier –String means the method returns a String –format is the name of the method –There is one parameter called number whose type is double

11 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The Structure of a Method Body Variables declared in the method body are local to the method Parameters are variables that can be used in the body of the method return statement is used to return a value from a method

12 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Defining findMaximum public static void findMaximum( double x, double y) { double max; if (x > y) max = x; else max = y; System.out.println( "The maximum is " + max); }

13 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Methods that return a value Think about how you might want to use a method like findMaximum –It really ought to return the maximum value so that it can be used to do something We define a method that returns a value by –using a return type other than void –putting a return statement at the end of the method body

14 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Defining findMaximum public static double findMaximum( double x, double y) { double max; if (x > y) max = x; else max = y; return max; }

15 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A Method can only return one value Method can return a primitive value an object reference A method whose return type is a class returns the location of an object Objects can contain multiple pieces of data

16 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Passing a Reference Value

17 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Exercise 9.a from Section 6.2 Write a method named distance that accepts the coordinates of two points, (x 1, y 1 ) and (x 2, y 2 ), and calculates the distance between them.

18 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Exercise 11 from Section 6.2 Write a method to calculate the monthly payment for a loan


Download ppt "©2004 Brooks/Cole Chapter 6 Methods. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Methods We've already seen examples of using methods."

Similar presentations


Ads by Google