OOP Bubble JAVA

OOP BUBBLE JAVA

Pada tutor kali ini saya akan membahas tentang bagaimana cara membuat bubble sort menggunakan oop pada java, let’s check this
Source Code :
import java.util.Scanner;
class oop_bubble
{
                private long [] data = new long [6]; // private = hanya diguanakan pada class, long = type data(int)
                private int n;
               
               
                public oop_bubble (int max)// method harus sama dengan class
                {
                                max = 6;
                                data = new long[max];
                                n = 6;
                }

                public void tambah(long nilai) // ini method
                {
                                data[n] = nilai;
                                n++;
                }
               
                public void input()
                {             
                                Scanner br = new Scanner(System.in);
                                int i;
                                for(i = 0; i<data.length; i++)
                                {
                                                System.out.print("Masukkan nilai ke - " +i +" = ");
                                                data[i] = br.nextInt();
                                }
                }
               
                public void cetak() //ini juga method (cetak)
                {
                                for (int j=0; j<n; j++)
                                System.out.print(data[j] +" ");
                                System.out.print(" ");
                }
               
                public void bubbleSort()//ini sama juga method (algorimt)
                {
                                int i, j;
                                                for(i=data.length-1; i>0; i--)
                                {
                                                for (j=0; j<i; j++)
                                                {
                                                                if (data[j] > data[j+1])
                                                                tukar(j, j+1);
                                                }
                                                System.out.print("Hasil langkah ke " +(-(i-n)) +"  : ");
                                                cetak();
                                                System.out.println("");
                                }
                }
               
                private void tukar(int a, int b) //ini juga method (tukar)
                {
                                long temp = data[a];
                                data[a] = data[b];
                                data[b] = temp;
                }
}

class oop_bubble_index
{
   
                    public static void main (String []args)
               
    {
                int ukuran = 100;
                oop_bubble DeretAngka; //oop_bubble = class
                DeretAngka = new oop_bubble (ukuran);
               
                                DeretAngka.input();
                               
                System.out.print ("Larik awal                                      : ");
                DeretAngka.cetak();
                System.out.println();
               
                DeretAngka.bubbleSort();
                System.out.print("Larik akhir                                      : ");
                DeretAngka.cetak();
                System.out.println();
    } 
}


Sekian tutor dari saya, kurang lebih nya saya mita maaf, sampai jumpa pada tutor beriktnya di minggu depan Terima Kasih :D


Newest
Previous
Next Post »

1 comments:

Click here for comments
LibCare
admin
15 April 2019 at 06:30 ×

Thanks

Congrats bro LibCare you got PERTAMAX...! hehehehe...
Reply
avatar