Welcome to Computer Programming

 

Math class - api

 

The math class allows us to do math calculations, such as square or higher powers of numbers, find the square root, find trig functions (sine,cosine...), find the exact value of pi.

We do not need to import anything.

 

To find what 15 squared is we would say:

Math.pow(15,2); // this would give us 15 to the power of 2

Math.pow(3,4); //this would give us 3 to the power of 4

To find the square root of a number we would say:

Math.sqrt(16); //this would give us the sqaure root of 16

 

To get the value of pi (not exact but a lot of decimal places)

Math.PI; //this would give us pi - note its not a method so no (), it is a field.

 

To find a random number between 0 and .99999999999 use

Math.random();

 

Activity:

  1. Create a method public void insultUser() that will randomly spit out 1 of 3 insults. What Math method will you use.
  2. Create public void roundHundredths(double x) that will take in x and round it to the nearest hundredth. TRICKY.