Laman

Rabu, 30 November 2011

Programing with Java2 (perhitungan pada Matrix)

public class Matrixx {
    public static void main(String[]args){

        int BarisMatrix1 = Integer.parseInt(JOptionPane.showInputDialog("Matrix 1\nJumlah Baris"));
        int KolomMatrix1 = Integer.parseInt(JOptionPane.showInputDialog("Matrix 1\nJumlah Kolom"));

        int BarisMatrix2 = Integer.parseInt(JOptionPane.showInputDialog("Matrix 2\nJumlah Baris"));

        int KolomMatrix2 = Integer.parseInt(JOptionPane.showInputDialog("Matrix 2\nJumlah Kolom"));

        int[][] matrix1 = new int[BarisMatrix1][KolomMatrix1];
        int[][] matrix2 = new int[BarisMatrix2][KolomMatrix2];
        int[][] matrix3 = new int[BarisMatrix1][KolomMatrix1];

        //input array matrix1
        for(int i=0;i<BarisMatrix1;i++){
            for(int j=0;j<KolomMatrix1;j++){
                matrix1[i][j] =  Integer.parseInt(JOptionPane.showInputDialog("Matrix 1 - Data\nBaris ke "+(i+1)+" Kolom ke "+(j+1)));
            }
        }
        //input array matrix2
        for(int i=0;i<BarisMatrix2;i++){
            for(int j=0;j<KolomMatrix2;j++){
                matrix2[i][j] =  Integer.parseInt(JOptionPane.showInputDialog("Matrix 2 - Data\nBaris ke "+(i+1)+" Kolom ke "+(j+1)));
            }
        }
        int menu=0;
        do{
            menu = Integer.parseInt(JOptionPane.showInputDialog("Matrix"
                    + "\n1. Tampilkan"
                    + "\n2. Tambah Matrix"
                    + "\n3. Kurang Matrix"
                    + "\n4. Kali Matrix"
                    + "\n5. Keluar"));
            switch(menu){
                case 1://Tampilkan Array
                    //output array matrix1
                    System.out.println("==> Matrix 1");
                    for(int i=0;i<BarisMatrix1;i++){
                        for(int j=0;j<KolomMatrix1;j++)
                            System.out.print(matrix1[i][j]+" ");
                        System.out.println();
                    }
                    System.out.println();
                    //output array matrix2
                    System.out.println("==> Matrix 2");
                    for(int i=0;i<BarisMatrix2;i++){
                        for(int j=0;j<KolomMatrix2;j++)
                            System.out.print(matrix2[i][j]+" ");
                        System.out.println();
                    }
                    break;
                case 2://Tambah Array
                    if(BarisMatrix1==BarisMatrix2){
                        if(KolomMatrix1==KolomMatrix2){
                            //input array matrix3
                            for(int i=0;i<BarisMatrix1;i++){
                                for(int j=0;j<KolomMatrix1;j++){
                                    matrix3[i][j] = matrix1[i][j]+matrix2[i][j];
                                }
                            }

                            //output array matrix3
                            System.out.println("==> Hasil Matrix 3");
                            for(int i=0;i<BarisMatrix1;i++){
                                for(int j=0;j<KolomMatrix1;j++)
                                    System.out.print(matrix3[i][j]+" ");
                                System.out.println();
                            }
                        }
                    }
                    break;
                case 3://Kurang Array
                    if(BarisMatrix1==BarisMatrix2){
                        if(KolomMatrix1==KolomMatrix2){
                            //input array matrix3
                            for(int i=0;i<BarisMatrix1;i++){
                                for(int j=0;j<KolomMatrix1;j++){
                                    matrix3[i][j] = matrix1[i][j]-matrix2[i][j];
                                }
                            }

                            //output array matrix3
                            System.out.println("==> Hasil Matrix 3");
                            for(int i=0;i<BarisMatrix1;i++){
                                for(int j=0;j<KolomMatrix1;j++)
                                    System.out.print(matrix3[i][j]+" ");
                                System.out.println();
                            }
                        }
                    }
                    break;
                case 4://Kali Array
                    if(BarisMatrix1==BarisMatrix2){
                        if(KolomMatrix1==KolomMatrix2){
                            //input array matrix3
                            for(int i=0;i<BarisMatrix1;i++){
                                for(int j=0;j<KolomMatrix1;j++){
                                    matrix3[i][j] = matrix1[i][j]*matrix2[i][j];
                                }
                            }

                            //output array matrix3
                            System.out.println("==> Hasil Matrix 3");
                            for(int i=0;i<BarisMatrix1;i++){
                                for(int j=0;j<KolomMatrix1;j++)
                                    System.out.print(matrix3[i][j]+" ");
                                System.out.println();
                            }
                        }
                    }
                    break;
                case 5:
                    JOptionPane.showMessageDialog(null,"Terima Kasih");
                    break;
                default:
                    JOptionPane.showMessageDialog(null,"Input Salah");
                    break;   
            }
        }while(menu!=5);
    }
}

Tidak ada komentar:

Posting Komentar