nama file -> DataMaxMin.java
Program ini otomatis akan mencari nilai terkecil dan nilai terbesar dari input data.
Code:
Penerapan yang kedua program penjumlahan matriks dengan menggunakan konsep array
/*
* @author neonmap 2007
*/
import java.io.*;
public class DataMaxMin
{
public static void main(String args[])
{
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("Masukkan jumlah data : ");
int jumlahData=Integer.parseInt(input.readLine());
int[] data=new int[jumlahData];
System.out.print("\nData adalah ");
for (int i=0;i<data.length;i++)
{
data[i]=(int)(Math.random()*100);
System.out.print(data[i]+" ");
}
int max=data[0],min=data[0];
for (int i=0;i<data.length;i++)
{
if (data[i]>max)
max=data[i];
if (data[i]<min)
min=data[i];
}
System.out.println("\n\nData maksimum adalah "+max);
System.out.println("Data minimum adalah "+min);
} catch (Exception e) {
System.out.println("\nInput data Salah!");
}
}
}
nama file -> jumlahMatriks.java
Code:
/*
* @author neonmap 2007
*/
public class jumlahMatriks
{
public static void main(String args[])
{
int[][] A={{1,2,3},{4,5,6},{7,8,9}};
int[][] B={{9,8,7},{6,5,4},{3,2,1}};
int[][] C=new int[A.length][A[0].length];
//mencetak matriks A dan matriks B
System.out.println("Matriks A + Matrix B =");
for (int baris=0;baris<A.length;baris++)
{
for (int kolom=0;kolom<A[0].length;kolom++)
{
System.out.print(A[baris][kolom]+" ");
}
System.out.print(" ");
if (baris==1)
System.out.print("+");
else
System.out.print(" ");
System.out.print(" ");
for (int kolom=0;kolom<B[0].length;kolom++)
{
System.out.print(B[baris][kolom]+" ");
}
System.out.println();
}
//penjumlahan matrix
System.out.println("\nHasil =");
for (int baris=0;baris<A.length;baris++)
{
for (int kolom=0;kolom<A[0].length;kolom++)
{
C[baris][kolom]=A[baris][kolom]+B[baris][kolom];
System.out.print(C[baris][kolom]+" ");
}
System.out.println();
}
}
}
Ada juga perkalian matriks
nama file -> kaliMatriks.java
Code:
/*
* @author neonmap 2007
*/
public class kaliMatriks
{
public static void main(String args[])
{
int[][] A={{1,2,3},{4,5,6},{7,8,9}};
int[][] B={{9,8,7},{6,5,4},{3,2,1}};
int[][] C=new int[A.length][A[0].length];
//mencetak matriks A dan matriks B
System.out.println("Matriks A x Matrix B =");
for (int baris=0;baris<A.length;baris++)
{
for (int kolom=0;kolom<A[0].length;kolom++)
{
System.out.print(A[baris][kolom]+" ");
}
System.out.print(" ");
if (baris==1)
System.out.print("x");
else
System.out.print(" ");
System.out.print(" ");
for (int kolom=0;kolom<B[0].length;kolom++)
{
System.out.print(B[baris][kolom]+" ");
}
System.out.println();
}
//perkalian matrix
System.out.println("\nHasil =");
for (int baris=0;baris<A.length;baris++)
{
for (int kolom=0;kolom<A[0].length;kolom++)
{
C[baris][kolom]=0;
for (int k=0;k<B.length;k++)
{
C[baris][kolom]+=A[baris][k]*B[k][kolom];
}
System.out.print(C[baris][kolom]+" ");
}
System.out.println();
}
}
}
Yang terakhir gue kasih program searching sederhana (siapa tau bisa ngalahkan google r0~f>
nama file -> searching.java
Code:
/*
* @author neonmap 2007
*/
import java.io.*;
public class searching
{
public static void main(String args[])
{
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("Masukkan jumlah data : ");
int jumlahData=Integer.parseInt(input.readLine());
int[] data=new int[jumlahData];
System.out.print("\nData adalah ");
for (int i=0;i<data.length;i++)
{
data[i]=(int)(Math.random()*100);
System.out.print(data[i]+" ");
}
System.out.print("\nMasukkan data yang akan dicari: ");
int cariData=Integer.parseInt(input.readLine());
int x;
for (x=0;x<data.length;x++)
{
if (data[x]==cariData)
break;
}
int posisi=x;
if (posisi<data.length)
System.out.println("\nData yang Anda cari ada pada posisi "+(posisi+1));
else
System.out.println("\nMaaf data tidak tersedia.");
} catch (Exception e) {
System.out.println("\nInput data Salah!");
}
}
}
No comments:
Post a Comment
thank you for commenting on our blogs