Selasa, 28 Mei 2013

Source code Penjumlah dan Pengurangan Matriks pada Java

Source code Penjumlah dan Pengurangan Matriks orde 3 X 3 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PraktikumASD;

import java.util.Scanner;

/**
 *
 * @author Nenov
 */
public class Matriks {

    private int A[][] = new int[3][3];
    private int B[][] = new int[3][3];
    private int C[][] = new int[3][3];

    public void input() {
        Scanner input = new Scanner(System.in);

        int i, j, k, m;

        System.out.println("   Matriks A");
        for (i = 0; i < 3; i++) {
            for (j = 0; j < 3; j++) {
                System.out.print("Baris " + (i + 1) + " Kolom " + (j + 1) + " : ");
                A[i][j] = input.nextInt();
            }
            System.out.println("");
        }

        System.out.println("\n    Matriks B");
        for (k = 0; k < 3; k++) {
            for (m = 0; m < 3; m++) {
                System.out.print("Baris " + (k + 1) + " Kolom " + (m + 1) + " : ");
                B[k][m] = input.nextInt();
            }
            System.out.println("");
        }
    }

    public void penjumlahan() {
        System.out.println("\nHasil Penjumlahan Matriks A dan B adalah ");

        for (int x = 0; x < 3; x++) {
            for (int y = 0; y < 3; y++) {
                C[x][y] = A[x][y] + B[x][y];
            }
        }
    }

    public void pengurangan() {
        System.out.println("\nHasil Pngurangan Matriks A dan B adalah ");

        for (int x = 0; x < 3; x++) {
            for (int y = 0; y < 3; y++) {
                C[x][y] = A[x][y] - B[x][y];
            }
        }
    }

    public void Cetak() {
        for (int x = 0; x < 3; x++) {
            for (int y = 0; y < 3; y++) {
                System.out.print(C[x][y] + "   ");
            }
            System.out.println("");
        }

    }

}
class MainMatriks{
    public static void main(String[] args) {
    Matriks a = new Matriks();
    a.input();
    a.penjumlahan();
    a.Cetak();
    a.pengurangan();
    a.Cetak();
        
    }
}