1 Introduction to Computers and Programming in JAVA

  • Slides: 20
Download presentation
1 Introduction to Computers and Programming in JAVA: V 22. 0002 Math class methods

1 Introduction to Computers and Programming in JAVA: V 22. 0002 Math class methods & User defined methods

Math class methods Math. sqrt(4. 0) Math. random() • java. lang is the library/package

Math class methods Math. sqrt(4. 0) Math. random() • java. lang is the library/package that provides Math class methods such as – Math. random() to generate random numbers • Java. lang is accessed by all java programs by default. – YOU do not have to include it at the beginning of the program as you do with javax package.

3 Random-Number Generation • Often we want our programs to generate random numbers. –

3 Random-Number Generation • Often we want our programs to generate random numbers. – games of chance – testing without user interaction • Java random-number generators – Math. random() • Returns a double value with a positive sign, greater than or equal to 0. 0 and less than 1. 0. – What if we want to generate random integers? 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this class.

4 Random-Number Generation – Math. random() • Produces double • from 0. 0 to

4 Random-Number Generation – Math. random() • Produces double • from 0. 0 to 1. 0 (excluding 1) • ( int ) ( Math. random() * 6 ) • Produces integers from 0– 5 Scaling – 1 + ( int ) ( Math. random() * 6 ) • Produces integers from Shifting 1– 6 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this class.

5 Benefits of Methods n n Experience has shown that the best way to

5 Benefits of Methods n n Experience has shown that the best way to develop and maintain large programs is to build it from smaller components or modules. In Java, one such module is the method. Each module is generally simpler and more manageable than the entire program. This concept is known as Divide and Conquer, also with Abstraction in the mix.

6 User defined Method returning a value • General format of method declaration: Method

6 User defined Method returning a value • General format of method declaration: Method header modifiers return-value-type method-name( parameter 1, …, parameter. N ) { Method body Scope of local variables declarations and statements } • Method can return one value: return expression; • Or, it can returns nothing using keyword void in the header. 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this class.

7 Return Value Types • You can only return one value from a method.

7 Return Value Types • You can only return one value from a method. • Returning void – void: means nothing – A method that returns void therefore returns nothing. – Hence, there is no need for the optional return statement. But using one can force early exit from the method. – Example: public static void print. Intro (int n);

8 Parameter Data Types • Unlike return values, you can pass as many parameters

8 Parameter Data Types • Unlike return values, you can pass as many parameters as you like. • To pass more than one parameter, you need to separate the parameters with commas. public static int maximum (int x, int y) { /*body*/ }

9 Warning • Unlike declaring variables, you must specifically state the type for multiple

9 Warning • Unlike declaring variables, you must specifically state the type for multiple variables – For example take. In. Two. Floats( float x, y ) is incorrect – Instead you must write take. In. Two. Floats(float x, float y)

10 No parameters • You can also have a method that accepts no parameters.

10 No parameters • You can also have a method that accepts no parameters. In such case, you would just have an empty parameter list. E. g. public static int roll. Die () public static void print. Intro ()

11 6. 5 Argument Promotion • Coercion of arguments – Forcing arguments to appropriate

11 6. 5 Argument Promotion • Coercion of arguments – Forcing arguments to appropriate type to pass to method • e. g. , System. out. println( Math. sqrt( 4 ) ); – Evaluates Math. sqrt( 4 ) – Then evaluates System. out. println() • Promotion rules – Specify how to convert types without data loss 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this class.

12 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this class.

12 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this class.

13 Understanding Scope

13 Understanding Scope

14 Local Variables & Scope A local variable: a variable defined inside a method.

14 Local Variables & Scope A local variable: a variable defined inside a method. Scope: the part of the program where the variable can be referenced. The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be declared before it can be used. 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this class.

15 Local Variables & Scope, continued You can declare a local variable with the

15 Local Variables & Scope, continued You can declare a local variable with the same name multiple times in different non-nesting blocks in a method, but you cannot declare a local variable twice in nested blocks. 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this class.

16 Overloading methods

16 Overloading methods

17 6. 15 Method Overloading • Method overloading – Several methods of the same

17 6. 15 Method Overloading • Method overloading – Several methods of the same name – Different parameter set for each method • Number of parameters • Parameter types – The Java compiler determines which method to use based on the parameters. – Can also be used in conjunction with argument coercion. • The combination can lead to ambiguous invocation which is an error 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this class.

18 Ambiguous Invocation Sometimes there may be two or more possible matches for an

18 Ambiguous Invocation Sometimes there may be two or more possible matches for an invocation of a method, but the compiler cannot determine the most specific match. This is referred to as ambiguous invocation. Ambiguous invocation is a compilation error. 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this class.

Ambiguous Invocation public class Ambiguous. Overloading { public static void main(String[] args) { System.

Ambiguous Invocation public class Ambiguous. Overloading { public static void main(String[] args) { System. out. println(max(1, 2)); } public static double max(int num 1, double num 2) { if (num 1 > num 2) return num 1; else return num 2; } public static double max(double num 1, int num 2) { if (num 1 > num 2) return num 1; else return num 2; } } 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this class. 19

20 review • What is a method? • What information can you learn about

20 review • What is a method? • What information can you learn about a method from its header? • What does it mean to invoke a method? • What is call by value? • What is scope of local variables within a method? • Why don’t we have to import the Math class? • What is abstraction in computer science?