UOB - Using the Math Class (Lesson)

APCompSci_LessonTopBanner.png

Using the Math Class

Introduction

Like the String class, the Math class is another useful class which can aid in calculations in a program. The Math class demonstrates the ability to use methods without having to create an object. These methods are called static methods. Static methods are class methods that can be called directly on the class using the class name, the dot operator, and the method name.

To keep things uniform, programmers may write methods that are meant to be used on objects. It is helpful to be able to use code written by others but if you have primitive data and want to use methods that are written for objects you have a type mismatch. This is resolved with Wrapper classes. Wrapper classes are used to convert primitive types to objects. Java will automatically box and unbox primitive to objects and back again.

Using the Math Class

The Math class is another frequently used class that contains several methods. The Math class is part of the java.lang package.

All the methods in the Math class are static methods. Static means the methods do not require the creation of an object to invoke (call/use) them. They are invoked through the class name. Static methods are used when we will have the same result regardless of the object. For example the abs() method will compute the absolute value of the number given every time. No individual object will change that computation.

Most Math methods will return a double value, however, there are some that will return integers.

The following static math methods are part of the Java Quick Reference and will be tested on the AP Exam:

  • int abs(int x) – Returns the absolute value of an int value
  • double abs(double x) – Returns the absolute value of a double value
  • double pow(double base, double exponent) – Returns the value of the first parameter raised to the power of the second parameter
  • double sqrt(double x) – Returns the positive square root of a double value
  • double random() – Returns a double value greater than or equal to 0.0 and less than 1.0

Random Numbers

Math.random returns a double between 0 and 1, not including 1...[0.0, 1.0). 

number line chart 

To get a number between 0 and 10 we can take Math.random and multiply it by 10. That will move the decimal point over and give us all doubles between 0.0 and 10.0, not including 10…[0.0, 10.0) 

number line chart 

Math.random()*10

Next, cast the numbers as ints. That will give us all ints 0 through 9.

(int)(Math.random()*10)

Finally, add 1 to our expression:

(int)(Math.random()*10) + 1;

There we have it! A formula that will return random ints from 1 to 10.

 

Wrapper Classes: Integer and Double

Programmers include variables in their code so that the same algorithm runs using different input values. This helps find specific solutions to general problems. Sometimes code will be written for objects, but we want to use it for primitive data. When that happens, we can use Wrapper classes. Wrapper classes take primitive data and wrap them up to look like objects. The two Wrapper classes that we will look at are Integer and Double.

The Integer class and Double class are part of the java.lang package. There are several Integer and Double constructors and methods that we will highlight. Each are listed in the Java Quick Reference.

Integer(int value) - Constructs a new Integer object that represents the specified int value.

Integer.MIN_VALUE - The minimum value represented by an int or Integer

Integer.MAX_VALUE - The maximum value represented by an int or Integer

int intValue() - Returns the value of this Integer as an int

Double(double value) - Constructs a new Double object that represents the specified double value

double doubleValue() - Returns the value of this Double as a double

We can use the Integer and Double constructors and methods to convert ints and doubles to objects and back to primitive data. However, Java will also do that for us.

Autoboxing is the automatic conversion that the Java compiler makes between primitive types and their corresponding object wrapper classes. This includes converting an int to an Integer and a double to a Double.

The Java compiler applies autoboxing when a primitive value is:

•Passed as a parameter to a method that expects an object of the corresponding wrapper class

•Assigned to a variable of the corresponding wrapper class.

Unboxing is the automatic conversion that the Java compiler makes from the wrapper class to the primitive type. This includes converting an Integer to an int and a Double to a double.

The Java compiler applies unboxing when a wrapper class object is:

•Passed as a parameter to a method that expects a value of the corresponding primitive type.

•Assigned to a variable of the corresponding primitive type.

 

Math Class

Practice Icon Practice-It! Self-Check

Experiment

Experiment with the different Math methods by typing the following program into Dr. Java. Pay close attention to what each method does and whether it returns an int or a double.

public class MathTester
{
public static void main (String [] args)
{
System.out.print1n(Math.abs(-3));
System.out.print1n(Math.ceil (4.201));
System.out.print1n(Math.floor(5.511));
System.out.print1n(Math.max(1, -4));
System.out.print1n(Math.abs(-2.0));
System.out.print1n(Math.pow(2,3));
System.out.print1n(Math.round(3.59));
System.out.print1n(Math.sqrt(529));
System.out.print1n(Math.abs(-3.0));
}
}
 

Practice Icon Using Objects Test Review

APCompSci_LessonBottomBanner.pngIMAGES CREATED BY GAVS