Using the Math Class


One of the most common operations in programming is adding or subtracting 1 from a variable. Adding 1
to a variable is called incrementing the variable. Subtracting 1 is called decrementing. In java, this type of calculation
can be done using ( ++ ) for increment and ( -- ) for decrement. Increment and decrement are used in loops most often.

Example)

a++;

We will add 1 to the variable a.

Example)

a--;

We will subtract 1 from the variable a.

 

Using Compound Assignment Operators

A compound assignment operator is an operator that performs a calculation and an assignment at the same time.
 

Operator Description
+= Addition and assignment
-= Subtraction and assignment
*= Multiplication and assignment
/= division and assignment
%= Remainder and assignment

Examples)

    1)    a += 10;    is equivalent to a = a + 10;

    2)    a *= 2;     is equivalent to a = a * 2;

    3)    a %= 3;    is equivalent to a = a % 3;

 

Using the Math Class

Luckily, we do not have to import anything for the math class. The math class is contained in the java.lang package.
Therefore, it's automatically available for us to use it. We have two constant that is already defined in the math class
and ready for us to use it, they are as follow.
 

PI 3.14592653589793
E 2.718281828459045

In addition to that, there are methods such as sqrt() which helps us to do the square root, pow() which returns
a power, and so on. We will see some examples on how to use the methods later on.

 

Creating Random Numbers

To create a random number, we would need to import java.util.Random;

Here are the steps that we need to take to generate a random number.

    1)    import java.util.Random;

    2)    Random rnumber = new Random();

    3)    int rn = rnumber.nextInt();

So once we have the random class imported, we can access the random class by doing step ( 2 ). We can use any name we want.
If we don't like to use rnumber, we can switch to something else. In step ( 3 ), rn will hold a random number generate from 0 and 1.

 

Now lets see some programming examples

 

Example)

/* The program will demonstrate how to find the square root
and the power of a number.

*
*/
import java.util.Scanner;

public class SqrPow
{
    public static void main(String[] args)
    {
        int x, y;
        double x1, y1;
        double sum = 0.0;

        Scanner key = new Scanner(System.in);

        System.out.print("Enter the first value: ");
        x = key.nextInt();

        System.out.print("Enter the second value: ");
        y = key.nextInt();

        //We call the sqrt() method
        x1 = Math.sqrt(x);
        y1 = Math.sqrt(y);

        // We call the pow() method
        sum = Math.pow(x, y);

        System.out.print("The square-root of " + x + " is " + x1 + "\n");
        System.out.print("The squareroot of " + y + " is " + y1 + "\n");
        System.out.print(x + " to the " + y +" power is " + sum);
    }
}
 

ClickHere to download SqrPow.java

 

Example)

/* The program will demonstrate how to use the Random class
We will roll a dice from 1 to 6

*
*/

import java.util.Random; // Needed for the random class

public class Dice
{
    public static void main(String[] args)
    {
        Random rNumber = new Random();

        // Here we add 1 because nextInt(6) only gives number from 0 to 5
        int rn = rNumber.nextInt(6) + 1;

        System.out.println("You rolled a " + rn);

    }
}
 

ClickHere to download Dice.java

 

Example)

/* The program will demonstarte how to use increment & decrement
Also, it will do some addition, subtraction and remainder in a different way

*
*/

import java.util.Scanner;

public class In_de
{
    public static void main(String[] args)
    {
         int x, y;
        int sum = 0;
        int total = 0, mod = 0;

        Scanner key = new Scanner(System.in);

        System.out.print("Enter the first value: ");
        x = key.nextInt();

        System.out.print("Enter the second value: ");
        y = key.nextInt();

        x++; // Increaments x
        y--; // Decrements y

        sum += y; // Same as sum = sum + y
        total -= x;// Same as total = total - x
        mod %= x; // Same as mod = mod % x

        System.out.println("The increment of first value is " + x);
        System.out.println("The decrement of second value is " + y);
        System.out.println("The value assigned to sum is " + sum);
        System.out.println("The value assigned to total is " + total);
        System.out.println("The remainder assigned to mod is " + mod);
    }
}

ClickHere to download In_de.java

 

 

 <Java 24><Home>