Contoh Penggunaan java.lang.Math

November 3, 2009

Class Math pada java.lang.Math secara implisit sudah ter-include dalam setiap deklarasi kelas. Jadi kita tidak perlu menambahkan: import java.lang.Math.*; lagi.

Berikut contoh penggunaan class Math tersebut.

/**
 * @File: MathTes.java
 * @url: https://thepetarung.wordpress.com
 * @date: 3 nop 2009
 */

public class MathTes{
    public static void main(String[] args){
        
        int a = 2;
        int b = 4;
        int c = 7;
        double d = 9.4;
        double e = 9.5;
        
        double[] baris = new double[5];
        
        baris[0] = Math.sqrt(b);
        baris[1] = Math.pow(a, 3);
        baris[2] = Math.sin(c);
        baris[3] = Math.round(d);
        baris[4] = Math.round(e);

        for(double r : baris)
            System.out.println(r);
             
    }
}

Setelah dikompilasi dan dijalankan, hasilnya adalah sebagai berikut:

2.0
8.0
0.6569865987187891
9.0
10.0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: