import java.util.Scanner;
public class latihan2 {
//property
private String [] data;
//konstruktor
public latihan2(int n){
data = new String[n];
}
//fungsi set data
public void SetData(int index, String value){
if (index >=0 && index < data.length){
data[index] = value;
}
}
//fungsi prncarian beruntun
public int Cari(String x){
for (int a=0; a<data.length; a++){
if (data[a].equals(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
latihan2 ss1 = new latihan2(n);
//masukkan data
for (int a=0; a<n; a++) {
System.out.print("data ke "+a+" =");
ss1.SetData(a, inp.next());
}
//tanaya angka yang di cari
System.out.print("nilai yang di cari=");
String x = inp.next();
//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