4 min read•Last Updated on June 18, 2024
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
So far, we have learned of four classes that will help us with later programs: Scanner (in Unit 1), String, Double, and Integer. Let's introduce the last class for the time being: the Math class.
The Math class will help us perform many mathematical operations that couldn't have been done before. Here is the Java documentation for the Math class. (Courtesy of Java.)
As you can see, all the methods for Math are static because you can't make a Math object due to the fact that there is no constructor for it. There are a few methods that will be useful in this class:
To find the absolute value of a number, we find its magnitude. To do this, we use the following method:
For example, to calculate the absolute value of -5, we would write Math.abs(-5);.
Here are a few examples of when absolute value might be useful in writing algorithms:
Without the Math class, we could only do multiplication, but with the Math class, we can now do exponents and roots. For exponents, we have the following method:
For example, to calculate 2 to the power of 5 (which equals 32), we would write Math.pow(2,5);.
Here are a few examples of when exponents might be useful in writing algorithms:
For example, to calculate the square root of 25, we could write either Math.sqrt(25); or Math.pow(25, 0.5);.
Here are a few examples of when square roots might be useful in writing algorithms:
This is the most complex method of the Math class. A call of:
To implement random numbers greater than or equal to a number a and less than a number b, we make this call:
For example, if you want to generate a random integer from 5 to 555, we set a to 5 since our starting value is 5 and we set b to 556 since our ending value is the value 1 less than 556. Thus, we make this call: (int) (Math.random() * (556 - 5) + 5);.
Here are a few examples of when random numbers might be useful in writing algorithms:
In Java, Double is both a wrapper class for primitive type double and also represents real numbers with decimal points. It provides useful methods for performing mathematical operations on these numbers.
Term 1 of 7
In Java, Double is both a wrapper class for primitive type double and also represents real numbers with decimal points. It provides useful methods for performing mathematical operations on these numbers.
Term 1 of 7
In Java, Double is both a wrapper class for primitive type double and also represents real numbers with decimal points. It provides useful methods for performing mathematical operations on these numbers.
Term 1 of 7
A Scanner is a class in Java that allows the user to read input from various sources, such as the keyboard or a file.
InputMismatchException: This term refers to an exception that occurs when the data entered by the user does not match the expected data type.
nextInt(): This method is used with a Scanner object to read an integer value from the input source.
close(): This method is used with a Scanner object to release system resources associated with it.
A String is a sequence of characters in Java, enclosed in double quotes. It represents text and can be manipulated using various methods.
length(): This method returns the number of characters in a String.
substring(): This method extracts a portion of a String based on specified indices.
equals(): This method compares two Strings for equality and returns true if they are equal.
In Java, Double is both a wrapper class for primitive type double and also represents real numbers with decimal points. It provides useful methods for performing mathematical operations on these numbers.
intValue(): This method converts a Double value into an int value.
MAX_VALUE: This constant represents the maximum finite value that can be represented by Double.
isNaN(): This method checks if a Double value is "Not-a-Number" (NaN).
An integer is a data type in programming that represents whole numbers without any decimal or fractional parts.
Math class: The Math class in programming provides various methods to perform mathematical operations on integers and other numeric data types.
Static methods: Static methods are functions that belong to a class rather than an instance of the class. They can be used to perform operations on integers without creating an object of the Integer class.
Floating-point: Floating-point is another data type that represents numbers with decimal or fractional parts. It is different from integers because it allows for more precision but also takes up more memory space.
The Math.abs() function returns the absolute value of a number, which is the positive value of the number regardless of its original sign.
Math.pow(base, exponent): This term refers to the power function in math where you raise a base to an exponent. For example, Math.pow(2, 3) would return 8 because 2 raised to the power of 3 equals 8.
Math.max(num1, num2): This term refers to the function that returns the larger of two given numbers. It's like having two contestants and determining who is taller by using this function.
Math.min(num1, num2): This term refers to the function that returns the smaller of two given numbers. It's similar to having two contestants and determining who is shorter by using this function.
Exponents are a way to represent repeated multiplication of a number by itself. For example, 2^3 means multiplying 2 by itself three times (2 x 2 x 2 = 8).
Base: The base is the number that is being multiplied repeatedly in an exponent expression.
Power: The power is the number that tells you how many times the base should be multiplied by itself.
Exponential Growth: Exponential growth refers to a rapid increase in quantity over time, where the rate of growth is proportional to the current value.
Math.random() is a method in Java that generates a random decimal number between 0 (inclusive) and 1 (exclusive). It is commonly used to generate random numbers in programming.
Random Number Generator: A program or function that generates numbers with no predictable pattern.
Math.floor(): A method in Java that rounds down a decimal number to the nearest whole number.
Math.ceil(): A method in Java that rounds up a decimal number to the nearest whole number.