Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.

Slides:



Advertisements
Similar presentations
Methods Java 5.1 A quick overview of methods
Advertisements

CS0007: Introduction to Computer Programming
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
1 Chapter Eight Designing Libraries. 2 Programming Complexity As long as we continue to solve problems of ever-increasing sophistication, the process.
Building Java Programs
Random variables 1. Note  there is no chapter in the textbook that corresponds to this topic 2.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
Chapter 4: Writing Classes
Slide 5-2 Copyright © 2008 Pearson Education, Inc. Chapter 5 Probability and Random Variables.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number.
Building Java Programs Chapter 5
A Java API Package java.security  The Java Security Package contains classes and interfaces that are required by many Java programs.  This package is.
PROBABILITY  A fair six-sided die is rolled. What is the probability that the result is even?
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
©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.
1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1, 5.6.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
Chapter 6 Objects and Classes. Using the RandomGenerator Class Before you start to write classes of your own, it helps to look more closely at how to.
Sets, Combinatorics, Probability, and Number Theory Mathematical Structures for Computer Science Chapter 3 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProbability.
Sets, Combinatorics, Probability, and Number Theory Mathematical Structures for Computer Science Chapter 3 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProbability.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Chapter 4: Loops and Files
Part II © Copyright by Pearson Education, Inc. All Rights Reserved.
Lecture Set 9 Arrays, Collections and Repetition Part C - Random Numbers Rectangular and Jagged arrays.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Random numbers. 2 The Random class A Random object generates pseudo-random numbers. –Class Random is found in the java.util package. import java.util.*;
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Using Java Class Library
Essential Statistics Chapter 91 Introducing Probability.
Chapter 6 Methods: A Deeper Look. Objectives In this chapter you will learn: How static methods and fields are associated with an entire class rather.
CompSci Classwork today  Back to bouncing balls  Create a new BouncingSmiley using inheritence  Create an Arraylist of bouncing balls and smileys.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
Chapter 6 Methods Chapter 6 - Methods.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.
Let’s not leave anything to chance. How that process to generate random numbers takes places requires some complicated statistics that are outside the.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Objects and Classes Eric Roberts CS 106A January 22, 2016.
Part III © Copyright by Pearson Education, Inc. All Rights Reserved.
Random Numbers Eric Roberts CS 106A January 22, 2010.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
Five-Minute Review 1.What is a method? A static method? 2.What is the motivation for having methods? 3.What role do methods serve in expressions? 4.What.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
1 Building Java Programs Chapter 5 Lecture 11: Random Numbers reading: 5.1, 5.6.
Introduction to programming in java Lecture 16 Random.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Building Java Programs
Building Java Programs
Building Java Programs Chapter 5
Five-Minute Review What is a method? A static method?
Methods Chapter 6.
Road Map Introduction to object oriented programming. Classes
Chapter 4: Writing Classes
Five-Minute Review What is a method? A static method?
The Random Class and its Methods
Random numbers What does it mean for a number to be random?
Chapter 4 Writing Classes.
Building Java Programs
Building Java Programs
Building Java Programs
Random numbers What does it mean for a number to be random?
Sets, Combinatorics, Probability, and Number Theory
Presentation transcript:

Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java 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

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. Nondeterministic 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. int nextInt(int n) Returns a random int between 0 and n - 1. double nextDouble(double low, double high) Returns a random double d in the range low ≤ d < high. double nextDouble() Returns a random double d in the range 0 ≤ d < 1. boolean nextBoolean() Returns a random boolean value, which is true 50 percent of the time. boolean nextBoolean(double p) Returns a random boolean, which is true with probability p, where 0 ≤ p ≤ 1. Color nextColor() Returns a random color.

Using the Random Methods To use the methods from the previous slide in a program, all you need to do is call that method using rgen as the receiver. As an example, you could simulate rolling a die by calling int die = rgen.nextInt(1, 6); Note that the nextInt, nextDouble, and nextBoolean methods all exist in more than one form. Java can tell which version of the method you want by checking the number and types of the arguments. Methods that have the same name but differ in their argument structure are said to be overloaded.

Exercises: Generating Random Values How would you go about solving each of the following problems? 1. Set the variable total to the sum of two six-sided dice. int total = rgen.nextInt(2, 12); This declaration makes 2 come up as often as 7. int d1 = rgen.nextInt(1, 6); int d2 = rgen.nextInt(1, 6); int total = d1 + d2; 2. Change the fill color of rect to some randomly chosen color. rect.setFillColor(rgen.nextColor());