Java Zone

Pemrograman java, Perulangan Java, Operator java, Thread Java, Makalah Filsafat Ilmu, Makalah Teologi dan lain lain

Powered by Blogger.

Array Pada Java

Array Dua Dimensi

Contoh Program:
Nama program: Contoh1.java
import javax.swing.JOptionPane;
public class Contoh1 {
 public static void main(String[] args) {
 int baris = 0;
 int kolom = 0;

 baris = Integer.parseInt(JOptionPane.showInputDialog("baris:"));
 kolom = Integer.parseInt(JOptionPane.showInputDialog("kolom:"));

 int[][] matriksA = new int[baris][kolom];
 int[][] matriksB = new int[baris][kolom];
 int[][] matriksC = new int[baris][kolom];

 for(int x=0; x<baris; x++) {
 for(int y=0; y<kolom; y++){
 matriksA[x][y] = (int)(Math.random()*100);
 matriksB[x][y] = (int)(Math.random()*100);
 matriksC[x][y] = matriksA[x][y] + matriksB[x][y];
 }
 }

 System.out.println("matriks A");
 System.out.println("---------");
 for(int x=0; x<baris; x++) {
 for(int y=0; y<kolom; y++){
 System.out.print(matriksA[x][y] + "\t");
 }
 System.out.println();
 }

 System.out.println();
 System.out.println("matriks B");
 System.out.println("---------");
 for(int x=0; x<baris; x++) {
 for(int y=0; y<kolom; y++){
 System.out.print(matriksB[x][y] + "\t");
 }
 System.out.println("");
 }

 System.out.println();
 System.out.println("matriks A + B");
 System.out.println("-------------");
 for(int x=0; x<baris; x++) {
 for(int y=0; y<kolom; y++){
 System.out.print(matriksC[x][y] + "\t");
 }
 System.out.println();
 }

 }

}
output program:

input jumlah baris
input jumlah baris
input kolom
input kolom

matriks A
---------
96        32        95        
76        19        24        
59        21        40        
93        60        23        

matriks B
---------
47        20        62        
77        29        82        
25        98        67        
36        7        42        

matriks A + B
-------------
143        52        157        
153        48        106        
84        119        107        
129        67        65       
Nama program: Contoh2.java
import javax.swing.*;
public class Contoh2 {
 public static void main(String[] args) {
 int barisA=0;
 int kolomA=0;
 int barisB=0;
 int kolomB=0;

 barisA = Integer.parseInt(JOptionPane.showInputDialog("jumlah baris matriks A:"));
 kolomA = Integer.parseInt(JOptionPane.showInputDialog("jumlah kolom matriks A:"));
 barisB = kolomA;
 kolomB = Integer.parseInt(JOptionPane.showInputDialog("jumlah kolom matriks B:"));

 int[][] matriksA = new int[barisA][kolomA];
 int[][] matriksB = new int[barisB][kolomB];
 int[][] matriksC = new int[barisA][kolomB];

 for(int x = 0; x<barisA; x++) {
 for(int y = 0; y<kolomA; y++) {
 matriksA[x][y] = (int)(Math.random()*100);
 }
 }

 for(int x = 0; x<barisB; x++) {
 for(int y = 0; y<kolomB; y++) {
 matriksB[x][y] = (int)(Math.random()*100);
 }
 }

 for(int x = 0; x<barisA; x++) {
 for(int y = 0; y<kolomB; y++) {
 matriksC[x][y] = 0;
 for(int a=0; a<barisB; a++)
 matriksC[x][y] += (matriksA[x][a] * matriksB[a][y]);
 }
 }

 System.out.println("Matriks A");
 System.out.println("---------");
 for(int x = 0; x<barisA; x++) {
 for(int y = 0; y<kolomA; y++) {
 System.out.print(matriksA[x][y] + "  ");
 }
 System.out.println();
 }

 System.out.println();
 System.out.println("Matriks B");
 System.out.println("---------");
 for(int x = 0; x<kolomA; x++) {
 for(int y = 0; y<kolomB; y++) {
 System.out.print(matriksB[x][y] + "  ");
 }
 System.out.println();
 }

 System.out.println();
 System.out.println("matriks A x B");
 System.out.println("-------------");
 for(int x = 0; x<barisA; x++) {
 for(int y = 0; y<kolomB; y++) {
 System.out.print(matriksC[x][y] + "  ");
 }
 System.out.println();
 }

 }

}
output program:
Matriks A
---------
18  92  87  
31  38  52  
42  79  48  
13  8  97  

Matriks B
---------
28  69  
64  10  
59  41  

matriks A x B
-------------
11525  5729  
6368  4651  
9064  5656  
6599  4954 
Bagikan :
+
Previous
Next Post »
0 Komentar untuk "Array Pada Java"

 
Copyright © 2015 Java Zone - All Rights Reserved
Template By Kunci Dunia
Back To Top