Bermain dengan Array di Java

Array merupakan objek yang dapat digunakan untuk menyimpan sejumlah data. Data yang dapat disimpan dapat berupa tipe data ataupun kelas (objek).

Beberapa contoh aplikasi array di Java (barangkali besok keluar di tugas kuliah).. 🙂

Membuat program untuk menginputkan 2 buah
matrik (misal matrik A dan matrik B).

/* ————————-
* author : Dian – UMBY
————————— */
import java.io.*;
public class Matrix1
{
public static void main(String[]args)
throws IOException
{
InputStreamReader inStream =new InputStreamReader(
System.in ) ;
BufferedReader darikeyboard =new BufferedReader( inStream
);
String inputData;

int[][]a =new int[2][3];
int[][]b =new int[2][3];
int i,j,x,y;

//entry matrik
System.out.println(“Input Matrik A”);
System.out.println(“—————–“);

for (i=0;i<=1;i++)
{
for (j=0;j<=2;j++)
{
System.out.print(“Matrik A [“+i+”,”+j+”] = “);

inputData=darikeyboard.readLine();a[i][j]=Integer.parseInt(inputData);
}
}

System.out.println(“—————–“);

System.out.println(“Input Matrik B”);
System.out.println(“—————–“);

for (x=0;x<=1;x++)
{
for (y=0;y<=2;y++)
{
System.out.print(“Matrix B [“+x+”,”+y+”] = “);

inputData=darikeyboard.readLine();b[x][y]=Integer.parseInt(inputData);
}
}

System.out.println(“—————–“);

System.out.println(“Matrik A”);
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
{
System.out.print(a[i][j]+” “);
}
System.out.println();
}

System.out.println(“Matrik B”);
for(x=0;x<=1;x++)
{
for(y=0;y<=2;y++)
{
System.out.print(b[x][y]+” “);
}
System.out.println();
}
}
}

run:
Input Matrik A
—————–
Matrik A [0,0] = 1
Matrik A [0,1] = 2
Matrik A [0,2] = 3
Matrik A [1,0] = 4
Matrik A [1,1] = 5
Matrik A [1,2] = 6
—————–
Input Matrik B
—————–
Matrix B [0,0] = 6
Matrix B [0,1] = 5
Matrix B [0,2] = 4
Matrix B [1,0] = 3
Matrix B [1,1] = 2
Matrix B [1,2] = 1
—————–
Matrik A
1 2 3
4 5 6
Matrik B
6 5 4
3 2 1

Membuat operasi penjumlahan terhadap
matrik A dan matrik B.

/* ————————-
* author : Dian – UMBY
————————— */
import java.io.*;
public class Matrix2
{
public static void main(String[]args)
throws IOException
{
InputStreamReader inStream =new InputStreamReader( System.in ) ;
BufferedReader darikeyboard =new BufferedReader( inStream );
String inputData;

int[][]a =new int[2][3];
int[][]b =new int[2][3];
int[][]c =new int[2][3];
int i,j,k,l,x,y;

//entry matrik A
System.out.println(“Input Matrik A”);
System.out.println(“—————–“);

for (i=0;i<=1;i++)
{
for (j=0;j<=2;j++)
{
System.out.print(“Matrik A [“+i+”,”+j+”] = “);
inputData=darikeyboard.readLine();a[i][j]=Integer.parseInt(inputData);
}
}

System.out.println(“—————–“);

//entry matrik B
System.out.println(“Input Matrik B”);
System.out.println(“—————–“);

for (k=0;k<=1;k++)
{
for (l=0;l<=2;l++)
{
System.out.print(“Matrix B [“+k+”,”+l+”] = “);
inputData=darikeyboard.readLine();b[k][l]=Integer.parseInt(inputData);
}
}

System.out.println(“—————–“);

//hitung matrik C = matrik A + matrik B
for(x=0;x<=1;x++)
{
for(y=0;y<=2;y++)
{
c[x][y] = a[x][y] + b[x][y];
}
}

//tampilkan matrik A
System.out.println(“Matrik A”);
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
{
System.out.print(a[i][j]+” “);
}
System.out.println();
}

//tampilkan matrik B
System.out.println(“Matrik B”);
for(k=0;k<=1;k++)
{
for(l=0;l<=2;l++)
{
System.out.print(b[k][l]+” “);
}
System.out.println();
}

//tampilkan matrik C
System.out.println(“Hasil Penjumlahan Matrik A + Matrik B = Matrik C”);
for(x=0;x<=1;x++)
{
for(y=0;y<=2;y++)
{
System.out.print(c[x][y]+” “);

}
System.out.println();
}

}

}

run:
Input Matrik A
—————–
Matrik A [0,0] = 1
Matrik A [0,1] = 2
Matrik A [0,2] = 3
Matrik A [1,0] = 4
Matrik A [1,1] = 5
Matrik A [1,2] = 6
—————–
Input Matrik B
—————–
Matrix B [0,0] = 6
Matrix B [0,1] = 5
Matrix B [0,2] = 4
Matrix B [1,0] = 3
Matrix B [1,1] = 2
Matrix B [1,2] = 1
—————–
Matrik A
1 2 3
4 5 6
Matrik B
6 5 4
3 2 1
Hasil Penjumlahan Matrik A + Matrik B = Matrik C
7 7 7
7 7 7

Advertisements
Bermain dengan Array di Java

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