Hi guys, jujur juga cinta bgt dah ama java n matematika. :p Nah, di postingan kali ini sengaja gw kasih contoh2 aplikasi program java buat operasi2 pada matematika informatika.
1. Operasi pada Vektor.
Berikut ini contoh program menghitung perkalian vektor.
import java.io.*;
class PerkalianVektor
{
public static void main(String[] args) throws Exception
{
BufferedReader input = new BufferedReader (new InputStreamReader (System.in));
int [] x = new int [10];
int [] v = new int [10];
System.out.println("Perkalian Vektor dengan Bilangan Skalar");
System.out.print("\nMasukkan nilai skalar = ");
int s = Integer.parseInt(input.readLine());
System.out.print("Masukkan jumlah elemen vektor = ");
int n = Integer.parseInt(input.readLine());
System.out.println("\nElemen Vektor");
for (int i=0; i<n; i++)
{
System.out.print("Elemen [1,"+i+"] = ");
v[i] = Integer.parseInt(input.readLine());
}
System.out.print("\nHasil perkalian vektor dengan skalar = [");
for (int i=0; i<n; i++)
{
x[i] = s*v[i];
System.out.print(x[i]);
if (i!=n-1)
{
System.out.print(",");
}
}
System.out.println("]");
}
}
2. Operasi pada Matriks
Berikut ini contoh program untuk operasi aritmatika pada matriks.
class Matriks
{
public static void main(String[] args)
{
String garis="===============================";
System.out.println("Operasi Aritmatika Pada Matriks\n"+garis);
int baris=Integer.parseInt(javax.swing.JOptionPane.showInputDialog(null,"Masukkan banyak baris untuk matriks","2"));
int kolom=Integer.parseInt(javax.swing.JOptionPane.showInputDialog(null,"Masukkan banyak kolom untuk matriks","2"));
System.out.println("\nMatriks A\n");
int A[][]=new int [baris][kolom];
for (int i=0; i<baris ;i++)
{
System.out.print("|");
for (int j=0; j<kolom; j++)
{
A[i][j]=Integer.parseInt(javax.swing.JOptionPane.showInputDialog(null,"Nilai A baris ke-"+(i+1)+", kolom ke-"+(j+1)+"=","0"));
System.out.print(" "+A[i][j]+" ");
}
System.out.println("|");
}
System.out.println("\nMatriks B\n");
int B[][]=new int [baris][kolom];
for (int i=0; i<baris ;i++)
{
System.out.print("|");
for (int j=0; j<kolom; j++)
{
B[i][j]=Integer.parseInt(javax.swing.JOptionPane.showInputDialog(null,"Nilai B baris ke-"+(i+1)+", kolom ke-"+(j+1)+"=","0"));
System.out.print(" "+B[i][j]+" ");
}
System.out.println("|");
}
System.out.println("\n \nPenjumlahan Pada Matriks\n"+garis+"\nA + B =\n");
for (int i=0; i<baris; i++)
{
System.out.print("|");
for (int j=0; j<kolom; j++)
{
System.out.print(" "+(A[i][j]+B[i][j])+" ");
}
System.out.println("|");
}
System.out.println("\n \nPengurangan Pada Matriks\n"+garis+"\nA - B =\n");
for (int i=0; i<baris; i++)
{
System.out.print("|");
for (int j=0; j<kolom; j++)
{
System.out.print(" "+(A[i][j]-B[i][j])+" ");
}
System.out.println("|");
}
System.out.println("\n \nPerkalian Matriks Dengan Skalar\n"+garis);
int s=Integer.parseInt(javax.swing.JOptionPane.showInputDialog(null,"Masukkan nilai skalar","1"));
System.out.println("\n"+s+"A =\n");
for (int i=0; i<baris; i++)
{
System.out.print("|");
for (int j=0; j<kolom; j++)
{
System.out.print(" "+(s*A[i][j])+" ");
}
System.out.println("|");
}
System.out.println("\n"+s+"B =\n");
for (int i=0; i<baris; i++)
{
System.out.print("|");
for (int j=0; j<kolom; j++)
{
System.out.print(" "+(s*B[i][j])+" ");
}
System.out.println("|");
}
}
}
Selamat mencoba :)
3 comments:
keren programnya, saya juga bikin mencari persamaan regresi menggunakan Java
http://cerita-marchy.blogspot.com/2011/06/regresi-java.html
thanks bgt broo,,,,,very useful
request perkalian vector dengan transpose nya donk gan(dot product), bingung ni q buatnya pake java
Posting Komentar