Objectives

Students will be able to:

  • understand that a program can take a different path depending on a condition
  • understand that an IF statement doesn't always have to have a ELSE
  • understand the concept of ELSE IF

The Math Class

The Math class is a library file full of useful code that you can use in your programs.

There are many functions.

Because these functions reside in a class file, they are usually called methods.

Any library file is useful, because it speeds up the development of your own programs.

Also, the code has already been tested, so it should be reliable and reduce the possibility of errors in your own code.

Here are some useful Math class methods. To call them, we need to let Java know that they exist in the Math class, so we start with Math.

int rand = (int)(Math.random()*10); //we are calling the random method of the Math class! How useful!

int result = Math.pow(2,3); //returns 2 to the power of 3

double result = Math.sqrt(7); //returns the square root of 7

int maxNum = Math.max(x,y); //send 2 variables into the max() function and get the result


That is the end of this unit! There are no exercises. But keep in mind the Math class when you are coding and do some research to see other methods that are available in this useful class.

Tags

classsyntax method max call sqrt pow randomreturn