Presentation is loading. Please wait.

Presentation is loading. Please wait.

©2004 Brooks/Cole Chapter 6 Methods and Scope. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A Method Can Be Viewed as a Black Box To use a.

Similar presentations


Presentation on theme: "©2004 Brooks/Cole Chapter 6 Methods and Scope. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A Method Can Be Viewed as a Black Box To use a."— Presentation transcript:

1 ©2004 Brooks/Cole Chapter 6 Methods and Scope

2 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A Method Can Be Viewed as a Black Box To use a method, we don't need to know what is inside The method signature tells us how to use it

3 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Variable Scope We declare variables in a number of places in our programs. Scope rules control where we can use the variables we declare. In Java, variables have block scope.

4 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Scope Rules Variables declared inside a method are local to the method –Variables have to be declared before they can be used –Method parameters are local to the method Variables declared in a block can be used only inside that block Class variables are visible everywhere in the class

5 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The Three Storage Areas Created by Program 6.6

6 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Shadowed variables What happens if two variables with the same name are visible in a particular part of the program? –Local variable is the one that is visible Class variables can be accessed even if they are shadowed. ClassName.variableName

7 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Pass by Value Arguments to methods are passed by value in Java –method stores a copy of the value that is passed in memory –Changes to a parameter are changes to the local memory

8 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Data is passed by value Actual Values of variables passed to method are stored in parameter memory

9 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Random Numbers Many kinds of programs need random numbers –Games –Simulations On a computer, we generate pseudorandom numbers –not truly random –behave as if they were random

10 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Random Number Generation The method Math.random is a pseudorandom number generator. It returns a number of type double that is greater than or equal to 0.0 but less than 1.0.

11 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Random Integers The number returned from the random method ranges from 0.0 up to (but not including) 1.0. If we want to generate random integers, we must perform a conversion so the number will fall within the desired range. –to generate a number between 0 and N-1, use the formula: y = (int)(Math.random() * N) –to generate a number between 1 and N, use the formula: y = (int)(Math.random() * N + 1)

12 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Random Number Example *Ch6TestRandomGenerator generates N random numbers between 1 and 4 to simulate the suit of a drawn card. It keeps one counter for each suit, and increments the matching counter after a random number is generated. At the end of the generation, it prints the ratio count/N.

13 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Random Class The Random class is part of the java.util package It provides methods that generate pseudorandom numbers of various types –float nextFloat() –int nextInt() –int nextInt(n) produces integer in range 0 to n-1

14 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Testing Programs Isolation testing Method stubs

15 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Point of Information: Isolation Testing


Download ppt "©2004 Brooks/Cole Chapter 6 Methods and Scope. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A Method Can Be Viewed as a Black Box To use a."

Similar presentations


Ads by Google