Created
March 21, 2017 13:22
-
-
Save theoyrus/ecaea1620f0b03b9fc47db4d1ee554ea to your computer and use it in GitHub Desktop.
Tugas SDT 2, Operasi Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
class OperasiArray { | |
Scanner masukan = new Scanner(System.in); | |
public void masukdata (float data[][]) { | |
for (int i=0; i<3; i++){ | |
for (int j=0; j<3; j++){ | |
System.out.print("(" + (i + 1) + " . " + (j + 1) + ") :"); | |
data[i][j] = masukan.nextFloat(); | |
} | |
} | |
} | |
public float[][] tambah(float AA[][], float BB[][]){ | |
float hasil[][] = new float[3][3]; | |
for (int i=0; i<3; i++) | |
for (int j=0; j<3; j++) | |
hasil[i][j] = AA[i][j] + BB[i][j]; | |
return hasil; | |
} | |
public float[][] kurang(float AA[][], float BB[][]){ | |
float hasil[][] = new float[3][3]; | |
for (int i=0; i<3; i++) | |
for (int j=0; j<3; j++) | |
hasil[i][j] = AA[i][j] - BB[i][j]; | |
return hasil; | |
} | |
public float[][] kali(float AA[][], float BB[][]){ | |
float hasil[][] = new float[3][3]; | |
for(int i=0; i<3; i++){ | |
for(int j=0; j<3; j++){ | |
hasil[i][j] = 0; | |
for(int k=0; k<3; k++){ | |
hasil[i][j] += AA[i][k] * BB[k][j]; | |
} | |
} | |
} | |
return hasil; | |
} | |
public void tampilData(float data[][], char nama){ | |
for (int i=0; i<3; i++){ | |
for (int j=0; j<3; j++) | |
System.out.print("\t" + nama + "[" + (i + 1) + "][" + (j + 1) + "] = " + data[i][j] + ""); | |
System.out.println(); | |
} | |
} | |
public static void main(String args[]){ | |
float A[][] = new float[3][3]; | |
float B[][] = new float[3][3]; | |
float C[][] = new float[3][3]; | |
OperasiArray operasi = new OperasiArray(); | |
System.out.println("masukkan matriks A"); | |
operasi.masukdata(A); | |
System.out.println("masukkan matriks B"); | |
operasi.masukdata(B); | |
System.out.println("\n Data Matrik \n"); | |
operasi.tampilData(A, 'A'); | |
System.out.println("\n"); | |
operasi.tampilData(B, 'B'); | |
System.out.println("\n Operasi Matrik Penjumlahan (A+B) \n"); | |
C = operasi.tambah(A, B); | |
operasi.tampilData(C, 'C'); | |
System.out.println("\n Operasi Matrik Pengurangan (A-B) \n"); | |
C = operasi.kurang(A, B); | |
operasi.tampilData(C, 'C'); | |
System.out.println("\n Operasi Matrik Perkalian (A*B) \n"); | |
C = operasi.kali(A, B); | |
operasi.tampilData(C, 'C'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment