Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Math class The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square.

Similar presentations


Presentation on theme: "The Math class The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square."— Presentation transcript:

1 The Math class The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.

2 The Math class is a part of java.lang.*
The Math class was created to help programmers solve problems that require mathematical computations The Math class is a part of java.lang.* The Math class does need an object The methods and fields of the Math class are accessed by using the class name followed by the method name: Math.methodName(); The Math class was created to help programmers solve problems that require mathematical computations and is a part of the java.lang packages (so it doesn’t need to be imported). Most of these methods can be found on a calculator that you would use in a math class. Compared to the other classes in the AP subset, the Math class is special in that it contains only static methods. This means that you don’t need to create an object from the class in order to use the methods in the class. All you have to do is use the class name followed by the dot operator and then the method name.

3 Absolute value Demonstrates how to use the absolute value method on an integer: int result = Math.abs(-8); // result is 8 double result = Math.abs(-8.4); //result is 8.4 The Math.abs method returns the absolute value of an int value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned. 

4 Square root Demonstrates how to use the square root method.
The sqrt method will compute the square root of any number that is greater or greater than or equal to 0. The result is always a double and must be a positive number double result = Math.sqrt(49); //result is 7.0

5 Base Raised to a Power Demonstrates how to use the power method.
The pow method always returns a double. double result = Math.pow(5,3); //result is (5 raised to the 3rd power) The Math.pow (double a, double b) returns the value of the first argument raised to the power of the second argument. 

6 Random Number Generator
Demonstrate how to use the random method. double result = Math.random(); //result is a double in the interval[0.0, 1.0] The random number generator from the Math class returns a double value in the range from 0.0 (inclusive) to 1.0 (exclusive), including 0.0 but not including 1.0.

7 Generate a random integer
Generate a random integer between 0 and 12 (inclusive) int result = (int) (Math.random() * (total # of choices)); int result = (int)(Math.random() * 13); //13 possible answers 0-12 int result = (int)(Math.random() * 17) + 4; //17 possible answers 4-20 The Math.random() method returns a double that is randomly chosen from the interval 0.0 to By casting the result of the Math.random () statement, you can generate a random integer.

8 Random class in the util package
import java.util.Random; //imports Random class from util package Random generator = new Random(); //creates a copy of the object int num1 = generator.nextInt(10); //generates a number from 0-9 and assigns it to num1 int num2 = generator.nextInt(10)+1; //generates a number from 1-10 and assigns it to num2 In addition to the random method of the Math class, there is the random class in the util package. You will often need random numbers when you are writing software. Games often use a random number to represent the roll of a die or the shuffle of a deck of cards. A flight simulator may use random numbers to decide how often a simulated flight has engine trouble. A program designed to help high school students prepare for SAT may use random numbers to choose the next question to ask. The Random class is a part of the util package and must be imported to be used. The Random class uses a pseudorandom number generator. A random number generator picks a number at random out of a range of values. A program that does this is called pseudorandom, because a program can’t really pick a number randomly. A pseudorandom number generator might be a series of complicated calculations, starting with an initial seed value and produce a number. Through they are technically not random, the numbers produced by a pseudorandom number generator usually seem to be random, at least random enough for most situations.

9 PI Demonstrate how to access the built-in value of PI from Math class.
double result = Math.PI; // PI is not a method, it is a public field PI is not a method, but rather a public field. It can be accessed through the built-in value of PI from the Math class. PI will always have the value of


Download ppt "The Math class The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square."

Similar presentations


Ads by Google