hi in this lesson we'll take a look at the math class let's dive in as we learned earlier in the unit instance methods need to be called with an object of the class whenever we've wanted to use one of the rectangle methods we've had to call the method on an existing rectangle so that we could use the data from the rectangle in the method being called but is it possible to create a method without creating an instance or object of the class in fact there is a way to create methods that don't require an instance of a class to work static methods are methods that can be called without creating an object a static method of a class is common to all instances of a class and is not called on an object instance static methods can be used anywhere in your programs to create a static method we simply need to include the keyword static in the method declaration this indicates that the method can be used by the class name itself and not necessarily through a class object let's say we wanted to create a method that reminded us of the different equations that we could use on rectangles we could create a static method that printed both the formula for area and perimeter now that the method wrecked equations exists we can call it using the name of the class it belongs to when rectangle. rect equations is called it executes the method even though there is no existing rectangle object one class that exclusively contains static methods is the math class the java. lang.
math class contains static methods for performing basic numeric operations such as the elementary exponential logarithm square root and trigonometric functions the math class is part of the java. lang package this diagram shows a small part of the java. lang package which we have covered in this unit the java.
lang package provides classes that are fundamental to the design of the java programming language the math class contains only static methods to call a static method in the math class use the dot operator with the class name followed by the method you want to use we don't need to make any instance of the math class in order to use any of its methods having to create a math object each time we wanted to utilize a math class would be a tedious process static methods allow us to simply use the class name to call methods in the math class that we want to use in fact if you try to create an instance of the math class and use it to call a static math class method the compiler will throw an error for static methods you can only use them without instances the next few slides will cover five common static math methods and will be very useful in your java programs they're also a part of the java quick reference which means they can be tested on the ap exam you can evaluate expressions that use the math class methods in your programs to help solve problems here are a couple of methods from the math class that will be helpful to us as we continue coding more complex problems let's take a look at the first method math. apps math. abs returns the absolute value of the numeric value that is input in the method there is a double and int version of the abs constructor so whichever type is input will be returned as an absolute value of the same type the next method is math.
pal math. pal allows us to create programs with exponential values pow takes the first input and raises it to the power of the second input the third method is math dot square root we can use math dot square root to get the positive square root of a double value finally let's take a look at math. random math.
random returns a random double value greater than or equal to zero and less than one when and why would you need to use random numbers in a java program as it turns out random numbers are used in lots of places in programming they can be used in games like rolling dice flipping coins shuffling cards and more they can also be used in simulations and for security systems like in creating secure keys in cryptography to make data secure to send and receive you can use random numbers anywhere you need some level of unpredictability while generating random numbers can be very useful we usually need to do something with them in our programs if we are rolling a dice selecting a card from a deck or we just need a whole number for our application we can do a little more to get whole numbers from our random numbers by defining a range and multiplying let's walk through how to do this the values returned from math. random can be manipulated to produce a random int or double in a defined range for example like the numbers 0 through 9. let's dive into this code on the next few slides every time math.
random is called a new decimal number between 0. 0 and 1. 0 is generated we then multiply that number by 10 so that there is a number in the ones place next the number that was generated is casted which drops the decimal values from the number and leaves us with the positive number between 0 and 9.
we can repeat the process as many times as we like and generate as many random numbers between 0 and 9 as we want while that only gives us numbers from 0 to 9 the general formula for getting a number from 0 to any range is int parentheses math. random times integer plus 1. let's take a look at how this expression works as we already noted math.
random creates a random integer from 0 to 0. 999 repeating when we multiply it by integer plus 1 it changes the range of the numbers produced to 0 to the integer 0. 999 repeating the reason that we add 1 to the range that we want is due to the fact that the integer value is not included in the range if we multiply by integer if we multiply any value from 0 to 0.
99 repeating by a number the range of possible outcomes will be 0 to the number minus 0. 0001 because casting to an int removes the decimal values from a given number multiplying by the number we want included in the range will never actually include the final number in the range if we add 1 to the range then the range when multiplied by number plus 1 will be 0 to the number 0. 9999 repeating now when we cast to an int that number will be inclusive in the range finally the int casts the number to a whole number that can be used for example if we wanted a random number between 0 and 5 we would plug 5 into the integer variable slot and write the expression as such when math.
random is called it creates a value between 0 to 0. 99 repeating that is then multiplied by 6 which creates a value between 0 and 5. 99 repeating that number is then cast to an int leaving a random number from 0 to 5.
if we wanted to change the start of the range to be 5 to 10 we can do so by adding 5 to the end of the math. random equation because the lowest value possible is 0 adding 5 to the end of the equation will make sure that the lowest possible value will be 5. now when int is called the possible range will be from 5 to 10.
here is the general formula for starting a range at a specific starting value and going through a specific range for example if we wanted the starting value to be 100 and the range to be from 100 to 150 the formula to create those random numbers would be int math. random times fifty plus one plus one hundred to learn more about math.