Sabtu, 16 Juli 2011

Program Operasi Matriks dengan Java

teman-teman udah lama gak posting jadi kengen sama blog ini(kayak pacar aja)
kali ini sy ingin posting program java operasi matriks, dimana pada program ini kita bisa melakukan perkalian 2 buah matriks dan Transpose ke-2 buah matriks tersebut. langsung aja ini source code programnya :

import javax.swing.*;

class operasimatrik {
public static void main(String[] args) {
System.out.println("********** Matriks **********\n");
int baris = Integer.parseInt(JOptionPane.showInputDialog(null,"Masukkan Jumlah Baris : ",2));
int kolom = Integer.parseInt(JOptionPane.showInputDialog(null,"Masukkan Jumlah Kolom : ",2));
int A[][]=new int [baris][kolom];
int B[][]=new int [baris][kolom];
int C[][]=new int [baris][kolom];
int D[][]=new int [baris][kolom];

do{
int pil=Integer.parseInt(JOptionPane.showInputDialog("PROGRAM MATRIK \n\n1. Inputan Matrik \n2. Perkalian Matrik A x B \n3. Perkalian Matrik B x A\n4. Transpose Matrik A \n5. Tranpose Matrik B \n6. Exit"));

switch(pil){ 
case 1:
//Matrik A
System.out.println("Matriks A");
for (int i=0;i<baris;i++){
  System.out.print(" | ");
  for (int j=0;j<kolom;j++){
    A[i][j]=Integer.parseInt(JOptionPane.showInputDialog(null,"Masukkan Matriks A ["+(i+1)+","+(j+1)+"]"));
    System.out.print(" "+A[i][j]+" ");
    }
  System.out.println(" | ");
}
//Matrik B
System.out.println("\nMatriks B");
for (int i=0;i<baris;i++){
  System.out.print(" | ");
  for (int j=0;j<kolom;j++){
    B[i][j]=Integer.parseInt(JOptionPane.showInputDialog(null,"Masukkan Matriks B ["+(i+1)+","+(j+1)+"]"));
    System.out.print(" "+B[i][j]+" ");
    }
  System.out.println(" | ");
}
continue;

case 2:
//Perkalian antar matrik
System.out.println("\nMatriks Perkalian");
for (int i=0;i<baris;i++){
  System.out.print(" | ");
  for (int j=0;j<kolom;j++){
    for (int k=0;k<baris;k++){
      C[i][j] = C[i][j]+(A[i][k]*B[k][j]) ;
    }
    System.out.print(" "+C[i][j]+" ");
  }
  System.out.println(" | ");
}
continue;

case 3:
//Perkalian antar matrik
System.out.println("\nMatriks Perkalian");
for (int i=0;i<baris;i++){
  System.out.print(" | ");
  for (int j=0;j<kolom;j++){
    for (int k=0;k<baris;k++){
      D[i][j] = D[i][j]+(B[i][k]*A[k][j]);
    }
    System.out.print(" "+D[i][j]+" ");
  }
  System.out.println(" | ");
}
continue;


case 4:
//Tranpose Matrik A
System.out.println("\nMatriks A Transpose");
for (int i=0;i<baris;i++){
  System.out.print(" | ");
  for (int j=0;j<kolom;j++){
    System.out.print(" "+A[j][i]+" ");
    }
  System.out.println(" | ");
}
continue;
case 5:
//Tranpose Matrik B
System.out.println("\nMatriks B Transpose");
for (int i=0;i<baris;i++){
  System.out.print(" | ");
  for (int j=0;j<kolom;j++){
    System.out.print(" "+B[j][i]+" ");
    }
  System.out.println(" | ");
}
continue;

case 6:System.exit(0);

default: JOptionPane.showMessageDialog(null,"Salah masukkan PILIHAN", "deFauLt", JOptionPane.INFORMATION_MESSAGE);
continue;
}

}while(JOptionPane.showConfirmDialog(null,"Mau hitung ulang lagi?")==JOptionPane.YES_OPTION);
System.exit(0);
}
}

gimana? keren khan?

Artikel Yang Berhubungan:

1 komentar:

 
" Blog Pelajar yang selalu ingin belajar "