Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
The Math Class Some classes allow you to use their methods without creating an object of the class first (with a constructor). The Math class is an example of this. We often use methods of the Math class just by invoking them in our code. First, add the following import statement to the top of your code: import java.lang.Math; The methods of the Math class that are included on the AP* exam: Math.abs(int num) Math.abs(double num) Returns the absolute value Math.pow(double num, double power) Returns the value num raised to the double num = 3; specified power double power = 2; double num2 = Math.pow(num, power); Math.random() Returns a random number between 0.0 (inclusive) and 1.0 (exclusive) Math.sqrt(double num) double num = 2; num2 = Math.sqrt(num); //returns 4 Returns the square root of num, which must be positive The Random Class The Random class uses a pseudorandom number generator. First, add the following import declaration: import java.util.Random; Then create an object of the class. Random generator = new Random(); Use the nextInt() and nextDouble() methods to produce random numbers. num1 = generator.nextInt(10); //from 0 to 9 num1 = generator.nextInt(10) + 1; //from 1 to 10