CH06-ObjectsAndClasses

CH06-ObjectsAndClasses - The Art and Science of An...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Jav a Objects and Classes C H A P T E R 6 To beautify life is to give it an object. Jos Mart, On Oscar Wilde, 1888 6.1 Using the RandomGenerator class 6.2 The javadoc documentation system 6.3 Defining your own classes 6.4 Representing student information 6.5 Rational numbers 6.6 Extending existing classes Using the RandomGenerator Class Before you start to write classes of your own, it helps to look more closely at how to use classes that have been developed by others. Chapter 6 illustrates the use of existing classes by introducing a class called RandomGenerator , which makes it possible to write programs that simulate random processes such as flipping a coin or rolling a die. Programs that involve random processes of this sort are said to be nondeterministic . Nondeterminstic behavior is essential to many applications. Computer games would cease to be fun if they behaved in exactly the same way each time. Nondeterminism also has important practical uses in simulations, in computer security, and in algorithmic research. Creating a Random Generator The first step in writing a program that uses randomness is to create an instance of the RandomGenerator class. In most cases, you create a new instance of a class by using the new operator, as you have already seen in the earlier chapters. From that experience, you would expect to create a RandomGenerator object by writing a declaration like this: RandomGenerator rgen = new RandomGenerator(); For reasons that will be discussed in a later slide, using new is not appropriate for RandomGenerator because there should be only one random generator in an application. What you want to do instead is to ask the RandomGenerator class for a common instance that can be shared throughout all classes in your program. private RandomGenerator rgen = RandomGenerator.getInstance(); The best way to create a RandomGenerator instance is to call the getInstance method, which returns a single shared instance of a random generator. The standard form of that declaration looks like this: This declaration usually appears outside of any method and is therefore an example of an instance variable . The keyword private indicates that this variable can be used from any method within this class but is not accessible to other classes. When you want to obtain a random value, you send a message to the generator in rgen , which then responds with the result. Methods to Generate Random Values The RandomGenerator class defines the following methods: int nextInt(int low, int high) Returns a random int between low and high , inclusive....
View Full Document

This note was uploaded on 02/18/2010 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.

Page1 / 48

CH06-ObjectsAndClasses - The Art and Science of An...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online