MATH AND RANDOM CLASSES.  The need for random numbers occurs frequently when writing software.  The Random class, which is part of the java.util class,

Slides:



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

© 2007 Lawrenceville Press Slide 1 Chapter 5 The if Statement  Conditional control structure, also called a decision structure  Executes a set of statements.
1 MATH METHODS THAT RETURN VALUES. 2 JAVA'S MATH CLASS.
Return values.
Arithmetic in Pascal (2) Arithmetic Functions Perform arithmetic calculations Gives an argument to the function and it returns the result.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
Function (L16) * Mathematical Library Functions * Program Components in C++ * Motivations for Functionalizing a Program * Function Prototype * Function.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
©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.
 2003 Prentice Hall, Inc. All rights reserved Introduction Modules –Small pieces of a problem e.g., divide and conquer –Facilitate design, implementation,
10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
* * 0 Chapter 6 Java Methods. * * 0 Method Syntax [access specifier][qualifier] return type method name(argument list) Access specifier public – method.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
1 The String Class Every character string is an object in Java, defined by the String class Every string literal, delimited by double quotation marks,
Chapter 5: Methods 1. Objectives To declare methods, invoke methods, and pass arguments to a method (§ ). To use method overloading and know ambiguous.
Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Part II © Copyright by Pearson Education, Inc. All Rights Reserved.
Math With Java The Math Class. First, A Quick Review of Math Operators in Java Primitive Data type in Java that represent numbers: Primitive Data type.
4-Methods Dr. John P. Abraham Professor UTPA. Common ways of packaging code Properties Methods Classes Namespaces (related classes are grouped into a.
CMSC 1041 Functions II Functions that return a value.
Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.
Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static.
Introduction to Computer Programming Math Random Dice.
Lecture 5 Methods. Sometimes we want to perform the same sequence of operations multiple times in a program. While loops allow us to do this, they are.
Using Java Class Library
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
APS105 Calculating 1. Basic Math Operations 2 Basic Arithmetic Operators Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / Recall.
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
The Math Class Methods Utilizing the Important Math Operations of Java!
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 Java Library Lecture 9 by Dr. Norazah Yusof. 2 Java Library Java has pre-defined classes that consist of the basic language classes in Java (organized.
Chapter 6: Repetition Continued. 2 Validity Checks What’s weak about the following code ? do { s1 = JOptionPane.showInputDialog (“Enter a number: ”);
7/31: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
TOPIC 6 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Math Class Mrs. C. Furman September 2, Math frequently used methods NameUse floor()rounds down ceil()rounds up pow(x,y)returns x to the power of.
Review for Nested loops & Math class methods & User defined methods.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Random Numbers. Are a series of numbers that have no pattern to them Ex) 7, 31, 4, 9, 8, 99… Random Numbers are used in… - Computer Games - Lotteries(6-49)
Let’s not leave anything to chance. How that process to generate random numbers takes places requires some complicated statistics that are outside the.
Creating and Using Class Methods. Definition Class Object.
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to.
Special Methods in Java. Mathematical methods The Math library is extensive, has many methods that you can call to aid you in your programming. Math.pow(double.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Conditional Control Structures Chapter 5 Review. The If Statement The if statement is a conditional control structure, also called a decision structure,
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Chapter 5 The if Statement
Formatting Output & Enumerated Types & Wrapper Classes
TMF1414 Introduction to Programming
Iterative Constructs Review
The Math class The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square.
Chapter 3, cont Sept 20, 2004.
Functions October 23, 2017.
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Random number generators
Chapter 6: User-Defined Functions I
The Random Class The Random class is part of the java.util package
Assignment Operators Topics Increment and Decrement Operators
Presentation transcript:

MATH AND RANDOM CLASSES

 The need for random numbers occurs frequently when writing software.  The Random class, which is part of the java.util class, represents a pseudorandom number generator. A random number generator picks a number at random out of a range of values.

 A pseudorandom number generator performs a series of complicated calculations, based on an initial seed value, and produces a number.  Though they are technically not random (because they are calculated), the values produced appear random.

 The statement double r = Math.random();  Produces a random real number in the range of 0.0 to 1.0, where 0.0 is included and 1.0 is not.  On the AP exam you will be expected to write algebraic expressions involving Math.random() that represent linear transformations of the original interval 0.0<= x <1.0.

 Examples: Produce a random real value x in the range 0.0 <= x <6.0. double x = 6 * Math.random(); Produce a random real value x in the range 2.0 <= x < 3.0. double x = Math.random() + 2; Produce a random real value x in the range 4.0 <= x < double x = 2 * Math.random() + 4; In general, to produce a random real value in the range lowValue <= x < highValue. double x = (highValue – lowValue) * Math.random() + lowValue;

 Using a cast to int, a scaling factor, and a shifting value, Math.random() can be used to produce random integers in any range.  Example: Produce a random integer, from 0 to 99. int number = (int) (Math.random() * 100; In general, the expression (int) (Math.random() * k) Produces a random int in the range 0,1…, k-1, where k is called the scaling factor. Note that the cast to int truncates the real number Math.random() * k.

 Example 2 Produce a random integer, from 1 to 100. int num = (int) (Math.random() * 100 ) + 1; In general, if k is a scaling factor, and p is a shifting value, the statement int n = (int) (Math.random() * k) + p; Produces a random integer n in the range p, p+1, …, p + (k-1). Produce a random integer from 5 to 24. int num = (int)(Math.random() * 20) + 5; There are 20 possible integers from 5 to 24, inclusive.

MATH CLASSES  The Math class provides alarge number of basic mathematical functions that are often helpful in making calculations.  The Math class is defined in the java.lang package  All the methods in the Math class are static methods, which means they can be invoked through the name of the class in which they are defined.

 The methods of the Math class return values, which can be used in expressions as needed.  The following statement computes the absolute value of the number stored in total, adds it to the value of count raised to the fourth poser and stores the result in the variable value.  Value = Math.abs(total) + Math.pow(count,4);

SOME METHODS OF THE MATH CLASS.  static int abs (int num) – Returns the absolute value of num.  static double exp(double power) – returns the value e raised to the specific power.  static double pow(double num, double pow) – returns the value num raised to the specified power.  static double random() – returns a random number between 0.0 (inclusive) and 1.0(exclusive)  static double sqrt (double num) – returns the square root of the num, which must be postiive