Jumat, 10 Mei 2013

Membuat Program Java For, While Dan Do - While Dengan NetBeans [JAVA]

Buatlan program dengan for, while dan do – while untuk menghasilkan output dibawah ini :

1. 10. Kharisma                                  2.            5                              3.            9                             
     20. Kharisma                                                 4                                              6
     30. Kharisma                                                 3                                              3
     40. Kharisma                                                 2                                              0
     50. Kharisma                                                 1

  4. 1+2+3+4+5=15

Desain Form :
Proses Perulangan 1 akan menampilkan : 10.Kharisma, 20.Kharisma, dst.
Proses Perulangan 2 akan menampilkan : 5,4,3,2,1.
Proses Perulangan 3 akan menampilkan : 9,6,3,0.
Proses Perulangan 4 akan menampilkan : 1+2+3+4+5=15.
Tombol Batal untuk membersihkan isi input dan hasil output.
Tombol Keluar untuk menutup program.



Komponen
Properties
Isi
Jlabel
Text
Masukan Jumlah Perulangan
Jlabel
Text
Hasil Perulangan 1
Jlabel
Text
Hasil Perulangan 2
Jlabel
Text
Hasil Perulangan 3
Jlabel
Text
Hasil Perulangan 4
JTextField
Name
Text
eBil
Kosongkan
JTextArea
Name
Text
areahasil1
Kosongkan
JTextArea
Name
Text
areahasil2
Kosongkan
JTextArea
Name
Text
areahasil3
Kosongkan
JTextArea
Name
Text
areahasil4
Kosongkan
JButton
Name
Text
bProses1
Proses Perulangan 1
JButton
Name
Text
bProses2
Proses Perulangan 2
JButton
Name
Text
bProses3
Proses Perulangan 3
JButton
Name
Text
bProses4
Proses Perulangan 4
JButton
Name
Text
bBatal
Batal
JButton
Name
Text
bKeluar
Keluar

Adapun kode program untuk tombol Batal sbb:
private void bBatalActionPerformed(java.awt.event.ActionEvent evt) {                                      
        // TODO add your handling code here:
        eBil.setText("");
        areahasil1.setText("");
        areahasil2.setText("");
        areahasil3.setText("");
        areahasil4.setText("");
        eBil.requestFocus();
    }

Adapun kode program untuk tombol Keluar sbb:
private void bKeluarActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        dispose();
    }

Tugas praktikum 7 dengan menggunakan for:
Tombol Proses Perulangan 1 :
private void bProses1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        int a,b;
       
        a=Integer.parseInt(eBil.getText());
        for(b=1;b<=a;b++){
            areahasil1.append(b+"0.Kharisma\n");
        }
    }                                       
Tombol Proses Perulangan 2 :
    private void bProses2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        int a,b;
       
        a=Integer.parseInt(eBil.getText());
        for(b=a;b>0;b--){
            areahasil2.append(b+"\n");
        }
    }     
Tombol Proses Perulangan 3 :
private void bProses3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        int a,b;
       
        a=Integer.parseInt(eBil.getText());
        for(b=a;b>=0;b=b-3){
            areahasil3.append(b+"\n");
        }
    }                                       
Tombol Proses Perulangan 4 :
    private void bProses4ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        int a,b;
        int jum=0;
        a=Integer.parseInt(eBil.getText());
        for(b=1;b<=a;b++){
            jum=jum+b;
           
            if(b<a){
                areahasil4.append(b+"+");
            }else{
                areahasil4.append(b+"="+jum);
            }
        }
    }            



Tugas praktikum 8 dengan menggunakan while:
Tombol Proses Perulangan 1 :
private void bProses1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        int a,b;
        a=Integer.parseInt(eBil.getText());
        b=1;
        while(b<=a){
            areahasil1.append(b+"0.Kharisma\n");
            b++;
        }
    }     
Tombol Proses Perulangan 2 :
    private void bProses2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        int a,b;
        a=Integer.parseInt(eBil.getText());
        b=a;
        while(b>0){
            areahasil2.append(b+"\n");
            b--;
        }
    }     



Tombol Proses Perulangan 3 :
private void bProses3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        int a,b;
       
        a=Integer.parseInt(eBil.getText());
        b=a;
        while(b>=0){
            areahasil3.append(b+"\n");
            b=b-3;
        }
    }           
Tombol Proses Perulangan 4 :
private void bProses4ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        int a,b;
        int jum=0;
        b=1;
        a=Integer.parseInt(eBil.getText());
        while(b<=a){
            jum=jum+b;
            if(b<a){
                areahasil4.append(b+"+");
            }else{
                areahasil4.append(b+"="+jum);
            }
            b++;                 }                  }                


Tugas praktikum 9 dengan menggunakan do - while:
Tombol Proses Perulangan 1 :
private void bProses1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        int a,b;
       
        a=Integer.parseInt(eBil.getText());
        b=1;
        do{
            areahasil1.append(b+"0.Kharisma\n");
            b++;
        }while(b<=a);
    } 
Tombol Proses Perulangan 2 :
private void bProses2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        int a,b;
        a=Integer.parseInt(eBil.getText());
        b=a;
        do{
            areahasil2.append(b+"\n");
            b--;
        }while(b>0);
    }       
Tombol Proses Perulangan 3 :
private void bProses3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        int a,b;
       
        a=Integer.parseInt(eBil.getText());
        b=a;
        do{
            areahasil3.append(b+"\n");
            b=b-3;
        }while(b>=0);
    }    

    
Tombol Proses Perulangan 4 :


private void bProses4ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        int a,b;
        int jum=0;
        b=1;
        a=Integer.parseInt(eBil.getText());
        do{
            jum=jum+b;
            if(b<a){
                areahasil4.append(b+"+");
            }else{
                areahasil4.append(b+"="+jum);
            }
            b++;
   }while(b<=a);
    }                  

Hasil Output Program :




Sertakan Sumber Bila Anda Ingin Copy-Paste
Hargai Karya Penulis


Tidak ada komentar:

Poskan Komentar