Minggu, 23 Januari 2011

Program Mencari Determinan Menggunakan Kofaktor


/*
* Matriks.java
*
* Created on January 23, 2011, 4:28 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

/**
*
* @author Mr. Miscall
*/
public class Matriks {
protected int baris;
protected int kolom;
protected double[][] nilai;

/*constructor -- Nilai dari matriks diset nol*/
public Matriks(int baris, int koloms) {
if (baris <= 0 || koloms <= 0) throw new IndexOutOfBoundsException();
this.baris = baris;
this.kolom = koloms;
this.nilai = new double[baris][koloms];
this.zero();
}

/*Full constructor -- Nilai dari matriks diset sesuai dengan nilai yang diberikan*/
public Matriks(int baris, int koloms, double[][] nilai) {
this(baris, koloms);
this.set(nilai);
}

/*Prosedure Membuat Matriks Nol*/
public void zero() {
for (int i = 0; i < this.baris; i++) {
for (int j = 0; j < this.kolom; j++) {
this.nilai[i][j] = 0;
}
}
}

/*Prosedure Menset Nilai Matriks*/
public void set(double[][] nilai) {
if (nilai == null) return;
int baris = nilai.length;
if (baris == 0) return;
for (int i = 0; i < baris && i < this.baris; i++) {
double[] row = nilai[i];
if (row != null) {
int kolom = row.length;
if (kolom != 0) {
kolom = kolom > this.kolom ? this.kolom : kolom;
System.arraycopy(row, 0, this.nilai[i], 0, kolom);
}
}
}
}

/*Fungsi Mengambil SubMatriks*/
public Matriks getSubMatriks(int row, int col) throws IndexOutOfBoundsException {
if (this.baris == 1 && row == 0) throw new IndexOutOfBoundsException();
if (this.kolom == 1 && col >= 0) throw new IndexOutOfBoundsException();

int nRows = (row >= this.baris || row < 0) ? this.baris : this.baris - 1;
int nCols = (col >= this.kolom || col < 0) ? this.kolom : this.kolom - 1;

int a = 0;
int b = 0;

Matriks m = new Matriks(nRows, nCols);

for (int i = 0; i < baris; i++) {
if (i != row) {
for (int j = 0; j < kolom; j++) {
if (j != col) {
m.nilai[a][b] = this.nilai[i][j];
b++;
}
}
a++;
b = 0;
}
}
return m;
}

/*Fungsi Rekursif Mencari Determinan Metode Cofactor*/
public double determinant(){
//untuk matriks bukan persegi
if (this.baris != this.kolom) {System.out.print("Tidak bisa!! Jumlah baris harus sama dengan jumlah kolom."); return 0;}

//untuk matriks 1x1
if (this.baris == 1) return this.nilai[0][0];

//untuk matriks 2x2
if (this.baris == 2) return this.nilai[0][0] * this.nilai[1][1] - this.nilai[0][1] * this.nilai[1][0];

//untuk matriks 3x3 ke atas
double det = 0;
for (int i = 0; i < baris; i++) {
det += (i % 2 == 0 ? 1 : -1) * this.nilai[i][0] * this.getSubMatriks(i, 0).determinant();
}
return det;
}

public void cetakIsiMatriks() {
System.out.println("Matriks =");
for(int i=0; i < this.baris; i++){
for(int j=0; j < this.kolom; j++){
System.out.print(this.nilai[i][j]+" ");
}
System.out.println();
}
}

public static void main(String[] args){
Matriks a = new Matriks(1, 1, new double[][] {{1}});
a.cetakIsiMatriks();
System.out.println("Determinan = "+a.determinant());

Matriks b = new Matriks(2, 2, new double[][] {{1, 3}, {1, 1}});
b.cetakIsiMatriks();
System.out.println("Determinan = "+b.determinant());

Matriks c = new Matriks(2, 1, new double[][] {{1}, {2}});
c.cetakIsiMatriks();
System.out.println("Determinan = "+c.determinant());

Matriks d = new Matriks(3, 3, new double[][] {{1, 3, 1}, {1, 1, 2}, {2, 3, 4}});
d.cetakIsiMatriks();
System.out.println("Determinan = "+d.determinant());

Matriks e = new Matriks(5, 5, new double[][] {{1, 1, 1, 1, 1}, {1, 2, 1, 2, 1}, {2, 3, 4, 3, 1}, {1, 1, 7, 5, 1},{1, 1, 1, 5, 1}});
e.cetakIsiMatriks();
System.out.println("Determinan = "+e.determinant());
}
}

