Pages

Thursday, March 14, 2013

Program Larik di Java

Kesempatan kali ini saya ingin share program yang saya buat untuk tugas kuliah saya kemarin :D Mungkin bisa bermanfaat untuk blogger lainnya,yang sekiranya bisa dibuat refrensi.Maklum saya sendiri masih newbie di pemograman.Jadi saling berbagi aja.

Penampakan Program :


Source Code :


public class LarikGUI extends javax.swing.JFrame {

    private int size;
    private int []Data,Dt1;
    private String[] arStr;
   
    public int [] MasukkanLarik(int n){ // memasukkan data ke array
        Data = new int[n];
        Dt1 = new int[n];
        String temp = "";
        size = n;
        for (int a = 0; a < size; a++) {
            try {
                Data[a] =  Integer.parseInt(JOptionPane.showInputDialog
("Masukan data ke " + (a + 1)));
                Dt1[a] = Data[a];
            } catch (HeadlessException | NumberFormatException ex) {}
            temp += Integer.toString(Dt1[a]) + "  ";
        }
        Tdatainput.setText(temp);
        return Dt1;
    }

   public void isiItem(int x, int y) {
        this.Dt1[x] = this.Dt1[y];
    }

   public int findMax(){ //mencari data terbesar
        int Max = this.Dt1[0];
        for (int i=1; i<this.size;i++){
            if(Max<this.Dt1[i]){
                Max=this.Dt1[i];}
        }return Max;
    }

   public int findMin(){  //mencari data terbesar
        int Min = this.Dt1[0];
        for (int i=1; i<this.size;i++){
            if(Min>this.Dt1[i]){
                Min=this.Dt1[i];}
        }return Min;
    }
  
    private int getPosMax(int id){ //Mencari Posisi Data terbesar
        int max =this.Dt1[id];
        int posMax = id;
        for(int i=id+1;i<size;i++){
            if(max<=this.Dt1[i]){
                max= this.Dt1[i];
                posMax=i;}
        }return posMax;
    }

    private int getPosMin(int id){ //Mencari Posisi Data terkecil
        int min =this.Dt1[id];
        int posMin = id;
        for(int i=id+1;i<size;i++){
            if(min>=this.Dt1[i]){
                min= this.Dt1[i];
                posMin=i;}
        }return posMin;
    }
  
    private String findPosGenap(int id){
        int i = id;
        int j = 0;
        int posGenap [] = new int [this.size];
        String temp = "";
        while (i<this.size){
            if ((this.Dt1[i]%2) == 0){
                posGenap[j] = i;
                temp += Integer.toString(posGenap[j])+" ";
                j++; }
           i++;
        }return temp;
    }
    
     public int getPosisi(int dtCari) {
        int pos = -99;
        boolean ketemu = false;
        int i = 0;
        while (!ketemu && i < this.size) {
            if (dtCari == this.Dt1[i]) {
                ketemu = true;
                pos = i; }
            i++;
        }return pos;
    }
   
     public int PencarianBiner(int dtCari, int awal, int akhir) {
        int pos = -99;
        int tengah = (awal + akhir) / 2;
        if (dtCari < this.Dt1[tengah]) {
            return PencarianBiner(dtCari, awal, tengah);
        } else if (dtCari > this.Dt1[tengah]) {
            return PencarianBiner(dtCari, tengah + 1, akhir);
        } else if (dtCari == this.Dt1[tengah]) {
            return tengah;
        } else {
            return pos;
        }
    }
   
      public String copyLarik(int k, int n) { // copy larik
        int[] lHasil;
        lHasil = new int[n];
        String temp = "";
        if (n <= this.size - k) {
            for (int i = k; i < k + n; i++) {
                lHasil[i - k] = Dt1[i];
                temp += Integer.toString(lHasil[i - k]) + " ";}
        } else {
            try {
                JOptionPane.showMessageDialog(rootPane, "Maaf jumlah array yang ingin anda copy melampaui jumlah data yang ada !!");
            } catch (Exception ex) {}
        }
        return temp;
    }

      public String bubleSort(int array[]){ // sorting bubble sort
        String temp = "";
        for (int x = 1; x < array.length; x++) {
            for (int y = 0; y < array.length - x; y++) {
                if (array[y] > (array[y + 1])) {
                    int temp1 = array[y];
                    array[y] = array[y + 1];
                    array[y + 1] = temp1;}
            }}
        for (int i = 0; i < array.length; i++) {
            temp += Integer.toString(array[i]) + "  ";}
        return temp;
    }
     
      public String SelectionSort(int array[]){ // sorting selection
        String temp = "";
        for (int i = 0; i < array.length; i++){
        int posData;
            posData = getPosMin(i);
            int dt1 = array[i];
            int dt2 = array[posData];
            array[i] = dt2;
            array[i] = dt2;
            array[posData] = dt1;
        }
        for (int i = 0; i < array.length; i++) {
            temp += Integer.toString(array[i]) + "  ";
        } return temp;
    }
     
      public String insertionSort(int array[]) { // sorting dinsertion
        int k, temp, j;
        String tempx = "";
        for (k = 2; k < array.length; k++) {
            temp = array[k];
            j = k - 1;
            while (temp <= array[j]) {
                array[j + 1] = array[j];
                j--;
            }
            if ((temp >= array[j]) || (j == 1)) {
                array[j + 1] = temp;
            } else {
                array[j + 1] = array[j];
                array[j] = temp;}
        }
        for (int i = 0; i < array.length; i++) {
            tempx += Integer.toString(array[i]) + "  ";
        }return tempx;
    }
     
    public void mergeSort_srt(int lo, int n) {
        int low = lo;
        int high = n;
        if (low >= high) {
            return;
        }
        int middle = (low + high) / 2;
        mergeSort_srt(low, middle);
        mergeSort_srt(middle + 1, high);
        int end_low = middle;
        int start_high = middle + 1;
        while ((lo <= end_low) && (start_high <= high)) {
            if (Integer.parseInt(arStr[low]) < Integer.parseInt(arStr[start_high])) {
                low++;
            } else {
                int Temp;
                Temp = Integer.parseInt(arStr[start_high]);

                for (int k = start_high - 1; k >= low; k--) {
                    arStr[k + 1] = arStr[k];
                }
                arStr[low] = String.valueOf(Temp);
                low++;
                end_low++;
                start_high++;
            }}}

    public String Mergesort(int array[]) {
        arStr = new String[size];
        String temp = "";
        for (int i = 0; i < size; i++) {
            arStr[i] = String.valueOf(array[i]); }
        mergeSort_srt(0, size - 1);
        for (int i = 0; i < size; i++) {
            temp += arStr[i] + "  ";}
    return temp;
    }
   
    public void Keluar() { // Method button keluar
        try {
           int reply = JOptionPane.showConfirmDialog(this, "Yakin Ingin Keluar? ", "Peringatan", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE);
            if (reply == JOptionPane.YES_OPTION) {
                setVisible(false);
                dispose();
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                System.exit(0);}
        } catch (Exception e) {}
    }
   
    public LarikGUI() {
        initComponents();
    }             

  private void BKeluarActionPerformed(java.awt.event.ActionEvent evt){                                       
         Keluar(); // Memanggil method keluar
    }                                       

private void BInputActionPerformed(java.awt.event.ActionEvent evt){                                      
        MasukkanLarik(Integer.parseInt(Tn.getText()));
      }                                     

private void BMaksMinActionPerformed(java.awt.event.ActionEvent evt) {                                        
        TMaks.setText(Integer.toString(findMax()));
        TMin.setText(Integer.toString(findMin()));
        Tpmaks.setText(Integer.toString(getPosMax(0)));
        Tpmin.setText(Integer.toString(getPosMin(0)));
    }                                       

 private void BcarigenapActionPerformed(java.awt.event.ActionEvent evt)  {                                          
        Tpgenap.setText((findPosGenap(0)));
    }                                          

 private void BcopylarikActionPerformed(java.awt.event.ActionEvent evt) {                                          
        int a = 0, n = 0;
        try {
            a = Integer.parseInt(JOptionPane.showInputDialog("Masukan awal posisi array yang ingin di copy "));
             n = Integer.parseInt(JOptionPane.showInputDialog("Masukan   jumlah data yang akan dicopy (terhitung dari posisi yang diinputkan sebelumnya)"));
        } catch (HeadlessException | NumberFormatException ex) { }
        Tcopylarik.setText((copyLarik(a, n)));
    }                                         

private void BSortActionPerformed(java.awt.event.ActionEvent evt) {                                     
        TBubble.setText(bubleSort(Dt1));
        TSelection.setText(SelectionSort(Dt1));
        Tinsertion.setText(insertionSort(Dt1));
        Tmerge.setText(Mergesort(Dt1));
    }                                    

private void BResetActionPerformed(java.awt.event.ActionEvent evt) {                                      
        Tn.setText(null);
        Tdatainput.setText(null);
        Tpmaks.setText(null);
        Tpmin.setText(null);
        TMaks.setText(null);
        TMin.setText(null);
        Tpgenap.setText(null);
        Tcopylarik.setText(null);
        TBubble.setText(null);
        TSelection.setText(null);
        Tinsertion.setText(null);
        Tpdata.setText(null);
        Tmerge.setText(null);
    }                                     

private void BcaridataActionPerformed(java.awt.event.ActionEvent evt){                                         
        int a = 0, b;
        try {
            a = Integer.parseInt(JOptionPane.showInputDialog("Masukan data yang akan di cari :"));
        } catch (HeadlessException | NumberFormatException ex) {
        }
        b = getPosisi(a);
        if (b == -99) {
            try { JOptionPane.showMessageDialog(rootPane,
                  "Maaf data tidak ditemukan !");}
            catch (Exception ex) {
            }
        } else {
        Tpdata.setText("Data ada di array ke : " +   Integer.toString(b));
        } } }

0 comments:

Post a Comment