Matrik 3×3,Determinan ( Inheritanse ) (Java)

Gambar di atas merupakan hasil,Disini saya membuat 5 class . Pertama yang pastinya main,matrik3x3(Sebagai Proses ) , tulis , Cetak , getDeterminan.

1. Create Class Matrik3x3

public class Matrik3x3 {

/**
* @param args the command line arguments
*/
public static double determinan(int b[][], int m) {
int i,j, c[][] = new int[5][5];
double sum = 0;
if(m==2){
sum = b[0][0]*b[1][1] – b[0][1]*b[1][0];
return sum;
}
for(int p=0;p<m;p++){
int h = 0,k = 0;
for(i=1;i<m;i++){
for(j=0;j<m;j++){
if(j==p)
continue;
c[h][k] = b[i][j];
k++;
if(k == m-1){
h++;
k = 0;
}
}
}
sum = sum + b[0][p] * Math.pow(-1,p) * determinan(c,(m-1));
}
return sum;
}
}

2.Create Class Tulis

import java.io.DataInputStream;
import java.io.IOException;

/**
*
* @author aerdy
*/
public class Tulis{

public static void tulis(int b[][], int m) throws IOException{
DataInputStream input = new DataInputStream(System.in);
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
System.out.print("Elemen ke ["+(i+1)+","+(j+1)+"]"+" : ");
b[i][j] = Integer.parseInt(input.readLine());
}
}
}

}

3. Create Class Cetak

public class Cetak{

public static void cetak(int b[][], int m){
for(int i=0;i<m;i++){
System.out.print(" ");
for(int j=0;j<m;j++)
System.out.print(b[i][j]+" ");
System.out.print("\n");
}
}

}

4.Create Class getDeterminan

import java.io.IOException;

/**
*
* @author aerdy
*/
public class getDeterminan extends Matrik3x3{

Tulis tl = new Tulis(); // Menginstansikan kembali class Tulis
Cetak ct = new Cetak(); // Menginstansikan class cetak
public static void getDeterminan(int d) throws IOException{
int a[][] = new int[d][d]; // Array Untuk 3×3
double result;
Tulis.tulis(a, d); // Memanggil method tulis pada Class Tulis
Cetak.cetak(a, d); // Memanggil method cetak pada class cetak
result = determinan(a, d); // memanggil method determinan dari class Matrik3x3
System.out.println(“Hasil matriks diatas adalah : “+result); // menampilkan hasil
}

}

5.Craate Class Main

import java.io.DataInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author aerdy
*/
public class Main extends getDeterminan{ // Merupakan class utama dari seluruh class

public static void main(String[] args) {

DataInputStream inputs = new DataInputStream(System.in);
getDeterminan gt = new getDeterminan(); //Menginstansikan class getDeterminan
try {
getDeterminan(3); //Memanggil method getDeterminan Untuk di eksekusi

} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}

}

}

Silakan Download Projecnya Di Sini

Advertisements

Published by

jarcode

hello I am jarcode

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s