import java.util.Scanner;
public class latihan1 {
//property
private int [] data;
//konstruktor
public latihan1(int n){
data = new int[n];
}
//fungsi set data
public void SetData(int index, int value){
if (index >=0 && index < data.length){
data[index] = value;
}
}
//fungsi prncarian beruntun
public int Cari(int x){
for (int a=0; a<data.length; a++){
if (data[a] == x){
return a;
}
}
return -1;
}
public static void main(String [] args){
//buat scanner
Scanner inp = new Scanner(System.in);
//tanya jumlah data
System.out.print("jumlah data=");
int n = inp.nextInt();
//buat objek
latihan1 ss1 = new latihan1(n);
//masukkan data
for (int a=0; a<n; a++) {
System.out.print("data ke "+a+" =");
ss1.SetData(a, inp.nextInt());
}
//tanaya angka yang di cari
System.out.print("nilai yang di cari=");
int x = inp.nextInt();
//hasil
System.out.print("data "+x+" ada di index ke "+ss1.Cari(x));
}
}
Selasa, 24 Juni 2014
Langganan:
Posting Komentar (Atom)
0 komentar:
Posting Komentar