Sabtu, 15 Januari 2011

Sad

All the word f*ck, sh*t, assh*le, d*mn, s*ck, and its friends are said when somebody is in bad mood. And now I do. I don't understand why I was angry with. What is called friends when they pretend not to know. I don't want this life ever. :(

Senin, 03 Januari 2011

Konversi Angka ke Bilangan Romawi


/*
* konversiToRomawi.java
*
* Created on January 4, 2011, 10:06 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

/**
*
* @author Mr. Miscall
*/

import javax.swing.JOptionPane;

public class konversiToRomawi
{
public static void konversi(int angka)
{
int i;
char romawi[]={'I','V','X','L','C','D','M'};

if(angka>=1&&angka<5)
{
if(angka==4) System.out.print(romawi[0]+""+romawi[1]);
else for(i=0;i }
else if(angka>=5&&angka<10)
{
if(angka==9) System.out.print(romawi[0]+""+romawi[2]);
else
{
System.out.print(romawi[1]);
if(angka!=5) konversi(angka-5);
}
}
else if(angka>=10&&angka<50)
{
if(angka>=40)
{
System.out.print(romawi[2]+""+romawi[3]);
if(angka>40) konversi(angka-40);
}
else
{
if(angka%10==0) for(i=0;i else
{
System.out.print(romawi[2]);
konversi(angka-10);
}
}
}
else if(angka>=50&&angka<100)
{
if(angka>=90)
{
System.out.print(romawi[2]+""+romawi[4]);
if(angka>90) konversi(angka-90);
}
else
{
System.out.print(romawi[3]);
if(angka!=50) konversi(angka-50);
}
}
else if(angka>=100&&angka<500)
{
if(angka>=400)
{
System.out.print(romawi[4]+""+romawi[5]);
if(angka>400) konversi(angka-400);
}
else
{
if(angka%100==0) for(i=0;i else
{
System.out.print(romawi[4]);
konversi(angka-100);
}
}
}
else if(angka>=500&&angka<1000)
{
if(angka>=900)
{
System.out.print(romawi[4]+""+romawi[6]);
if(angka>900)konversi(angka-900);
}
else
{
System.out.print(romawi[5]);
if(angka!=500) konversi(angka-500);
}
}
else if(angka>=1000&&angka<5000)
{
if(angka%1000==0) for(i=0;i else
{
System.out.print(romawi[6]);
konversi(angka-1000);
}
}

else System.out.print(""); //untuk angka>=5000, karena keterbatasan
//compiler dalam menampilkan character unicode, maka tidak bisa ditampilkan
}


public static void main(String[] args)
{
int x;
String input;

do
{
input = JOptionPane.showInputDialog(null,"Masukkan bilangan [1-5000]= ","Input Dialog",JOptionPane.QUESTION_MESSAGE);
x = Integer.parseInt(input);
if(x<1||x>=5000)
{
JOptionPane.showMessageDialog(null,"Maaf, input anda di luar batas range. Harap input ulang","Pesan Error",JOptionPane.INFORMATION_MESSAGE);
}
}
while(x<1||x>=5000);

System.out.print("Angka = "+x);
System.out.print("\nBilangan romawi = ");
konversi(x);
}
}